Link to home
Start Free TrialLog in
Avatar of CodedK
CodedKFlag for Greece

asked on

Get File version info.

Hi.

I need a small working code to retrieve some version information for a file.
I've searched EE database and saw some answers but the code is big and some times doesnt work.

Thanks in advance :)
Avatar of jpedef
jpedef
Flag of Finland image

This piece of code should work

const
  InfoNum = 10;
  InfoStr: array[1..InfoNum] of string = ('CompanyName',
  'FileDescription', 'FileVersion', 'InternalName', 'LegalCopyright',
  'LegalTradeMarks', 'OriginalFileName', 'ProductName', 'ProductVersion', 'Comments');

function GetFileVersionString(AFileName: string): string;
var
  VerStr: string;
  n, Len: DWORD;
  Buf: PChar;
  Value: PChar;
begin
  n := GetFileVersionInfoSize(PChar(AFileName), n);
  if n > 0 then
  begin
    Buf := AllocMem(n);
    try
      GetFileVersionInfo(PChar(AFileName), 0, n, Buf);
      if VerQueryValue(Buf, PChar('\StringFileInfo\040B04E4\FileVersion'), Pointer(Value), Len) then
        VerStr := 'Version: ' + Value
      else
        VerStr := 'Unknown version ';
    finally
      FreeMem(Buf);
    end;
  end
  else
    VerStr := 'No version information available';
  Result := VerStr;
end;
Avatar of CodedK

ASKER

Hi jpedef, thanks.

There is a problem with the code...
It shows 'no version info available' for files that have a version string.

I use it like this :
GetFileVersionString(Path of exe + exefilename)

How can i get the other strings like "comments","FileDescription","Company" etc ?
Try this one:
function getVersion : string;                         // poached from the online documentation
const    NOVIDATA = '';
var
  dwInfoSize,           // Size of VERSIONINFO structure
  dwVerSize,            // Size of Version Info Data
  dwWnd: DWORD;         // Handle for the size call.
  FI: PVSFixedFileInfo; // Delphi structure; see WINDOWS.PAS
  ptrVerBuf: Pointer;   // pointer to a version buffer
  strFileName,          // Name of the file to check
  strVersion : string;  // Holds parsed version number
begin
   strFileName := paramStr( 0 );
   dwInfoSize := getFileVersionInfoSize( pChar( strFileName ), dwWnd);
   if ( dwInfoSize = 0 ) then
      result := NOVIDATA
   else
     begin
       getMem( ptrVerBuf, dwInfoSize );
       try
         if getFileVersionInfo(pChar(strFileName),dwWnd,dwInfoSize,ptrVerBuf) and verQueryValue(ptrVerBuf,'\',pointer(FI),dwVerSize) then
           strVersion := format('%d.%d.%d.%d',[hiWord(FI.dwFileVersionMS),loWord(FI.dwFileVersionMS),
                       hiWord(FI.dwFileVersionLS),loWord(FI.dwFileVersionLS)]);
       finally
         freeMem( ptrVerBuf );
       end;
     end;
  Result := strVersion;
end;
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodedK

ASKER

Thanks Russell :)
No problem

Russell