您的位置:首页 > 其它

如何增量 Visual c + + 中的每个版本后的版本信息

2011-04-21 13:57 162 查看
您可以编写程序来修改资源编译器 (.rc) 文件,而不是使用此处所述的步骤。但是,RC 文件是 Visual c + + 的控制之下。visual c + +
修改 RC 文件,在保存时,这可能会影响版本资源。本节中介绍的方法可应用于任何 Visual c + + 项目。此示例使用 Microsoft
基础类项目。

创建新的项目使用 MFC (EXE) Appwizard 并调用它 MyProject。MyProject 必须包括 MyProject.rc2 的 MyProject.rc
文件。.rc2 文件旨在为用户定义的资源。请按照下列步骤以递增 MyProject 的每个版本后的版本信息:

版本资源删除.rc 文件并将其放置在.rc2 文件中:

在文本编辑器中打开 MyProject.rc 和 MyProject.rc2 (在分辨率文件夹中找到)。若要用于 Visual c + +
编辑器在 文件 菜单上单击 打开,然后选择为 MyProject.rc 文件在 打开方式 列表中的
文本

在 $ MyProject.rc 中查找版本资源语句。它看起来应类似于:

[code]/////////////////////////////////////////////////////////////////////// 
// 
// 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

[/code]

从 MyProject.rc 文件剪切版本资源并将其粘贴到下面注释 MyProject.rc2
文件"的添加已手动编辑此处的资源"。有关在资源域中的每一个含义的信息,请参阅 VERSIONINFO 资源语句,在帮助中。

替换宏 FILEVER 和 PRODUCTVER FILEVERSION 和 PRODUCTVERSION 数据。同样,宏 STRFILEVER 和
STRPRODUCTVER 替换 FileVersion 和 ProductVersion 字符串数据。

添加一个 # VS_VERSION_INFO 资源语句前立即包括 VersionNo.h。现在看起来版本资源 (如:

[code]/////////////////////////////////////////////////////////////////////// 
// 
// 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

[/code]

创建您的项目所在的同一目录中名为 VersionNo.h 的头文件。此文件将包含下面的语句是第 2 步中使用的宏的定义:

[code]#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"

[/code]

注: 添加换行符和回车符返回最后一行的字符。

现在,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 文件 ; 下一步介绍安装):

[code]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

[/code]

注: 此代码是一个不受支持的示例。您可能会对其进行修改为您生成的方案。

如果尚未安装,请安装 DSM 文件。若要安装,单击 工具 菜单上的 自定义、 单击
加载项和宏文件 选项卡、 浏览以选择 DSM 文件,单击 关闭

生成 菜单中选择 生成 MyProject.exe。生成完成后,打开 VersionNo.h 文件。它将包含以下语句:

[code]#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"

[/code]

如果生成代码再次,在此版本信息包含在该可执行文件和版本信息就会增加。您可能会引入一些代码,在宏中前面所述,以防止递增的版本号,如果在生成产生错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: