Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read from a version resource in Visual C++

I have a version resource in my resources in a C++ project which contains version number, copyright and build details. Is there an easy way to access this at run-time to populate my help/about dialog as I am currently maintaining seperate const values of this information. Ideally, the solution should work for Windows/CE mobile and earlier versions of Visual C++ (6.0 upwards).

like image 969
SmacL Avatar asked Nov 25 '08 07:11

SmacL


People also ask

How do I open resource view?

Go to menu View > Other Windows > Resource View. If the Resource View window isn't the top-most window, select the Resource View tab to bring it to the top. From Resource View, expand the folder for the project that contains resources you want to view.

What are resource files Visual Studio?

When you add a resource file to your Visual Studio project, Visual Studio creates a . resx file in the project directory. Visual Studio provides resource editors that enable you to add strings, images, and binary objects.


2 Answers

This is an edited version of my original answer.

bool GetProductAndVersion(CStringA & strProductName, CStringA & strProductVersion) {     // get the filename of the executable containing the version resource     TCHAR szFilename[MAX_PATH + 1] = {0};     if (GetModuleFileName(NULL, szFilename, MAX_PATH) == 0)     {         TRACE("GetModuleFileName failed with error %d\n", GetLastError());         return false;     }      // allocate a block of memory for the version info     DWORD dummy;     DWORD dwSize = GetFileVersionInfoSize(szFilename, &dummy);     if (dwSize == 0)     {         TRACE("GetFileVersionInfoSize failed with error %d\n", GetLastError());         return false;     }     std::vector<BYTE> data(dwSize);      // load the version info     if (!GetFileVersionInfo(szFilename, NULL, dwSize, &data[0]))     {         TRACE("GetFileVersionInfo failed with error %d\n", GetLastError());         return false;     }      // get the name and version strings     LPVOID pvProductName = NULL;     unsigned int iProductNameLen = 0;     LPVOID pvProductVersion = NULL;     unsigned int iProductVersionLen = 0;      // replace "040904e4" with the language ID of your resources     if (!VerQueryValue(&data[0], _T("\\StringFileInfo\\040904e4\\ProductName"), &pvProductName, &iProductNameLen) ||         !VerQueryValue(&data[0], _T("\\StringFileInfo\\040904e4\\ProductVersion"), &pvProductVersion, &iProductVersionLen))     {         TRACE("Can't obtain ProductName and ProductVersion from resources\n");         return false;     }      strProductName.SetString((LPCSTR)pvProductName, iProductNameLen);     strProductVersion.SetString((LPCSTR)pvProductVersion, iProductVersionLen);      return true; } 
like image 159
Mark Ransom Avatar answered Sep 19 '22 15:09

Mark Ransom


To get a language independent result to Mark's answer change :

   // replace "040904e4" with the language ID of your resources     !VerQueryValue(&data[0], _T("\\StringFileInfo\\040904e4\\ProductVersion"), &pvProductVersion, &iProductVersionLen)) {     TRACE("Can't obtain ProductName and ProductVersion from resources\n");     return false; } 

To

UINT                uiVerLen = 0; VS_FIXEDFILEINFO*   pFixedInfo = 0;     // pointer to fixed file info structure // get the fixed file info (language-independent)  if(VerQueryValue(&data[0], TEXT("\\"), (void**)&pFixedInfo, (UINT *)&uiVerLen) == 0) {     return false; }   strProductVersion.Format("%u.%u.%u.%u",      HIWORD (pFixedInfo->dwProductVersionMS),     LOWORD (pFixedInfo->dwProductVersionMS),     HIWORD (pFixedInfo->dwProductVersionLS),     LOWORD (pFixedInfo->dwProductVersionLS)); 
like image 26
EdM Avatar answered Sep 18 '22 15:09

EdM