12,075,066 members (63,048 online)
alternative version

180.1K views
52 bookmarked
Posted

# Dependency-Grapher for C++-Projects

, 8 Jul 2002
 Rate this:
An add-in written in VB.NET to generate an inheritance and/or dependency graph of a c++-project. Uses the graphviz library from AT&T.
<!-- Article image -->

## Description

ClassDep is an add-in which generates a JPEG or JPG image showing an inheritance and/or a dependency graph of a C++ project in visual studio. is uses the graphviz tools from http://www.research.att.com/sw/tools/graphviz/. The picture above shows the generated dependency graph of one of my projects.

## Installation

Just double-click on the msi file and follow the installation instructions. All files from the graphviz library are included in the msi file.

## Usage

to use the addin simply click on the ClassDep-Entry in the Tool-Menu of Visual Studio .NET. It only works for C++ projects (C# and VB are not supported). The resulting picture files are stored in the corresponding project folder with the name(s) projectdep.jpg and/or projectdep.png. Please note that the picture files may become really huge for big projects!

## Implementation

for generating inheritance/dependency graphs it is usually necessary to parse the source code. but you sure have already noticed that visual studio does that in the "class-view"-window - it shows the base classes and the members of all project classes. So I wondered if and how I could use this built-in functionality of visual studio for my needs. the DTE-object was the perfect solution:

Dim applicationObject As EnvDTE.DTE
Dim project As Project

For Each project In applicationObject.ActiveSolutionProjects()
'...

'read out all c++-related object in the current project
BuildObjectList(project.CodeModel.CodeElements, 0)
For Each cl In project.CodeModel.CodeElements
If (cl.Kind = vsCMElement.vsCMElementClass) Then
'cl is a class
ScanBases(cl, 0)    'scan all base classes/structs
depfile.WriteLine(";")
If (ob.dependency.Checked) Then
ScanDependencies(cl)    'also scan all dependencies of the class
End If
End If
Next
'...

I started first with a macro and then decided to switch to an add-in because of the easier installation. That's the reason why this add-in is written in VB.

one problem of an addin is that I don't know a way to get the path of where the addin-dll is installed. that is also the path where the graphviz files are installed and I need to know that to use these files. the only way I know of is by using the registry. So the installation msi writes also a registry entry with the installation path:

Dim dotdir As String
dotdir = Registry.LocalMachine.OpenSubKey( _
False).GetValue("instpath", " ")

and last the call to the graphviz tools:

If ob.outjpeg.Checked Then
'call the graphviz tool with required params to generate the jpeg
Shell(dotdir + "\dot.exe -Tjpg """ + dir + "dep.txt"" -o """ + dir +_
project.Name + "dep.jpg""", AppWinStyle.MinimizedFocus, True)
End If
If ob.outpng.Checked Then
Shell(dotdir + "\dot.exe -Tpng """ + dir + "dep.txt"" -o """ + dir +_
project.Name + "dep.png""", AppWinStyle.MinimizedFocus, True)
End If

## Conclusion

There are a lot of improvements possible like a better ordering of the dependency graph and the like. Some of you might find the colors awful - feel free to change that

I also included the graphviz files inside the msi-installer. As far as I understand the license of the graphviz this is allowed - mail me if I'm wrong.

If someone knows how to change that ugly smiley-icon of the menu-entry please let me know!

## History

July 9 2002 - updated setup download.

July 15 2002 - fixed uncaught exception bug found by Brian D Pearson

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

## About the Author

 Software Developer Switzerland
---

## Comments and Discussions

 View All Threads First Prev Next
 Doesn't know Namespaces gschroecker17-Sep-03 0:51 gschroecker 17-Sep-03 0:51
 Re: Doesn't know Namespaces SteveKing17-Sep-03 11:37 SteveKing 17-Sep-03 11:37
 Last Visit: 31-Dec-99 19:00     Last Update: 11-Feb-16 15:57 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.