貌似用以下2个方法可以取得当前工程的版本信息
1.代码如下所示:
#pragma comment(lib, "Version.lib ")
TCHAR szFullPath[MAX_PATH];
DWORD dwVerInfoSize = 0;
DWORD dwVerHnd;
VS_FIXEDFILEINFO * pFileInfo;
GetModuleFileName(NULL, szFullPath, sizeof(szFullPath));
dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
if (dwVerInfoSize)
{
// If we were able to get the information, process it:
HANDLE hMem;
LPVOID lpvMem;
unsigned int uInfoSize = 0;
hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpvMem = GlobalLock(hMem);
GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem);
::VerQueryValue(lpvMem, (LPTSTR)_T( "\\ "), (void**)&pFileInfo, &uInfoSize);
WORD m_nProdVersion[4];
// Product version from the FILEVERSION of the version info resource
m_nProdVersion[0] = HIWORD(pFileInfo-> dwProductVersionMS);
m_nProdVersion[1] = LOWORD(pFileInfo-> dwProductVersionMS);
m_nProdVersion[2] = HIWORD(pFileInfo-> dwProductVersionLS);
m_nProdVersion[3] = LOWORD(pFileInfo-> dwProductVersionLS);
CString strVersion ;
strVersion.Format(_T( "The file 's version : %d.%d.%d.%d "),m_nProdVersion[0],
m_nProdVersion[1],m_nProdVersion[2],m_nProdVersion[3]);
GlobalUnlock(hMem);
GlobalFree(hMem);
AfxMessageBox(strVersion);
}
2.如果对于像AxtiveX或者OCX的版本取得,如果使用上面的办法,则取得很有可能取得是当前使用的IE
的版本信息。所以这个时候可以采用下面的方法,但是需要注意下面GetModuleHandle函数的参数就应该是
使用的DLL或者OCX的名字。
HMODULE hModule;
HRSRC hRsrc;
HGLOBAL hGlobal;
DWORD ver1, ver2, ver3, ver4;
struct VS_VERSIONINFO {
WORD wLength;
WORD wValueLength;
WORD wType;
WCHAR szKey[16];
WORD Padding1[1];
VS_FIXEDFILEINFO Value;
// WORD Padding2[];
// WORD Children[];
} *pData;
ver1 = ver2 = ver3 = ver4 = 0;
hModule = GetModuleHandle("XXXXX.ocx");
if ((hRsrc = FindResource(hModule,(LPCTSTR)VS_VERSION_INFO,RT_VERSION))==NULL) {
MessageBox(NULL,"FindResource!","ver",0);
return ;
}
if ((hGlobal = LoadResource(hModule,hRsrc))==NULL) {
MessageBox(NULL,"LoadResource!","ver",0);
return ;
}
pData = (VS_VERSIONINFO*)LockResource(hGlobal);
ver1 = pData->Value.dwFileVersionMS>>16;
ver2 = pData->Value.dwFileVersionMS&0xffff;
ver3 = pData->Value.dwFileVersionLS>>16;
ver4 = pData->Value.dwFileVersionLS&0xffff;
TCHAR szVersion[MAX_LOADSTRING];
wsprintf(szVersion,"Version %d.%d.%d.%d",ver1,ver2,ver3,ver4);