简体   繁体   中英

Getting DLL Version from within the DLL Programmatically in C++ - Again

Tried out the answers from MSDN forum & the answer mentioned in thread - How do I programmatically get the version of a DLL or EXE file? - but I always get version number as "0.0.0.0"

Right Click my DLL -> Properties -> Details -> File Version has correct value. The EXE that loads my DLL does not version info - but not sure if that would matter.

My code is below (as part of the dll)->

   std::string moduleName     = "<full absolute path to DLL that this code is part of>";
   DWORD  zero                = 0;
   uint32 verInfoLen          = 0;
   BYTE   *verInfo            = NULL;
   VS_FIXEDFILEINFO *fileInfo = NULL;
   uint32 len                 = 0;

   /* Get the size of FileVersionInfo structure */
   verInfoLen = GetFileVersionInfoSize(moduleName.c_str(), &zero);
   if (verInfoLen == 0) {
      printf("GetFileVersionInfoSize() Failed!");
      return;
   }

   /* Get FileVersionInfo structure */
   verInfo = new BYTE[verInfoLen];
   if (!GetFileVersionInfo(moduleName.c_str(), zero, verInfoLen, verInfo)) {
      printf("GetFileVersionInfo Failed!");
      return;
   }

   /* Query for File version details. */
   if (!VerQueryValue(verInfo, "\\", (LPVOID *)&fileInfo, &len)) {
      printf("VerQueryValue Failed!");
      return;
   }

   /* None of the above func calls fail - but both printf below print all zeros */
   printf("Version is %d.%d.%d.%d",
                     (fileInfo->dwProductVersionMS >> 16) & 0xff,
                     (fileInfo->dwProductVersionMS >>  0) & 0xff,
                     (fileInfo->dwProductVersionLS >> 16) & 0xff,
                     (fileInfo->dwProductVersionLS >>  0) & 0xff);

   printf("Version is %d.%d.%d.%d",
                     HIWORD(fileInfo->dwProductVersionMS),
                     LOWORD(fileInfo->dwProductVersionMS),
                     HIWORD(fileInfo->dwProductVersionLS),
                     LOWORD(fileInfo->dwProductVersionLS));

Any help appreciated !

You are printing out the ProductVersion fields when you should be printing out the FileVersion fields instead:

printf("Version is %d.%d.%d.%d",
                 HIWORD(fileInfo->dwFileVersionMS),
                 LOWORD(fileInfo->dwFileVersionMS),
                 HIWORD(fileInfo->dwFileVersionLS),
                 LOWORD(fileInfo->dwFileVersionLS));

Also, your code is leaking the verInfo array if GetFileVersionInfo() or VerQueryValue() fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM