http://support.microsoft.com/kb/237870
創建新項目時使用 MFC (EXE) 應用程序嚮導,並調用它我的項目。我的項目將 MyProject.rc 文件,其中包括 MyProject.rc2。.Rc2 文件用於用戶定義的資源。請按照下列步驟,以增加每次生成後我的項目的版本信息:
- 從.rc 文件中刪除版本資源並將其放置在.rc2 文件中:
- 在文本編輯器中打開 MyProject.rc 和 MyProject.rc2 (Res 文件夾中找到)。若要使用 Visual c + + 編輯器中,單擊文件菜單上的打開,並在 MyProject.rc 文件打開方式列表中選擇文本。
-
在 MyProject.rc 中找到的版本資源語句。它應如下所示:
/////////////////////////////////////////////////////////////////////// // // 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
- 剪切從 MyProject.rc 文件版本資源並將其粘貼到一則評語下面的 MyProject.rc2 文件"添加手動編輯此資源。有關資源中的字段中的每一個含義的詳細內容,請參閱幫助中的 VERSIONINFO 資源語句。
- 替換宏 FILEVER 和 PRODUCTVER 的文件和產品數據。同樣,替換宏 STRFILEVER 和 STRPRODUCTVER 的文件和產品字符串數據。
- 添加 # VS_VERSION_INFO 資源語句前立即包括VersionNo.h 。現在版本資源看起來:
/////////////////////////////////////////////////////////////////////// // // Version // #include "VersionNo.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
- 創建與您的項目位於同一目錄中名爲 VersionNo.h 的頭文件。此文件將包含以下語句,將使用在步驟 2 中的宏的定義:
#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"
現在,MyProject.rc 文件包含 MyProject.rc2,並且 MyProject.rc2 文件包含 VersionNo.h。 - 將使用 Visual Basic 腳本宏修改 VersionNo.h 文件的內容。宏描述控點的下方 Visual c + + BuildFinish 事件,因此它將不激發直到生成完成。每當調用此 VB 腳本代碼時,它首先按固定金額、
增加內部的頭文件的版本號,然後保存該文件,並將其關閉。在後續的生成過程中可執行文件中包含新的版本號。
要安裝並使用 VB 腳本代碼,請執行以下操作:- 打開現有的 DSM (宏觀) 文件,或在 Visual c + + 中創建一個新的 DSM 文件。要創建一個新文件,請單擊文件菜單上的新建、文件選項卡上選擇的宏文件、 爲其指定一個名稱,和單擊確定
-
粘貼下面的以下 VB 腳本代碼 (在 Visual c + + 中,無法安裝空 DSM 文件 ; 下一步解釋安裝):
Function GetProjectDir(FullName) 'VC++ doesn't provide any method for getting the path of the active project 'See the VB Script reference for more information on the VB Script functions 'used in this function 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(selection, count, incrementby) 'selection represents the TextSelection object 'count represents the position of the version number to be incremented 'incrementby represents a number that will be added to the existing version number selection.WordRight dsMove, count selection.WordRight dsExtend, 1 Dim str str = selection.Text str = str + incrementby selection.Text = str End Sub Sub Application_BuildFinish(numError, numWarning) 'This event will be triggered after every build of a project 'You can check numError and/or numWarning to determine if you want to continue 'If numError <> 0 Then 'exit sub 'Obtain the full path of the active project Dim full_path full_path = GetProjectDir(ActiveProject.FullName) full_path = full_path & "versionno.h" 'Open the VersionNo.h file Documents.Open full_path 'Obtain the TextSelection object Dim selection set selection = ActiveDocument.Selection selection.StartOfDocument 'Increment the version information ReplaceText selection, 9, 1 selection.LineDown selection.StartOfLine ReplaceText selection, 9, 1 selection.LineDown selection.StartOfLine ReplaceText selection, 10, 1 selection.LineDown selection.StartOfLine ReplaceText selection, 10, 1 ActiveDocument.Save ActiveDocument.Close End Sub
- 如果未安裝,請安裝 DSM 文件。要安裝,請在工具菜單上單擊自定義、 單擊加載項和宏文件選項卡、 瀏覽以選擇 DSM 文件中,並單擊關閉
- 從生成菜單中選擇生成 MyProject.exe 。生成完成後,打開 VersionNo.h 文件。它將包含以下語句:
#define FILEVER 1,0,0,2 #define PRODUCTVER 1,0,0,2 #define STRFILEVER "1, 0, 0, 2\0" #define STRPRODUCTVER "1, 0, 0, 2\0"