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:
- If you have created a new project(MFC or Win32…), open the resource file(“ProjectName.rc”) with a text editor.
- 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.
- Open the “ProjectName.rc2” file with a text editor. (We opened two resource files.)
- Find the following part in the “ProjectName.rc” file and cut it.
- 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 . . .
- Save the two files and close them.
- 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"
- In VS.NET, create a new macro (Tools-> Macros -> New Macro Project).
- When you make a new project, VS.NET displays the “Macro Explorer.” Click “+” on the macro project that you generate, and it displays “Module1.”
- Rename “Module1” to what you want to call it.
- Double-click on the renamed module; then, VS.NET launches the Macro IDE.
- 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
- 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]
- If every thing is okay, version numbers in the VersionNo.h file will be increased every time you compile your project.
////////////////////////////////////////////////////////
//
// 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
.
.
.