1. Home
  2. Computing & Technology
  3. Delphi Programming

Get EXE Version Information

By , About.com Guide

Here's how to populate a listbox with the executable's version information:

~~~~~~~~~~~~~~~~~~~~~~~~~
{
Usage:
VersionInformation(ListBox1)
}

{Pads or truncates a String and
Justifies Left if StrJustify=True}
Function StringPad(
   InputStr,
   FillChar: String;
   StrLen: Integer;
   StrJustify: Boolean): String;
Var
   TempFill: String;
   Counter : Integer;
Begin
   If Not (Length(InputStr) = StrLen) Then
   Begin
     If Length(InputStr) > StrLen Then
     Begin
       InputStr := Copy(InputStr,1,StrLen) ;
     End
     Else
     Begin
       TempFill := '';
       For Counter := 1 To StrLen-Length(InputStr) Do
       Begin
         TempFill := TempFill + FillChar;
       End;
       If StrJustify Then
       Begin
         {Left Justified}
         InputStr := InputStr + TempFill;
       End
       Else
       Begin
         {Right Justified}
         InputStr := TempFill + InputStr ;
       End;
     End;
   End;
   Result := InputStr;
End;


Function VersionInformation(
   ListBox : TListBox): Boolean;
const
   InfoNum = 11;
   InfoStr : array [1..InfoNum] of String =
     ('CompanyName', 'FileDescription', 'FileVersion',
'InternalName', 'LegalCopyright', 'LegalTradeMarks',
'OriginalFilename', 'ProductName', 'ProductVersion',
'Comments', 'Author') ;
   LabelStr : array [1..InfoNum] of String =
     ('Company Name', 'Description', 'File Version',
'Internal Name', 'Copyright', 'TradeMarks',
'Original File Name', 'Product Name',
'Product Version', 'Comments', 'Author') ;
var
   S : String;
   n, Len, j : Integer;
   Buf : PChar;
   Value : PChar;
begin
   Try
     S := Application.ExeName;
     ListBox.Items.Clear;
     ListBox.Sorted := True;
     ListBox.Font.Name := 'Courier New';
     n := GetFileVersionInfoSize(PChar(S),n) ;
     If n > 0 Then Begin
       Buf := AllocMem(n) ;
       ListBox.Items.Add
(StringPad('Size',' ',20,True)+' = '+IntToStr(n)) ;
       GetFileVersionInfo(PChar(S),0,n,Buf) ;
       For j:=1 To InfoNum Do Begin
         If VerQueryValue(Buf,PChar('StringFileInfo\040904E4\'+
                           InfoStr[j]),Pointer(Value),Len) Then
         Begin
           Value := PChar(Trim(Value)) ;
           If Length(Value) > 0 Then
           Begin
             ListBox.Items.Add
(StringPad(labelStr,' ',20,True)+' = '+Value) ;
           End;
         End;
       End;
       FreeMem(Buf,n) ;
     End
     Else Begin
       ListBox.Items.Add
('No FileVersionInfo found') ;
     End;
     Result := True;
   Except
     Result := False;
   End;
End;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Implementing a lasso drawing technique
« Set File Date (created)

About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >