Click here to Skip to main content
13,455,913 members (47,113 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked
Posted 9 May 2004

XML Comments for Managed C++ Applications

, 9 Nov 2004
Rate this:
Please Sign up or sign in to vote.
Visual Studio .NET Add-in allowing XML style comments in Managed C++ applications.


Most of my application development these days is in C#. Over the past two years, I have grown accustomed to the C# XML comments built into the C# editor. As we all know by now, you can edit the project properties of your C# application and define an XML documentation file. With that processed XML file and the compiled assembly, NDoc can automatically build a Windows help file for your software.

There are days, though, when I have to leave the friendly confines of C# and drop down to C++. When this happens, I am usually writing a managed C++ class that hides unmanaged functionality (for example, serial port access). At that point, though, we have lost our ability to use XML commenting of our source code. Without XML commenting and the processing of those XML comments, we cannot use NDoc for that subsystem. That is a real problem, given that 90% of my projects are C# and documented in NDoc!

A while back, I wrote a Visual Studio Add-in (in C#) that handles XML commenting for managed C++ applications. It allows you to type XML comments. When the assembly is compiled, it processes the XML comments and builds a processed XML file that can be used as input to NDoc. This way, I can consistently comment all of my source code!

Writing Add-Ins

Support for add-ins in Visual Studio exists in the form of an "extensibility" project called "Visual Studio .NET Add-in". I will not cover the nuances of creating add-ins. If you want a good book on the subject, refer to the book "Developing Visual Studio .NET Macros and Add-Ins" by Jeff Cogswell. It is one of the few references I found.

Once you create an add-in project, you can then create event handlers for the specific events that are relevant for your add-in. There is an extensive object model describing the layout of the Visual Studio environment, solutions, and projects.

Two Areas To Address

When designing this add-in, there were two separate areas of concern. The first is to provide support for entering XML comments into source code. As we all know, when you start typing '///' in the C# editor, it automatically will expand if it precedes a method or property definition. I wanted to try to maintain a similar user experience.

The second area to address is the processing of those XML comments in order to build the XML comment files that can be used by NDoc.

Entering XML Comments Into Source Code

The first decision I made was that XML comments would be added to the header file. That is where all methods and properties will be defined, so naturally it is a logical place for the user to enter the XML comments. When I created the project, I was hoping to find an event that would fire each time the user pressed a key in the editor. However, that level of notification does not exist. I quickly discovered that the only editor event that came close was a line changed event from the TextEditorEvents object. This event gets fired every time the line number changes, including when the enter key is pressed.

Practically, this means that we cannot duplicate the exact behavior of the C# comment completion feature. The best that could be done is to make a check for '///' when a line changes. Hence, if you type '///' then press the enter key, the add-in will determine if XML comments are appropriate. If so, then it will expand the section to look like the familiar:


From there, you can enter a summary comment.

As noted above, when you enter a new comment section, the code in the Line Changed event will scan the editor to see if the comment is before a function or a variable definition. If it determines the target is a function, it will attempt to decode the parameters of the function and build the appropriate XML comment structure. For example:

///<param name="port"> </param>
///<returns> </returns>
bool    Open(int port);

If you are in the middle of an XML comment section and you press Enter, it will recognize your position and automatically add '///' to the start of the new line and space it with the other comment tags in the section. For example, in the example above, if the cursor was at the end of the 'param name = "port"' line and you pressed Enter, the comment section would look like this...

///<param name="port"> </param>
///<returns> </returns>
bool    Open(int port);

Processing XML Comments to make the XML File

After the XML comments are entered into the header file, they need to be processed to create the XML comment file. To do that, I create an event handler to monitor the Build Done event for the project. Whenever a project is built, that event will fire and the add-in is notified. It will check to make sure that the XML comment file should be built. It only builds the comment file for managed C++ projects that have been successfully built.

The building of the XML file is fairly straightforward. When the project is built, the compiler creates a data structure that describes all the entities associated with the code. Every property, method, enumeration, namespace, etc. is represented as a CodeElement. We can examine the CodeElement object for the project after it is built, to get a list of all entities in the project. We can iterate through the object list and examine the Kind each element represents. Based on the type, we create nodes in the XML document object that follows the format for the XML commenting scheme.

The code is not perfect

I developed this code rather quickly. Like most of us, I do not have time to make it perfect. It does what I need it to do.

There was one area in particular I wanted to address, but was unsuccessful. The way the add-in works now, the XML comment file will always be built if you are compiling a managed C++ application. The file it creates is based on the project file name. The file is always placed in the active configuration directory. For example, if the project is called 'MyProj' and you are building a Release build, the XML file will always be "Release/MyProj.xml". What I tried to do was build a project extender property that would allow you to specify the XML file name and location. I was unsuccessful in getting that done. If anyone has suggestions, let me know.


The add-in assumes that attribute definitions are defined immediately preceding a function definition on the lines above the function. When processing the code, the add-in examines each line using a simple Regex.IsMatch function. When looking for attributes, it uses "\\[(.)*\\]".


  • 5/14/2004 - Corrections for bug when processing function parameters.
  • 5/20/2004 - Added exception handling and stack trace when failure occurs.
  • 5/26/2004 - Handles interface documentation. Fixed misc. bugs in decoding parameter text.
  • 8/09/2004 - Attributes are now handled. See 'Limitations' section above for more details.
  • 9/22/2004 - Updated based on user feedback. Should fix problems with enumerations and parameters. Many thanks to M Ward and FredericM102LI90.
  • 11/09/2004 - Fixed exception being thrown when no .h file exists. Thanks to barrd for pointing this one out.


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

Jay Nelson
Architect Omron Adept Technologies, Inc
United States United States
I have been developing software professionaly since 1991 writing software in automation and manufacturing environments. For 14 years I worked for companies that built custom robotic automated equipment for the semiconductor, telecommunications, and other industies. Presently, I work for a company that manufacturers industrial robots where I write high level coordination and control software.

My undergraduate degrees are in Mathematics and Philosopy. My graduate degree is in Management Information Systems. I am MCSD certified in Visual C++ 6.0 and MCSD.NET certified in C#. I am also have the PMI-ACP certification.

I enjoy karate and reading.

You may also be interested in...


Comments and Discussions

Generalre: XML file will always be "Release/MyProj.xml" Pin
Jesse Chisholm24-Aug-15 8:16
memberJesse Chisholm24-Aug-15 8:16 
GeneralVS2008 Pin
LeslieB9-Dec-09 1:24
memberLeslieB9-Dec-09 1:24 
GeneralRe: VS2008 Pin
Member 442466824-Dec-11 8:22
memberMember 442466824-Dec-11 8:22 
GeneralCannot install for VS 2005 Pin
potrto12-Apr-07 23:00
memberpotrto12-Apr-07 23:00 
GeneralRe: Cannot install for VS 2005 Pin
mfgreen7719-Apr-07 4:31
membermfgreen7719-Apr-07 4:31 
GeneralRe: Cannot install for VS 2005 Pin
DEGT16-Nov-07 5:07
memberDEGT16-Nov-07 5:07 
GeneralRe: Cannot install for VS 2005 Pin
mfgreen7717-Nov-07 11:23
membermfgreen7717-Nov-07 11:23 
GeneralRe: Cannot install for VS 2005 Pin
DEGT18-Nov-07 21:14
memberDEGT18-Nov-07 21:14 
GeneralRe: Cannot install for VS 2005 Pin
mfgreen7720-Nov-07 2:48
membermfgreen7720-Nov-07 2:48 
GeneralNew version of MCXDoc for VS2005 (/clr:oldSyntax) [modified] Pin
void12320-Dec-06 6:02
membervoid12320-Dec-06 6:02 
GeneralRe: New version of MCXDoc for VS2005 (/clr:oldSyntax) Pin
James.Love2-Jan-07 8:23
memberJames.Love2-Jan-07 8:23 
GeneralRe: New version of MCXDoc for VS2005 (/clr:oldSyntax) Pin
ujos4-Jan-07 22:23
memberujos4-Jan-07 22:23 
General#include from unmanaged header file causes error [modified] Pin
stempsoft19-Nov-06 17:06
memberstempsoft19-Nov-06 17:06 
GeneralRe: #include from unmanaged header file causes error Pin
stempsoft22-Nov-06 10:53
memberstempsoft22-Nov-06 10:53 
GeneralUnable to extract XML: Edit Point not found Pin
stempsoft19-Nov-06 16:22
memberstempsoft19-Nov-06 16:22 
QuestionXML documentation for console application? Pin
minzhu127-Jun-06 15:59
memberminzhu127-Jun-06 15:59 
AnswerRe: XML documentation for console application? Pin
Jay Nelson28-Jun-06 3:03
memberJay Nelson28-Jun-06 3:03 
AnswerRe: XML documentation for console application? [modified] Pin
minzhu128-Jun-06 4:20
memberminzhu128-Jun-06 4:20 
GeneralAdd-in for documenting C++ Code [modified] Pin
marcoow7-Jun-06 4:03
membermarcoow7-Jun-06 4:03 
QuestionHow to show right root page! Pin
trongnghiavc18-May-06 21:11
membertrongnghiavc18-May-06 21:11 
QuestionNDoc support Framework 2 ? Pin
Redaemon22-Jan-06 5:39
memberRedaemon22-Jan-06 5:39 
AnswerRe: NDoc support Framework 2 ? Pin
Stephan Brenner26-Jan-06 4:27
memberStephan Brenner26-Jan-06 4:27 
QuestionProblem generating documentation Pin
IsaacB20-Dec-05 8:41
memberIsaacB20-Dec-05 8:41 
AnswerRe: Problem generating documentation Pin
jstevenco28-Dec-05 13:26
memberjstevenco28-Dec-05 13:26 
AnswerRe: Problem generating documentation Pin
void12314-Aug-06 21:33
membervoid12314-Aug-06 21:33 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180322.1 | Last Updated 9 Nov 2004
Article Copyright 2004 by Jay Nelson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid