The Code Project View our sponsorsCodejock Software - Serious GUI CodeAdvertise on the CodeProject
Home >> Macros and Add-ins >> DevStudio Add-ins

Auto-Incrementing Build Numbers
By Navi Singh

Describes a way to automatically generate an application build number. 
 Beginner
 VC6, Win9x, NT4, MFC
 Posted 17 Feb 2000
 Updated 09 Mar 2000
Articles by this author
Send to a friend
Printer friendly version
Home Latest updates Submit your article About Us Advertise on the Code Project Contact us Discussion Forums
Navigation bar
25 users have rated this article. result:
3.76 out of 5.
  • Download demo project - 35 Kb
  • Updated: 09 Mar 2000. See below for updates.

    This code increments the version number on each build.

    After including the autobuild.dll as an Add-In for dev studio, the following happens. When you do a build, a file autobuild.h is created/updated in your project's folder.

    If you want to increment your version number with each build, set the flag INCREMENT_BUILD_NUM to true in the generated autobuild.h, AND make the file attribute for your project's .rc file to read-write.

    #ifndef __AUTOBUILD_H__
    #define __AUTOBUILD_H__
    //change the FALSE to TRUE for autoincrement of build number
    #define INCREMENT_BUILD_NUM TRUE
    #define BUILD_NUM 2723
    #endif //__AUTOBUILD_H__
    

    If the flag is true, the project's version number stored in the .rc file is incremented. If the flat is false, or the .rc file is read-only, the version number is not incremented. That is the extent of the change brought about by including the add-in in the project.

    In my project, I also use Manuel Laflamme's code from the CodeGuru.com site, to get the version number from the .rc file and display it in the help-about dialog box. Here is the code snippet that is used to get the version number within the code.

    dlgAbout.cFileVersion.Open(m_sExePath + m_sExeName);
    dlgAbout.m_sBuild = "Pro-JCL version: " +
    dlgAbout.cFileVersion.GetProductVersion();
    

    In the about box, I have a cstatic control that displays the version number.

    This approach has been a real convenience in my interactions with the QA group. Hope you find it useful.


    Update: 09 Mar 2000.

  • Download updated demo project - 61 Kb
  • MSDN recently provided a mechanism for incrementing the version number after each build. The key thing was that they slipped the version resource into the .rc2 file instead of the .rc file. I modified my addin to do the the same. i.e., follow their instructions for moving and modifying the version resource from the .rc to the .rc2 file. Then, add the add-in to your ide as before.

    The file AutoBuild.h will be generated by the developer studio add-in and will contain the following:

    #ifndef __AUTOBUILD_H__
    #define __AUTOBUILD_H__
    //change the FALSE to TRUE for autoincrement of build number
    #define INCREMENT_VERSION FALSE
    #define FILEVER        1,0,0,1
    #define PRODUCTVER     1,0,0,1
    #define STRFILEVER     "1, 0, 0, 1\0"
    #define STRPRODUCTVER  "1, 0, 0, 1\0"
    
    #endif //__AUTOBUILD_H__
    

    Include the generated file in your project and set the INCREMENT_VERSION flag to TRUE if you want to increment the version number with each build. The version will be incremented with each build. i.e., 1,0,0,1 will become 1,0,0,2 ... 1,0,0,3 ...

    By using FILEVER, PRODUCTVER, STRFILEVER, STRPRODUCTVER strings in place of the version numbers inside the version resource as the MSDN article demonstrates, we get rid of the irritating warning message from developer studio that its reloading the modified .rc file. That message motivated me to modify the code to use their approach.

    Below is the MSDN / VCDJ documentation http://support.microsoft.com/view/dev.asp?kb=237870 for moving the version resource from the .rc file to the .rc2 file and making the appropriate changes to the version number strings.

    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"
                VALUE "LegalCopyright", "Copyright (C) 1999\0"
                VALUE "OriginalFilename", "MyProject.EXE\0"
                VALUE "ProductName", "MyProject Application\0"
                VALUE "ProductVersion", "1, 0, 0, 1\0"
           END
        END
        BLOCK "VarFileInfo"
        BEGIN
            VALUE "Translation", 0x409, 1200
        END
    END
    

    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 AutoBuild.h immediately before the VS_VERSION_INFO resource statement. Now the version resource will look like:

    ///////////////////////////////////////////////////////////////////////
    //
    // Version
    //
    #include "AutoBuild.h"
    VS_VERSION_INFO VERSIONINFO
     FILEVERSION FILEVER
     PRODUCTVERSION PRODUCTVER
     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", STRFILEVER
                VALUE "InternalName", "MyProject\0"
                VALUE "LegalCopyright", "Copyright (C) 1997\0"
                VALUE "OriginalFilename", "MyProject.EXE\0"
                VALUE "ProductName", "MyProject Application\0"
                VALUE "ProductVersion", STRPRODUCTVER
            END
        END
        BLOCK "VarFileInfo"
        BEGIN
            VALUE "Translation", 0x409, 1200
        END
    END
    

    The file AutoBuild.h will be generated by the developer studio add-in and will contain the following:

    #ifndef __AUTOBUILD_H__
    #define __AUTOBUILD_H__
    //change the FALSE to TRUE for autoincrement of build number
    #define INCREMENT_VERSION FALSE
    #define FILEVER        1,0,0,1
    #define PRODUCTVER     1,0,0,1
    #define STRFILEVER     "1, 0, 0, 1\0"
    #define STRPRODUCTVER  "1, 0, 0, 1\0"
    
    #endif //__AUTOBUILD_H__
    

    [Top] Sign in to vote for this article:     PoorExcellent  

    View our sponsorsCodejock Software - Serious GUI CodeAdvertise on the CodeProject

    Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
     Keyword Filter
     View   Per page   Messages since
    New threadMessages 1 to 4 of 4 (Total: 4)[First] [Prev] [Next] [Last]
    Subject 
    Author 
    Date 
      A handy addition to this
     Wolfram Steinke 4:41 11 Jun 01 
      Making this work with 'Old' projects
     Wolfram Steinke 21:50 9 Jun 01 
      Incrementing the build number
    Unconfirmed/Anonymous posting Eugen Paval 9:37 15 Mar 00 
      Re: Incrementing the build number
    Unconfirmed/Anonymous posting Syver Enstad 19:40 30 Apr 00 
    Last Visit: 12:00 Friday 1st January, 1999[First] [Prev] [Next] [Last]
    Home >> Macros and Add-ins >> DevStudio Add-ins
    Advertise on The Code Project
    Article content copyright Navi Singh, 2000
    everything else © CodeProject, 1999-2001.