TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
Displaying Version Information
 
   
 Posted: 25 January 2001
 
   
 
 Applies to: Delphi 4.0 and later
 
   
 
Audience: Intermediate
 
       
   

Introduction

Windows has long provided a VERSIONINFO structure designed to help application developers present version, copyright, and contact information in a consistent, professional, and (perhaps most importantly) easy-to-locate fashion. For example, if you right-click a program file in Windows Explorer and then choose Properties, the property sheet will contain a Version tab listing various details about the program. This tab only appears, however, when the application is compiled with a VERSIONINFO structure.

Unfortunately, VERSIONINFO is not the easiest structure to work with; it has several complexities that many developers politely describe as "daunting."

Recognizing an opportunity during the development of Delphi 4.0, Borland added a compilation option that creates and maintains VERSIONINFO structures in your projects. This option also increments version numbers when you build your project. This can be a tremendous time-saving device, especially if you've been relying on manually updated constants.

Yet, the Version Info Project Options only provides half the solution. There are times when you want to extract pieces of this structure for your own use. For example, you might want to display the version number in your About box.

The following function shows one way to do this:

function getVersion : string;
{ ---------------------------------------------------------
   Extracts the FileVersion element of the VERSIONINFO
   structure that Delphi maintains as part of a project's
   options.

   Results are returned as a standard string.  Failure
   is reported as "".

   Note that this implementation was derived from similar
   code used by Delphi to validate ComCtl32.dll.  For
   details, see COMCTRLS.PAS, line 3541.
  -------------------------------------------------------- }
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 ) then

            if 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;

Granted, this is a very limited implementation, for it only extract the version number. However, there are times when that's all you need.

Note: A more complete implementation can be found in Delphi 5 Developer's Guide; see Sources for details.

Here's one way you might use this in your application:

procedure TfrmAboutBox.FormCreate(Sender: TObject);
begin
   lblVersion.Caption := 'Version: ' + getVersion;
end;

Sources:

  • Source code provided with Delphi 5 Professional; see the implementation of the GetComCtlVersion function. With the 5.01 patch applied, this starts on line 3547 in COMMCTRL.PAS.
  • Delphi 5 Developer's Guide, by Steve Teixeira and Xavier Pacheco. Published December 1999, by Sams Publishing. ISBN: 0672317818 (See Chapter 12).

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 01 June 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]