我在声明的资源中有版本信息:
100 VERSIONINFO
FILEVERSION 1,0,0,2
PRODUCTVERSION 1,0,0,2
FILEOS VOS_NT
FILETYPE VFT_APP
{
BLOCK "StringFileInfo"
{
BLOCK "000004b0"
{
VALUE "FileDescription", "My application"
VALUE "FileVersion", "1.0.0.2"
VALUE "InternalName", "app.exe"
VALUE "LegalCopyright", "Copyright © 2012 by David."
VALUE "OriginalFilename", "app.exe"
VALUE "ProductName", "app"
VALUE "ProductVersion", "1.0.0.2"
VALUE "Assembly Version", "1.0.0.2"
}
}
BLOCK "VarFileInfo"
{
VALUE "Translation", 0x0000 0x04B0
}
}
我通过这种方式获取版本信息:
HRSRC hResInfo;
HGLOBAL hResData;
LPCVOID pRes;
UINT uLen;
VS_FIXEDFILEINFO *lpFfi;
hResInfo = FindResource(hInst, MAKEINTRESOURCE(100), RT_VERSION);
MessageBox(0, "FindResource", 0,0);
hResData = LoadResource(hInst, hResInfo);
MessageBox(0, "LoadResource", 0,0);
pRes = LockResource(hResData);
MessageBox(0, "LockResource", 0,0);
VerQueryValue(pRes, "\\" ,(LPVOID*)&lpFfi, &uLen);
MessageBox(0, "VerQueryValue", 0,0);
FreeResource(hResData);
DWORD dwFileVersionMS = lpFfi->dwFileVersionMS;
DWORD dwFileVersionLS = lpFfi->dwFileVersionLS;
DWORD dwLeftMost = HIWORD(dwFileVersionMS);
DWORD dwSecondLeft = LOWORD(dwFileVersionMS);
DWORD dwSecondRight = HIWORD(dwFileVersionLS);
DWORD dwRightMost = LOWORD(dwFileVersionLS);
在函数VerQueryValue中是一个错误,因为程序中断(不显示带有文本“VerQueryValue”的MessageBox),并且Visual C++向我显示了以下消息:
如何修复此代码?
问候,大卫
最佳答案
VerQueryValue()
无法直接从原始资源访问版本信息。您必须在内存中复制资源,然后将该内存传递给VerQueryValue()
。原因是因为VerQueryValue()
设计为可与GetFileVersionInfo()
一起使用,这需要用户分配的可写内存块并在该内存中执行某些修复。访问VS_FIXEDFILEINFO
结构不需要修复程序,但是内存块仍必须可写。您不能将原始资源直接传递给VerQueryValue()
,因为它是只读存储器。
尝试以下方法:
HRSRC hResInfo;
DWORD dwSize;
HGLOBAL hResData;
LPVOID pRes, pResCopy;
UINT uLen;
VS_FIXEDFILEINFO *lpFfi;
hResInfo = FindResource(hInst, MAKEINTRESOURCE(100), RT_VERSION);
dwSize = SizeofResource(hInst, hResInfo);
hResData = LoadResource(hInst, hResInfo);
pRes = LockResource(hResData);
pResCopy = LocalAlloc(LMEM_FIXED, dwSize);
CopyMemory(pResCopy, pRes, dwSize);
FreeResource(hResData);
VerQueryValue(pResCopy, TEXT("\\"), (LPVOID*)&lpFfi, &uLen);
DWORD dwFileVersionMS = lpFfi->dwFileVersionMS;
DWORD dwFileVersionLS = lpFfi->dwFileVersionLS;
DWORD dwLeftMost = HIWORD(dwFileVersionMS);
DWORD dwSecondLeft = LOWORD(dwFileVersionMS);
DWORD dwSecondRight = HIWORD(dwFileVersionLS);
DWORD dwRightMost = LOWORD(dwFileVersionLS);
LocalFree(pResCopy);
更新:仅在您访问
VS_FIXEDFILEINFO
结构时才有效。如果需要访问任何其他值,则必须使用GetFileVersionInfo()
。 Per Raymond Chen的博客:The first parameter to VerQueryValue really must be a buffer you obtained from GetFileVersionInfo