Important alert: (current site time 7/16/2013 5:50:11 AM EDT)
 

VB icon

access application version information programmatically.

Email
Submitted on: 12/21/2001 2:08:12 PM
By: filter 
Level: Intermediate
User Rating: By 2 Users
Compatibility: Delphi 5
Views: 17324
 
     This function will read the file version information from your application's executable file. It does this through the Win32 API functions GetFileVersionInfoSize(), GetFileVersionInfo(), and VerQueryValue(). This can be very useful if you have your IDE set to increment build numbers each time you compile your project, as it can be used to return the current version number. Since I use the version number in a lot of my applications, it's really a pain to have to update a global constant each time the version number changes. This makes it much easier. Please note: I found a version of this code in the Borland Delphi 6 help files, and modified it so it would be a function, and return only the file version information. Additionally, there were very few comments, so I commented much of the code as well. You can view the original code if you do a search in the help files for "Reading version information".
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
!**************************************
! Name: access application version information programmatically.
! Description:This function will read the file version information from your application's executable file. It does this through the Win32 API functions GetFileVersionInfoSize(), GetFileVersionInfo(), and VerQueryValue(). This can be very useful if you have your IDE set to increment build numbers each time you compile your project, as it can be used to return the current version number. Since I use the version number in a lot of my applications, it's really a pain to have to update a global constant each time the version number changes. This makes it much easier. Please note: I found a version of this code in the Borland Delphi 6 help files, and modified it so it would be a function, and return only the file version information. Additionally, there were very few comments, so I commented much of the code as well. You can view the original code if you do a search in the help files for "Reading version information".
! By: filter
!
! Returns:A string containing the current file version of your application's executable file.
!
!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=542&lngWId=7!for details.!**************************************

function Application_Version: String;
const
 InfoNum = 10;
 InfoStr: array[1..InfoNum] of string = ('CompanyName', 'FileDescription', 'FileVersion', 'InternalName', 'LegalCopyright', 'LegalTradeMarks', 'OriginalFileName', 'ProductName', 'ProductVersion', 'Comments');
var
 S: string; n, Len, i: DWORD;
 Buf: PChar; Value: PChar;
begin
 S := Application.ExeName; //this application's EXE filename
 n := GetFileVersionInfoSize(PChar(S), n); // the return value is the size in bytes of the file's version information.
 Result := ''; (* this sets a default result in case a file version isn't found.
I add this to functions because the compiler will bug you that
the result may not be defined otherwise. you could change this,
or remove it alltogether, although it's probably best to leave it. *)
 if n > 0 then begin
Buf := AllocMem(n); // allocate the needed amount of memory into the buffer
GetFileVersionInfo(PChar(S), 0, n, Buf); // store the file version information in the memory buffer. it stores all of the values listed in the InfoStr array.
for i := 1 to InfoNum do
 if VerQueryValue(Buf, PChar('StringFileInfo\040904E4\' + InfoStr[i]), Pointer(Value), Len) then
if trim(lowerCase(InfoStr[i])) = 'fileversion' then Result := Value;// loop through each value until we find the "FileVersion" information.
FreeMem(Buf, n); // free the memory we stored in the buffer.
 end;
end;


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/1/2003 7:30:31 PM

Hi, I tried this code in Delphi 5 running on xp and it returns a null string instead of the appropriate data. What am I doing wrong?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.