Q: How can I get file version information from my own exe/dll or any other file?
A: The VerQueryValue function retrieves specified version information from the specified version-information resource. To retrieve the appropriate resource, before you call VerQueryValue, you must first call the GetFileVersionInfoSize function, and then the GetFileVersionInfo function.
Here is a funtion that will extract version number vrom your own exe if ModulePath doesn’t point to a file path:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | bool GetFileVersionInformation(char *Result,char *ModulePath) { LPVOID lpStr1 = NULL; LPVOID lpStr2 = NULL; WORD* wTmp; DWORD dwHandlev = NULL; UINT nRet; UINT dwLength; char sFileName[1024]={0}; char sTmp[1024]={0}; char *sInfo; LPVOID* pVersionInfo; bool Success=false; if (Result==NULL) return Success; if (ModulePath==NULL) GetModuleFileName(NULL,sFileName,1024); else strcpy(sFileName,ModulePath); DWORD dwInfoSize = GetFileVersionInfoSize((char*)(LPCTSTR)sFileName, &dwHandlev); if (dwInfoSize) { pVersionInfo = new LPVOID[dwInfoSize]; nRet = GetFileVersionInfo((char*)(LPCTSTR)sFileName, dwHandlev, dwInfoSize, pVersionInfo); if(nRet) { nRet = VerQueryValue(pVersionInfo, "\\VarFileInfo\\Translation", &lpStr1, &dwLength); if(nRet) { wTmp = (WORD*)lpStr1; sprintf(sTmp,"\\StringFileInfo\\%04x%04x\\FileVersion", *wTmp, *(wTmp + 1)); nRet = VerQueryValue(pVersionInfo, sTmp, &lpStr2, &dwLength); if(nRet) { sInfo = (char*)lpStr2; strcpy(Result,sInfo); Success=true; } } } delete[] pVersionInfo; } return Success; } |
Using it is simple:
1 2 3 4 5 6 7 8 9 10 | //------------------------------ char bf[2556]; //get your exe version: GetFileVersionInformation(bf,NULL); MessageBox ( 0,bf,"",0); //get a file version: GetFileVersionInformation(bf,"c:\\MyFolder\\application.exe"); MessageBox ( 0,bf,"",0); //----------------- |
Programmers should be trusted. If your brain surgeon told you the operation you need takes five hours, would you pressure him to do it in three? 