Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / Visual Basic
Article

Dependency-Grapher for C++-Projects

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
8 Jul 20022 min read 211.5K   3K   53   43
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.

Sample Image - ClassDep.jpg

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:

VB
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:

VB
Dim dotdir As String
dotdir = Registry.LocalMachine.OpenSubKey( _
                "Software\Microsoft\VisualStudio\7.0\AddIns\ClassDep.Connect", _
                False).GetValue("instpath", " ")

and last the call to the graphviz tools:

VB
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

License

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


Written By
Software Developer
Switzerland Switzerland
---

Comments and Discussions

 
GeneralNeeds to recompile Pin
acraft7-Dec-04 9:12
acraft7-Dec-04 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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