#include "stdafx.h"
#include <windows.h>
#include <atlstr.h>
#pragma comment(lib, "version")
int _tmain(int argc, _TCHAR* argv[])
{
LPCTSTR lpszModuleName = _T("C:\\Windows\\notepad.exe");
// Get the version information size for allocate the buffer
DWORD dwHandle;
DWORD dwDataSize = ::GetFileVersionInfoSize((LPTSTR)lpszModuleName, &dwHandle);
if ( dwDataSize == 0 )
return FALSE;
// Allocate buffer and retrieve version information
LPBYTE lpVersionData = new BYTE[dwDataSize];
if (!::GetFileVersionInfo((LPTSTR)lpszModuleName, dwHandle, dwDataSize,
(void**)lpVersionData) )
{
delete[] lpVersionData;
return FALSE;
}
// Retrieve the first language and character-set identifier
UINT nQuerySize;
DWORD* pTransTable;
if (!::VerQueryValue(lpVersionData, _T("\\VarFileInfo\\Translation"),
(void **)&pTransTable, &nQuerySize) )
{
delete[] lpVersionData;
return FALSE;
}
// Swap the words to have lang-charset in the correct format
DWORD dwLangCharset = MAKELONG(HIWORD(pTransTable[0]), LOWORD(pTransTable[0]));
// Query version information value
LPVOID lpData;
CString strValue, strBlockName;
strBlockName.Format(_T("\\StringFileInfo\\%08lx\\%s"),
dwLangCharset, _T("FileVersion"));
if ( ::VerQueryValue((void **)lpVersionData, strBlockName.GetBuffer(0),
&lpData, &nQuerySize) )
strValue = (LPCTSTR)lpData;
strBlockName.ReleaseBuffer();
printf("%S 版本号:%S\n", lpszModuleName, strValue);
return 0;
}
工程代码点此下载。