Versionsinformationen einer Datei lesen

  • Verwendung:
VerInfo * pVer = new VerInfo("C:\\WINDOWS\\notepad.exe");

for (int i = 0; i < 10; i++)
{
    Memo1 -> Lines -> Add(pVer -> GetVerInfo(ilCount));
}

if (pVer != NULL) { delete pVer; pVer = NULL; }
  • *.h
#ifndef versionsinfoH
#define versionsinfoH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <vcl.h>
//----------------------------------------------------------------
// Konstanten für die Verwendung als Parameter mit der
// Funktion GetVerInfo(int iId):
//-----------------------------------------------------------------
const int VI_COMPANY_NAME      = 0;      // Firmen-Name
const int VI_FILE_DESCRIPTION  = 1;      // Dateibeschreibung
const int VI_FILE_VERSION      = 2;      // Versionsnummer
const int VI_INTERNAL_NAME     = 3;      // Interner Name
const int VI_LEGAL_COPYRIGHT   = 4;      // Copyright-Info
const int VI_LEGAL_TRADEMARKS  = 5;      // Warenzeichen
const int VI_ORIGINAL_FILENAME = 6;      // Orig.-Dateiname
const int VI_PRODUCT_NAME      = 7;      // Produktname
const int VI_PRODUCT_VERSION   = 8;      // Produkt-Version
const int VI_FILE_COMMENTS     = 9;      // Kommentare

class TVerInfo
{
      private:

        AnsiString sCompany_name;
        AnsiString sFile_description;
        AnsiString sFile_version;
        AnsiString sInternal_name;
        AnsiString sLegal_copyright;
        AnsiString sLegal_trademarks;
        AnsiString sOriginal_filename;
        AnsiString sProduct_name;
        AnsiString sProduct_version;
        AnsiString sFile_comments;

      protected:

        char *GetVersionKey(char *cBuffer, const AnsiString &sPrefix, char *cKey);

      public:

        __fastcall TVerInfo(AnsiString sExeName);
        AnsiString GetVerInfo(int iID);
};
#endif
  • *.cpp
__fastcall TVerInfo::TVerInfo(AnsiString sExeName)
{
           DWORD dwHandle;
           DWORD dwSize;

           sCompany_name       = "";
           sFile_description   = "";
           sFile_version       = "";
           sInternal_name      = "";
           sLegal_copyright    = "";
           sLegal_trademarks   = "";
           sOriginal_filename  = "";
           sProduct_name       = "";
           sProduct_version    = "";
           sFile_comments      = "";

           if (FileExists(sExeName))
           {
              dwSize = GetFileVersionInfoSize(sExeName.c_str(), &dwHandle);

              if (dwSize > 0)
              {
                 char *cBuffer = new char[dwSize];

                 if (GetFileVersionInfo(sExeName.c_str(), 0, dwSize, cBuffer))
                 {
                    UINT uiDataSize;
                    unsigned short *usTranslation;
                    VerQueryValue(cBuffer, "\\VarFileInfo\\Translation",(void **) &usTranslation, &uiDataSize);

                    AnsiString sPrefix = "\\StringFileInfo\\" + AnsiString::IntToHex(usTranslation [0], 4)+ AnsiString::IntToHex(usTranslation [1], 4);

                    sCompany_name       = GetVersionKey(cBuffer, sPrefix, "CompanyName");
                    sFile_description   = GetVersionKey(cBuffer, sPrefix, "FileDescription");
                    sFile_version       = GetVersionKey(cBuffer, sPrefix, "FileVersion");
                    sInternal_name      = GetVersionKey(cBuffer, sPrefix, "InternalName");
                    sLegal_copyright    = GetVersionKey(cBuffer, sPrefix, "LegalCopyright");
                    sLegal_trademarks   = GetVersionKey(cBuffer, sPrefix, "LegalTrademarks");
                    sOriginal_filename  = GetVersionKey(cBuffer, sPrefix, "OriginalFilename");
                    sProduct_name       = GetVersionKey(cBuffer, sPrefix, "ProductName");
                    sProduct_version    = GetVersionKey(cBuffer, sPrefix, "ProductVersion");
                    sFile_comments      = GetVersionKey(cBuffer, sPrefix, "Comments");
                 }

                 delete [] cBuffer;
              }
           }
}

char* TVerInfo::GetVersionKey(char *cBuffer, const AnsiString &sPrefix, char *cKey)
{
      char *cData;
      UINT uiDatasize;
      AnsiString sFullkey = sPrefix + "\\" + cKey;
      bool bStatus = VerQueryValue(cBuffer, sFullkey.c_str(), (void **)&cData, &uiDatasize);
      return (bStatus) ? cData : "";
}

AnsiString TVerInfo::GetVerInfo(int iID)
{
           AnsiString sRetValue;

           switch (iID)
           {
                  case VI_COMPANY_NAME      : { sRetValue = sCompany_name; break; }
                  case VI_FILE_DESCRIPTION  : { sRetValue = sFile_description; break; }
                  case VI_FILE_VERSION      : { sRetValue = sFile_version; break; }
                  case VI_INTERNAL_NAME     : { sRetValue = sInternal_name; break; }
                  case VI_LEGAL_COPYRIGHT   : { sRetValue = sLegal_copyright; break; }
                  case VI_LEGAL_TRADEMARKS  : { sRetValue = sLegal_trademarks; break; }
                  case VI_ORIGINAL_FILENAME : { sRetValue = sOriginal_filename; break; }
                  case VI_PRODUCT_NAME      : { sRetValue = sProduct_name; break; }
                  case VI_PRODUCT_VERSION   : { sRetValue = sProduct_version; break; }
                  case VI_FILE_COMMENTS     : { sRetValue = sFile_comments; break; }
                  default                   : { sRetValue = ""; }
           }

           return sRetValue;
}

Schreibe einen Kommentar