Raz-Soft » Blog Archive » File version information Buy Cheap Software
Rebuilding my home… »
« Bulina rosie si pe carti?

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:

C++:
  1. bool GetFileVersionInformation(char *Result,char *ModulePath)
  2. {
  3.   LPVOID lpStr1 = NULL;
  4.   LPVOID lpStr2 = NULL;
  5.   WORD* wTmp;
  6.   DWORD dwHandlev = NULL;
  7.   UINT nRet;
  8.   UINT dwLength;
  9.   char sFileName[1024]={0};
  10.   char sTmp[1024]={0};
  11.   char *sInfo;
  12.   LPVOID* pVersionInfo;
  13.  
  14.   bool Success=false;
  15.   if (Result==NULL) return Success;
  16.  
  17.   if (ModulePath==NULL)
  18.     GetModuleFileName(NULL,sFileName,1024);
  19.   else
  20.     strcpy(sFileName,ModulePath);
  21.  
  22.   DWORD dwInfoSize = GetFileVersionInfoSize((char*)(LPCTSTR)sFileName, &dwHandlev);
  23.   if (dwInfoSize)
  24.   {
  25.       pVersionInfo = new LPVOID[dwInfoSize];
  26.       nRet = GetFileVersionInfo((char*)(LPCTSTR)sFileName, dwHandlev, dwInfoSize, pVersionInfo);
  27.     if(nRet)
  28.      {
  29.       nRet = VerQueryValue(pVersionInfo, "\\VarFileInfo\\Translation", &lpStr1, &dwLength);
  30.       if(nRet)
  31.        {
  32.          wTmp = (WORD*)lpStr1;
  33.          sprintf(sTmp,"\\StringFileInfo\\%04x%04x\\FileVersion", *wTmp, *(wTmp + 1));
  34.          nRet = VerQueryValue(pVersionInfo, sTmp, &lpStr2, &dwLength);
  35.          if(nRet)
  36.            {
  37.              sInfo = (char*)lpStr2;
  38.              strcpy(Result,sInfo);
  39.              Success=true;
  40.            }
  41.         }
  42.       }
  43.  
  44.       delete[] pVersionInfo;
  45.    }
  46.  
  47.   return Success;
  48. }

Using it is simple:

C++:
  1. //------------------------------
  2. char bf[2556];
  3. //get your exe version:
  4. GetFileVersionInformation(bf,NULL);
  5. MessageBox ( 0,bf,"",0);
  6.  
  7. //get a file version:
  8. GetFileVersionInformation(bf,"c:\\MyFolder\\application.exe");
  9. MessageBox ( 0,bf,"",0);
  10. //-----------------

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.

No comments yet

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions