Click here to Skip to main content
15,894,291 members
Articles / Programming Languages / Visual Basic

Increase Build Numbers in Visual C++ .NET using Macros

Rate me:
Please Sign up or sign in to vote.
4.41/5 (19 votes)
18 May 2004CPOL4 min read 225.2K   946   28   36
Implements a macro for Visual C++ .NET for automatic build number increments in VERSIONINFO definitions of project .RC files.

VERSIONINFO Sample Screenshot

Introduction

There are many solutions out there for automatic build number increment support. Many are meant for VC6. The ones I found for Visual C++ .NET are commonly using add-ins for the DevStudio. Usually, a good and very integrated method, they all have the disadvantage of inflexibility. They require some sort of separate header file with defines that have to be used in the project's resource file. And if you want to adopt the add-in's functionality somehow to fit your needs more closely, it often is not possible to get the source code of the add-in.

So, I tried to find a macro for VC.NET instead of add-ins, unfortunately without success. That's the reason why I decided to write my own macro and offer it to the public. You can simply change it according to your needs, or take it as a template sample for your own macro implementations.

Functionality

My macro implementation reacts on the DevStudio's event when a build starts. The macro scans resource files (.RC extension only) in all projects of the current solution. All files not having the extension .RC will be skipped.

In a resource file, the macro will search for all VERSIONINFO definitions, and inside there, it will increment the build numbers for all 4 statements FILEVERSION, PROJECTVERSION, "FileVersion" and "ProjectVersion". It will handle multiple language blocks, too (if present).

To disable parsing a .RC file, specify the following line of code somewhere in the .RC file:

#define Disable_IncBuildNrMacro

If you don't specify this line or if it is remarked out, then the .RC file will be parsed and build numbers will be incremented.

DevStudio language related notes:

At the start of a build process, the IncBuildNr macro will output information messages about success or failure of manipulating the .RC files or when .RC files have been skipped. The messages will be posted into the output-window's Build pane where the compiler inserts its own progress information, too. The macro is implemented for English versions of the DevStudio. If your DevStudio has a different language, it can be, that the macro does not find the correct output-window-pane. This can happen, because the macro simply opens a pane named "Build". If in your DevStudio, the output-window-pane for build progress information is named differently, it is recommended to change the pane name in the macro sources accordingly (in module EnvironmentEvents, in function BuildEvents_OnBuildBegin()).

