Automatic Build Number Increasing Macro for VS.NET

Environment: VS.NET

After the emergence of .NET, many developers might spend much time getting accustomed to the new environment and tools. Especially, I think that it was a painful time for VB programmers because VB has changed a lot. Although I am a C++ programmer, VB's version of macros gave me a hard time. Most VC6 macros, which are written in VBScript, no longer work in the VS.NET. One of my favorite macros is an automatic build number increasing macro, but it also does not work. So, I revised my old macro so it can work with VS.NET.

Additionally, I got the old macro from the MS Web site (http://support.microsoft.com/default.aspx?scid=KB;en-us;q237870). Let's create a new macro! Just follow these steps:

  1. If you have created a new project(MFC or Win32...), open the resource file("ProjectName.rc") with a text editor.

  2. If your project is a MFC project, open another resource file in the "\res" folder("ProjectName.rc2").

    *If your project is a Win32App project, copy a "res" folder from any MFC project.

    Remove all files, except the "ProjectName.rc2" file, and rename it.

  3. Open the "ProjectName.rc2" file with a text editor. (We opened two resource files.)

  4. Find the following part in the "ProjectName.rc" file and cut it.
  5.    ////////////////////////////////////////////////////////
       //
       // Version
       //
       VS_VERSION_INFO VERSIONINFO
       FILEVERSION 1,0,0,1
       PRODUCTVERSION 1,0,0,1
       FILEFLAGSMASK 0x3fL
       .
       .
       .
          BLOCK "VarFileInfo"
           BEGIN
               VALUE "Translation", 0x409, 1200
           END
       END
    
    *If your project is Win32App project, then add "#include ""res\\ProjectName.rc2"" in the "ProjectName.rc", as shown below.
       .
       .
       .
       2 TEXTINCLUDE
       BEGIN
        "#include ""afxres.h""\r\n"
        "#include ""res\\ProjectName.rc2""  // <--- Add this line
        "\0"
       END
       .
       .
       .
    
  6. Paste the copied portion in the "ProjectName.rc2" file, and modify it as below.

       .
       .
       .
       ////////////////////////////////////////////////////////
       // Add manually edited resources here...
    
       ////////////////////////////////////////////////////////
       ////////////////////////////////////////////////////////
       //
       // Version
       //
       #include "VersionNo.h"       // <---- Add here
       VS_VERSION_INFO     VERSIONINFO
       FILEVERSION       1,0,0,1    // <---- Replace FILEVERSION
                                    //       with FILEVER
       PRODUCTVERSION    1,0,0,1    // <---- Replace
                                    //       PRODUCTVERSION
                                    //       with PRODUCTVER
       FILEFLAGSMASK 0x3fL
       .
       .
       .
            BEGIN
                VALUE "FileDescription", "MyProject Application"
                VALUE "FileVersion", "1.0.0.1"
                       // Replace "1.0.0.1" with STRFILEVER
                VALUE "InternalName", "MyProject"
                VALUE "LegalCopyright", "Copyright (C) 2003"
                VALUE "OriginalFilename", "MyProject.exe"
                VALUE "ProductName", " MyProjectApplication"
                VALUE "ProductVersion", "1.0.0.1"
                       // Replace "1.0.0.1" with STRPRODUCTVER
            END
       .
       .
       .
    
  7. Save the two files and close them.

  8. Create a VersionNo.h file in your project and paste the following to it.

       #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"
    
  9. In VS.NET, create a new macro (Tools-> Macros -> New Macro Project).

  10. When you make a new project, VS.NET displays the "Macro Explorer." Click "+" on the macro project that you generate, and it displays "Module1."

  11. Rename "Module1" to what you want to call it.

  12. Double-click on the renamed module; then, VS.NET launches the Macro IDE.

  13. Copy the following code and paste it in your module.

    Imports EnvDTE
    Imports System.Diagnostics
    
    Public Module BuildDone  '<--------------- Your module name
    
        Function GetProjectDir(ByVal FullName)
    
            Dim proj_path
            proj_path = Split(StrReverse(FullName), "\", -1, 1)
    
            Dim count
            count = UBound(proj_path)
    
            Dim full_path
            full_path = ""
            Dim i
    
            For i = 1 To count
                full_path = full_path & "\" & proj_path(i)
            Next
    
            GetProjectDir = StrReverse(full_path)
    
        End Function
    
        Sub ReplaceText(ByVal objSel As TextSelection, _
        ByVal count As Integer, ByVal incrementby As Integer, _
        ByVal Type As Integer)
    
            'selection represents the TextSelection object
            'count represents the position of the version number
            'to be incremented
            'incrementally represents a number that will be added
            'to the existing version number
    
            Dim strTemp As String
            Dim i
    
            strTemp = ""
            objSel.EndOfLine()
    
    
            If Type = 0 Then
                For i = 1 To count
                    If strTemp.StartsWith(",") = True Then
                        Exit For
                    Else
                        objSel.CharLeft(True, 1)
                        strTemp = objSel.Text
                    End If
                Next
                strTemp = strTemp.Remove(0, 1)
                strTemp = strTemp + incrementby
                objSel.Text = "," & strTemp
            Else
                For i = 1 To count
                    If strTemp.StartsWith("\") = True Then
                        Exit For
                    Else
                        objSel.CharLeft(True, 1)
                        strTemp = objSel.Text
                    End If
                Next
                strTemp = strTemp.Remove(0, 1)
                strTemp = strTemp.Remove(strTemp.Length - 1, 1)
                strTemp = strTemp + incrementby
                objSel.Text = "\" & strTemp & """"
            End If
    
        End Sub
        
        Dim WithEvents bldevents As BuildEvents
        Dim applicationObject As EnvDTE.DTE
    
        Sub BuildDoneEvents()
            Dim addInInstance As EnvDTE.AddIn
    
            applicationObject = CType(Application, EnvDTE.DTE)
            bldevents = CType(applicationObject.Events. _
                              BuildEvents, EnvDTE.BuildEvents)
        End Sub
    
        Private Sub bldevents_OnBuildDone(ByVal _
                              Scope As EnvDTE.vsBuildScope, _
                              ByVal Action As EnvDTE. _
                              vsBuildAction) Handles _
                              bldevents.OnBuildDone
            'This event will be triggered after every build
            'of a project
    
            'Obtain the full path of the active project
            Dim full_path
            full_path = GetProjectDir(DTE.ActiveDocument.Path)
    
            full_path = full_path & "versionno.h"
    
            'Open the VersionNo.h file
            Dim doc As Document
            DTE.ItemOperations.OpenFile(full_path)
    
            Dim objDoc As TextDocument
    
            'Obtain the TextSelection object
            objDoc = DTE.ActiveDocument.Object("TextDocument")
    
            Dim objSel As TextSelection = _
                          DTE.ActiveDocument.Selection
    
            objSel.StartOfDocument()
    
    
            'Increment the version information
            ReplaceText(objSel, 5, 1, 0)
            objSel.LineDown()
            objSel.StartOfLine()
            ReplaceText(objSel, 5, 1, 0)
            objSel.LineDown()
            objSel.StartOfLine()
            ReplaceText(objSel, 5, 1, 100)
            objSel.LineDown()
            objSel.StartOfLine()
            ReplaceText(objSel, 5, 1, 100)
    
            ActiveDocument.Save()
            ActiveDocument.Close()
        End Sub
    
    End Module
    
  14. Save this macro, and return to VS.NET. In the Macro Explorer, select your macro module and expand it. Then, you can find a module that you made in the previous step. Right-click it and choose "Run".

    [image:RunMacro.jpg]
  15. If every thing is okay, version numbers in the VersionNo.h file will be increased every time you compile your project.


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • Email is the most common communication vehicle used by organizations of all shapes and sizes. Among the billions of email messages sent every day are sensitive information, critical requests, and other essential business data. IT staff bear the burden of ensuring the confidentiality, integrity, and availability of the information contained within the communication. This white paper explores the email security landscape, an assessment of the threats organizations face,  and the building blocks of an effective …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds