Getting version information from a file (a better one)
   


GetAppVersionInfo() is great for your About boxes in your Delphi projects. To use it, make sure you have the necessary info built into the EXE by selecting (in the Delphi IDE) Project | Project Options, Version Info tab, input your EXE info, and build your project. Simply get this info during run-time by calling GetAppVersionInfo(Application.ExeName) and read the results in the TFileVersionInfo record. You can do the same for other EXEs by calling GetAppVersionInfo('EXENAME.EXE'). This is similar to GetAllFileVersionInfo() except that this one seems to work better on most Microsoft and 3rd party DLLs and EXEs. Thanks to Harald Fjerdingstad for this code.

Note: Some developers have reported problems with storing version information resources to their DLL's written in Delphi. To solve this, just add the compiler directive
{$R *.RES} to the DLL project source, which is NOT added in by default for new DLL's.

{$DEFINE Delphi3Below}
{$IFDEF VER130} //Delphi 5
  {$UNDEF Delphi3Below}
{$ELSE}
  {$IFDEF VER120} //Delphi 4
    {$UNDEF Delphi3Below}
  {$ENDIF}
{$ENDIF}

type
  TFileVersionInfo = record
    fCompanyName,
    fFileDescription,
    fFileVersion,
    fInternalName,
    fLegalCopyRight,
    fLegalTradeMark,
    fOriginalFileName,
    fProductName,
    fProductVersion,
    fComments: string;
  end;

var
  FileVersionInfo: TFileVersionInfo;

procedure GetAppVersionInfo(sAppNamePath: string);
{ sAppNamePath must specify full path and file name, if sAppNamePath }
{ is empty then the current running app is used. } 
{ Thanks to Harald Fjerdingstad [[email protected]] for most of this code. }
{ Some developers have reported problems with storing version information }
{ resources to their DLL's written in Delphi. To solve this, just add the }
{ compiler directive {$R *.RES} to the DLL project source, which is NOT }
{ added in by default for new DLL's. }

var
  VerSize: integer;
  VerBuf: PChar;
  VerBufValue: pointer;
  
{$IFDEF Delphi3Below}
  VerHandle: integer;
  VerBufLen: integer;
  
{$ELSE}
  VerHandle: cardinal;
  VerBufLen: cardinal;
  
{$ENDIF}
  VerKey: string;

  function GetInfo(ThisKey: string): string;
  begin
    Result := '';
    VerKey := '\StringFileInfo\' + IntToHex(loword(integer(VerBufValue^)), 4) +
    IntToHex(hiword(integer(VerBufValue^)), 4) + '\' + ThisKey; 
    if VerQueryValue(VerBuf, PChar(VerKey), VerBufValue, VerBufLen) then
      Result := StrPas(VerBufValue);
  end;

  function QueryValue(ThisValue: string): string;
  begin
    Result := '';
    if GetFileVersionInfo(PChar(sAppNamePath), VerHandle, VerSize, VerBuf) and
      VerQueryValue(VerBuf, '\VarFileInfo\Translation', VerBufValue, VerBufLen) then
      Result := GetInfo(ThisValue);
  end;

begin
  if sAppNamePath = '' then
    sAppNamePath := Application.ExeName;
  VerSize := GetFileVersionInfoSize(PChar(sAppNamePath), VerHandle);
  VerBuf := AllocMem(VerSize);
  try
    FileVersionInfo.fCompanyName := QueryValue('CompanyName'); 
    FileVersionInfo.fFileDescription := QueryValue('FileDescription');
    FileVersionInfo.fFileVersion := QueryValue('FileVersion'); 
    FileVersionInfo.fInternalName := QueryValue('InternalName'); 
    FileVersionInfo.fLegalCopyRight := QueryValue('LegalCopyRight');
    FileVersionInfo.fLegalTradeMark := QueryValue('LegalTradeMark');
    FileVersionInfo.fOriginalFileName := QueryValue('OriginalFileName');
    FileVersionInfo.fProductName := QueryValue('ProductName'); 
    FileVersionInfo.fProductVersion := QueryValue('ProductVersion');
    FileVersionInfo.fComments := QueryValue('Comments');
  finally
    FreeMem(VerBuf, VerSize);
  end;
end;



Blue Orb Software
[email protected]