Tuesday, March 01, 2011

How to increment version information after each build in Visual C++

You can write a program to modify the resource compiler (.rc) file instead of using the steps described here. However, the RC file is under the control of Visual C++. Visual C++ modifies the RC file while saving, and this may affect the version resource. The approach described in this section can be applied to any Visual C++ project. This example uses a Microsoft Foundation Classes project.
Create a new project using the MFC (EXE) Appwizard and call it MyProject. MyProject will have a MyProject.rc file, which includes MyProject.rc2. The .rc2 file is meant for user-defined resources. Follow these steps to increment MyProject's version information after each build:
Remove the version resource from the .rc file and place it in the .rc2 file:
Open both MyProject.rc and MyProject.rc2 (found in the Res folder), in a text editor. To use the Visual C++ editor, click Open on the File menu and select Text in the Open As list for the MyProject.rc file.
Find the version resource statements in MyProject.rc. It should look something like:

///////////////////////////////////////////////////////////////////////
//
// Version
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
   BLOCK "StringFileInfo"
   BEGIN
       BLOCK "040904b0"
       BEGIN
           VALUE "Comments", "Sample Application\0"
           VALUE "CompanyName", "Microsoft Corp.\0"
           VALUE "FileDescription", "MyProject MFC Application\0"
           VALUE "FileVersion", "1, 0, 0, 1\0"
           VALUE "InternalName", "MyProject\0"

Cut the version resource from the MyProject.rc file and paste it into the MyProject.rc2 file below the comment "Add manually edited resources here." For information about what each one of the fields in the resource means, see the VERSIONINFO resource statement in Help.
Replace the FILEVERSION and PRODUCTVERSION data with macros FILEVER and PRODUCTVER. Similarly, replace the FileVersion and ProductVersion string data with the macros STRFILEVER and STRPRODUCTVER.
Add a #include VersionNo.h immediately before the VS_VERSION_INFO resource statement. Now the version resource will look like:

Read more: MS Support