Further description you can find in the macro sources themselves (when you open it in the DevStudio's Macro Editor).

Installation Instructions

First, go to the location of your Visual Studio macros. By default, this is Personal\Visual Studio Projects\VSMacros. In there, create a subdirectory named IncBuildNrMacro and copy the file IncBuildNrMacro.vsmacros in there.

Now, open the VC++ .NET DevStudio, open the Macro Explorer, right click on the root item "Macros", and select "Load Macro Project". Open IncBuildNrMacro.vsmacros. A warning appears informing you that the loaded macro contains event handling code. Select "Enable event handling code", and press OK. This is required, otherwise the macro would not react on build starts.

That's it. The next time you build a project, the IncBuildNr macro will be invoked automatically.

Uninstallation Instructions

In VC++ .NET DevStudio, go to the Macro Explorer, right click on "IncBuildNrMacro" and select "Unload Macro Project". You can delete the IncBuildNrMacro.vsmacros file now, if you like to.

Usage in your C++ project(s)

To query the VERSIONINFO definition contents in your application at runtime, use GetFileVersionInfo() and related functions.

Drawback

All .RC files in all Projects of the current Solution will be handled at once. Even if the user wants to build only one specific project within the current solution, the macro will increment build numbers of all the projects in the solution. I did not find a way (yet) to detect which projects are being compiled. Although this can be seen as a drawback, it can also be seen as an advantage: all the build numbers in the projects will be handled always at the same time. So, the build numbers remain accurate and equal over all projects.

But nevertheless, if you find a way to detect specific projects being built, let me know.

Further, whenever a project's resources have been opened in the DevStudio either in the Resource View (the tree view) or in resource editing windows such as the dialog editor, the icon editor etc., the macro first will close all these windows so that it can access the contents of the resource file. (Exception: the Resource View Tool window will stay present.) It may be annoying to see all the resource windows disappear every time a build process is started. But again, I did not find any other solution for this problem yet. Maybe you have a better idea. If so, please let me know.

History

  • 20 May 2004.

    This is the first version of IncBuildNrMacro.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Visual Studio.NET 2003 issue Pin
Kamziki20-Dec-05 23:28
Kamziki20-Dec-05 23:28 
GeneralGreat macro Pin
Mr Strange Code26-May-04 20:16
Mr Strange Code26-May-04 20:16 
GeneralRe: Great macro Pin
Roman Komary27-May-04 6:26
Roman Komary27-May-04 6:26 
GeneralRe: Great macro Pin
Mr Strange Code27-May-04 12:40
Mr Strange Code27-May-04 12:40 
GeneralTest and Debug. Pin
PeterHarrie26-May-04 3:21
PeterHarrie26-May-04 3:21 
GeneralRe: Test and Debug. Pin
Roman Komary26-May-04 4:38
Roman Komary26-May-04 4:38 
GeneralVisual Studio.NET 2003. Pin
PeterHarrie26-May-04 3:17
PeterHarrie26-May-04 3:17 
GeneralRe: Visual Studio.NET 2003. Pin
Roman Komary26-May-04 4:37
Roman Komary26-May-04 4:37 
[In here, I answer both of your questions, this one and the one about how to debug macros.]

Thanx for the info. I don't have VS.NET 2003 and also don't have access to it anyhow.
What a mess that microsoft seems having changed the triggering event.

But first, are you sure, you have event handling enabled for the macro when you have loaded it? With me, an asking dialog is opened by the Studio. Did you see this one, too, and did you reply by allow enabling event handling with the macro?

If you are not sure, or if the dialog didn't pop up, then you can (or should be able to) en/disable event handling in the macro by going into the macro's properties in the macro explorer.

Guess, you did find the macro explorer (since you were able to load the macro). Go there again, click on "IncBuildNrMacro" tree item. Then press Alt+Return to pop up the properties toolbox. (For an unknown reason, the tree item's popup menu does not contain a properties menu item).

In the prop toolbox, you see the props of the macro, and also the security setting, that should be set to "enable event handling code".

Ok, if this is the case and the event does still not occur, then VS.NET03 definitively has changed the event invokation procedure.

So I explain, how I did make the code for the event handling.
During this, I also tell you how to debug the macro.

First you have to open the macro editor. Go to the macro explorer, open the "IncBuildNrMacro" treeitem and double click on "IncBuildNr" subitem.
The macro editor opens.

In the editor, you can set breakpoints as usual. That's all about debugging. When a breakpoint is set, the debugger will be started only when you start one of the functions listed in the macro explorer tree.
(IncBuildNrMacro does not have listed such functions, except you can uncomment-out the test function, see later).

Ok, now to the events:
In the macro editor, double click to the "EnvironmentsEvents" tree item in the project explorer toolbox.
In there, you see the "BuildEvents_OnBuildBegin" which is the entry point called by the event handling of VS.NET.

I think, the "BuildEvents_OnBuildBegin" is the problem.

How did I add this call (the macro editor can create the outer body of this function automatically for you):
The macro source code window in the macro editor has 2 combo boxes. The first says "EnvironmentEvents". I did open this one and selected "BuildEvents". Then I went to the right combo box and selected "OnBuildBegin" and voila, the event handling function body has been inserted for me.
That's all. I just had to insert the call to my function.

Maybe this event does either not exist any more on VS.NET03 or it has been changed somehow. I don't know.
Maybe you can post the complete text of the "EnvironmentsEvents" code of a default sample macro (not IncBuildNrMacro) delivered with VS.NET03 so that I can have a look at it to see if something changed to VS.NET.

Debugging:
Unfortunately the macro debugger does not work when an event occurs and executes macro commands.
So for debugging, I added a (commented-out) macro test function.
Double click on "IncBuildNr" in the project explorer of the macro editor.
In the source code window, scroll down to the line with "Public Module IncBuildNr". On top of this section, you see the (commented) function "TestUpdVerInfoInAllSolnProjs".
Remove the comments and save it.
Then go back to the macro explorer toolbox and you will see the function listed in it's tree (it must not have any parameters to be listed there). When you call this function by double clicking, the macro debugger is started and breakpoints do work now.

Hope this did help.
And I hope, you can help me, too. I would be really glad.
GeneralRe: Visual Studio.NET 2003. Pin
ziofabri30-Nov-04 4:20
ziofabri30-Nov-04 4:20 

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.