WIN32 API 获取文件版本信息
阅读原文时间:2023年07月08日阅读:1

CString strVersion;
CString strPath(_T("xxxxxxxx.exe"));
// 读文件信息
DWORD dwVerHnd = 0;
DWORD dwVerInfoSize = ::GetFileVersionInfoSize(strPath, &dwVerHnd);

if (dwVerInfoSize)
{
// If we were able to get the information, process it:
HANDLE hMem;
LPVOID lpvMem;
unsigned int uInfoSize = 0;
VS_FIXEDFILEINFO * pFileInfo;

hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpvMem = ::GlobalLock(hMem);
::GetFileVersionInfo(strPath, dwVerHnd, dwVerInfoSize, lpvMem);

struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;

// Read the list of languages and code pages.
UINT cbTranslate = sizeof(LANGANDCODEPAGE);
VerQueryValue(lpvMem,
TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate);

CString strSubBlock;
strSubBlock.Format(_T("\\StringFileInfo\\%04x%04x\\FileDescription"),
lpTranslate->wLanguage, lpTranslate->wCodePage);

char *buffer = NULL;
UINT nSize = 0;
VerQueryValue(lpvMem, strSubBlock, (LPVOID*)&buffer, &nSize);

::GlobalUnlock(hMem);
::GlobalFree(hMem);
}