File version information
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:
-
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:
-
//------------------------------
-
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);
-
//-----------------
related things:
Author: Raz | On February 25th, 2007 | C/C++, C++ Builder, [ En ] | No Comments
Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Spammers: Beware of » my Dog! «




