Click here to Skip to main content
Click here to Skip to main content

An Automatic Build Incrementer for VC6

By , 11 Jul 2000
 
<!-- Download Links -->
  • Download source files - 2 Kb
  • <!-- Main HTML starts here -->

    Introduction

    Several weeks ago, I started trying to find a way to automatically increment build numbers on each compile. I found several articles on the subject and some source code but none of it did exactly what I wanted it to do, so I decided to write my own incrementer.

    Add the code to an existing macro file or, if you don't have an existing macro file, create one and add one dummy macro so you can access the code snippet. Application_BeforeBuildStart() is a Visual Studio defined event handler and although the code is in a macro file, when you select the Tools|Macro menu item, you won't see it, so you need at least a dummy macro in the file to access the source code.

    Note that these code snippets have been subjected to limited testing and that you use them at your own risk. They work correctly in the test environment, but you assume all risk for their use. I will not assume any liability whatsoever for their failure to work correctly in your environment.

    Please e-mail any comments, suggestions for improvement, problems, etc. to me.

    Sub Application_BeforeBuildStart()
       ' written 7/11/00 by Curt Blackmon
       ' e-mail any comments, corrections, etc. to curtblackmon@home.com
       ' updated versions will be at www.ccbcon.com on the Code Snippets page
       ' invoked automatically before build starts
       ' will open the project's *.rc file as text
       ' will search for the version info
       ' and will increment the build number
    
       dim oDoc
       dim sBuild
       dim sRCFile
       dim sOne
       dim sTwo
       dim iSelect
       dim lLineNbr
       dim bFound
    
       'get the name of the active project's .rc file
       sRCFile = application.activeproject & ".rc"
    
       'open *.rc file as text
       set oDoc = Documents.Open(sRCFile, "Text")
    
       'position to the correct section of the file
       oDoc.Selection.FindText " FILEVERSION", dsMatchCase
       'save the line number for the next search
       lLineNbr = oDoc.Selection.CurrentLine
    
       'use a regular expression search string for the first version search
       sOne = "[0-9]+,[0-9]+,[0-9]+,"
    
       'find the first string
       oDoc.Selection.FindText sOne, dsMatchRegExp
    
       if oDoc.Selection.CurrentLine = lLineNbr then
          'convert the regular expression to an absolute search string
          sOne = oDoc.Selection
          'build an absolute search string for the strings with embedded spaces
          sTwo = Replace(sOne, ",", ", ")
          'move to the build number
          oDoc.Selection.CharRight
          'select the build number
          oDoc.Selection.WordRight dsExtend
          'increment the build number
          sBuild = oDoc.Selection + 1
          'replace the old build number with the new one
          oDoc.Selection = sBuild
       else 'something went wrong
          msgbox "Version number 1 not found. Closing without changes."
          oDoc.Close dsSaveChangesNo
          set oDoc = nothing
          exit sub
       end if
       
       'now change the other 3 occurences of the build number
       for iSelect = 2 to 4
          if iSelect = 2 then
             bFound = oDoc.Selection.FindText(sOne)
          else
             bFound = oDoc.Selection.FindText(sTwo)
          end if
          oDoc.Selection.CharRight
          oDoc.Selection.WordRight dsExtend
          oDoc.Selection = sBuild
          if bFound = False then
             msgbox "Version number " & iSelect & " not found. Closing without changes."
             oDoc.Close dsSaveChangesNo
             set oDoc = nothing
             exit sub
          end if
       next
    
       'close and save *.rc
       oDoc.Close dsSaveChangesYes
       set oDoc = nothing
    End Sub
    


    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

    About the Author

    Curt Blackmon
    Web Developer
    United States United States
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    Generalanother improvementmembergok16-Apr-04 12:10 
    Great macro. Thanks!
    Some applications are called from inside other frames (dll). To get dll version I'm using extra string in string table called "IDS_FILEVERSION" synchronizing with Version section like that:
     
    ...
    'replace the old build number with the new one
    oDoc.Selection = sBuild
     
    '-----------------------------------------------------------
    'gok: replace in string table as well
    oDoc.Selection.FindText "IDS_FILEVERSION", dsMatchCase
    oDoc.Selection.WordRight dsMove, 8 'skip spaces, quote, 3 numbers
    oDoc.Selection.WordRight dsExtend, 1 'seat on build number
    oDoc.Selection = sBuild
    '-----------------------------------------------------------
    ...
     
    Wink | ;)

     
    Gennady Khokhorin
    Software Developer
    GeneralA small problemmemberCharles Sicotte25-Dec-00 11:13 
    The macro works, except for the fact that each time that I change something in my resource file or that I simply open the ResourceView and build after that, I get the message:
     
    "This file is open for resource editing. Continuing will close the resource editor. OK CANCEL"
     
    Thanks in advance.
    GeneralNice macro, but...sussSteve18-Jul-00 3:32 
    The macro looks pretty cool, but there is one problem (it happens with all other build incrementers):
     
    If you're using SourceSafe, you will need to checkout/in the RC file to make the changes.
     
    Any ideas
    GeneralRe: Nice macro, but...sussArchieSEB18-Jul-00 4:09 
    Hi,
     
    Go to your MSDN and search for the technical article:
    Visual SourceSafe 6.0 Automation
     
    You can control VSS by automation.
     
    PS: the example included in this article is a VSS entirely rewrite in Visual Basic that used VSS by automation. The guy who wrote it was a maniac of VSS :
    GeneralRe: Nice macro, but...memberRubke16-Jul-01 2:08 
    i did have the same problem, any solutions ?
    GeneralAdditional Instructions for VS macro newbiessussRicky Faulstich17-Jul-00 15:10 
    Works excellently. Here's some additional instructions, since it didn't work "out of the box" for me (because I really have no VS macro experience. This is a no-brainer for macro-jockies out there.)
     
    1. Make sure you have a resource file with a version resource in the first place. I originally tried it on a console-app service without a resource file and had to add one myself. I called it .rc, added it to the "Resources" folder in the project's file tab and all was well.
     
    2. If the macro crashes, like it did for me when I didn't have a resource file (#1), the macro file itself gets disabled. Make sure you go back into Tools | Macros and re-enable it
    GeneralWorks fine, I tried some of the others , this one seems easiestsussJCardinal17-Jul-00 11:23 
    Not sure why it was rated so low by the other guys, but I used it as is and it worked fine. I agree with the person that wanted to turn it off for debug builds, easy enough to change though.
     
    thank you
    GeneralDebug buildsussJens Kreiensiek16-Jul-00 21:06 
    Hi,
     
    I tried your piece of code and it worked fine.
    Since I don't want to count my debug builds, I added this
    line as the first one to your code:
     
    if instr(1, application.activeconfiguration, "debug", 1) then exit sub
     
    It doesn't fit all circumstances (e.g. own configurations which doesn't contain the word "debug" in the title), but works fine for me...
     
    bye,
    Jen
    GeneralRe: Debug buildmemberEdgar Rocha1-Oct-03 15:07 
    Do you know what is the same condition in visual studio.net?
     
    I have made some changes like use rc2 instead of rc and make it work in visual studio.net, but I could not find a way to get if I'm on debug or release configuration.
     
    Thanks
     
    Edgar
    GeneralRe: Debug buildmemberEdgar Rocha1-Oct-03 17:03 
    I had this idea after read your article. I don't know if it is correct way to do this, but it worked fine until now:Big Grin | :-D
     
        Function GetActiveProjectNumber() As Integer
     
            Dim activePrj As String
            Dim CurrentProject As Project
     
            activePrj = DTE.Solution.Properties.Item("StartupProject").Value
            For j = 1 To DTE.Solution.Projects.Count
                CurrentProject = DTE.Solution.Projects.Item(j)
                If CurrentProject.Name = activePrj Then
                    Return j
                End If
            Next
     
            Return 0
     
        End Function
    
     
    Then I used:
     
            
    Dim actPrjN
    actPrjN = GetActiveProjectNumber()
     
    mode = DTE.Solution.Item(actPrjN).ConfigurationManager.ActiveConfiguration.ConfigurationName.ToString
     
    If InStr(UCase(mode), "DEBUG") Then
        Exit Sub
    End If
    
     

    Edgar
    GeneralBug and fix!sussFredrik Tonn13-Jul-00 7:33 
    I tried your solution and it did not work as it should. The IDE complained about the following line:
     

    'get the name of the active project's .rc file
    sRCFile = application.activeproject & ".rc"
     

    I got it to work when I changed it to:
    'get the name of the active project's .rc file
    sRCFile = application.activeproject.fullname
    sRCFile = Left( sRCFile, Len(sRCFile) - 3 )
    sRCFile = sRCFile & "rc"
     
    // Fredrik
    GeneralRe: Bug and fix!sussCurt Blackmon13-Jul-00 11:51 
    I can't duplicate the problem you had, but in any case your changes improve the code since your changes return the path.
     
    It'll take me a couple of days to do since I'm rebuilding my development system to dual-boot Win2K Professional and WinME, but I'll make the changes, check them out and post a corrected ZIP source file on my web site, www.ccbcon.com. It'll be on the Code Snippets page.
     
    Curt Blackmon
    GeneralRe: Bug and fix!sussJames27-Oct-00 1:44 
    I found the same problem. It happened when I opened a text or source file that was not in the same directory / on the same drive as the project file before running the macro. I guess VC6 remembered this as the default directory, and couldn't find the .rc file. Great macro by the way
    GeneralRe: Bug and fix!sussflo027-Jan-03 22:25 

    I also had this problem and fixed it by gettin "fullname" and it work perfect
    thanx
     
    but to make 100% sure that the macro does not crash if the resource file was not found, I added the following lines:
     
    'check if project *.rc exists
    Set fso = CreateObject("Scripting.FileSystemObject")
    If (fso.FileExists(sRCFile)) then
    'open *.rc file as text
    set oDoc = Documents.Open(sRCFile, "Text")
    else
    'do nothing (or add code here to search *.rc file etc.)
    exit sub
    end if

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

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.6.130617.1 | Last Updated 12 Jul 2000
    Article Copyright 2000 by Curt Blackmon
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid