您好,有没有办法在 C++ 项目的文件版本中创建自定义字段。这样我们就可以看到这些字段以及文件版本、公司名称等。我想创建诸如 revision = 1000 Customization = OEM1 之类的字段。
谢谢约翰
您可以向 .rc 文件中的版本信息添加额外字段。您无法添加数字字段,但可以添加本地化字符串字段。我从未尝试过使用 GUI 来做到这一点,但我知道您可以通过直接更改文件来做到这一点。
在 Visual C++ 中,右键单击 .rc 文件,然后单击“查看代码”。在其中的某个地方,您会发现一个以以下内容开头的部分:
BLOCK "StringFileInfo"
Run Code Online (Sandbox Code Playgroud)
该块可能只有一个子块:
BLOCK "0409904b0"
Run Code Online (Sandbox Code Playgroud)
该数字是 en_us 区域设置描述符的数字版本。该块包含多个 VALUE 条目,例如:
VALUE "FileVersion", "1, 0, 0, 0"
VALUE "OriginalFilename", "MyProjectName"
Run Code Online (Sandbox Code Playgroud)
您可以将任何想要的字段添加到此部分,它将显示在可执行文件的属性对话框的版本选项卡上。
如果您需要能够在运行时读取这些值,您可以使用 GetFileVersionInfo,如下所示:
wchar_t myModululeName[MAX_PATH];
GetModuleFileName(NULL,myModuleName,MAX_PATH);
DWORD dummy;
DWORD versionSize=GetFileVersionInfoSize(myModuleName,&dummy);
//I don't remember why I added extra space to these
void * versionInfo=malloc(versionSize+10);
GetFileVersionInfo(myModuleName,0,versionSize+1,versionInfo);
//This part is optional
//The VS_FIXEDFILEINFO contains information from the non-localized parts of
//the "StringFileInfo" block in the .rc file
VS_FIXEDFILEINFO * fixedFileInfo;
UINT fixedFileSize;
VerQueryValue(versionInfo,L"\\",(void **)(&fixedFileInfo),&fixedFileSize);
//This will retrieve the local codes that are defined in the StringFileInfo block
WORD * translationTable;
UINT translationSize;
VerQueryValue(verionInfo,L"\\VarFileInfo\\Translation",(void **)(&translationTable),&translationTableSize);
//This always uses the first locale, you could examine translationTable
//if you need to for other codes
wchar_t mySpecialQuery[128];
sprintf_s(mySpecialQuery,L"\\StringFileInfo\\%04x%04x\\MySpecialVersionInfo",translationTable[0],translationTable[1]);
wchar_t * mySpecialValue;
UINT mySpecialValueSize;
VerQueryValue(versionInfo,mySpecialQuery,(void **)(&mySpecialValue),&mySpecialValueSize);
//you can now do whatever you need to do with mySpecialValue, including using _wtoi()
//and query for more values
free(versionInfo);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2092 次 |
| 最近记录: |