Title: Getting an applications version number in runtime
Question: How can I get my applications version numbers (the one entered in the project options) in runtime?
Answer:
You can get your applications version numbers (and lots of other information) using the WinAPI "VerQueryValue". see code below...
Also check Radikal Q3's Delphi3000 article:1275 for more information on how to use the VerQueryValue API.
http://www.delphi3000.com/article.asp?ID=1275
Make sure "Windows" is in your uses clause.
//=================================================================
// DWHi returns the high word in the DWord
function DWHi(val : DWord) : word assembler;
asm
mov EAX, val;
shr EAX,16;
end;
// DWHi returns the low word in the DWord
function DWLo(val : DWord) : word assembler;
asm
mov EAX, val;
end;
// GetFileVersion fills the VerBlk with the version information from file "filename"
// If no version information is avaible the function returns false, otherwise true
function GetFileVersion(filename : string; var VerBlk : VS_FIXEDFILEINFO) : boolean;
var InfoSize,puLen : DWord;
Pt,InfoPtr : Pointer;
begin
InfoSize := GetFileVersionInfoSize(PChar(filename),puLen);
fillchar(VerBlk,sizeof(VS_FIXEDFILEINFO),0);
if InfoSize 0 then begin
GetMem(Pt,InfoSize);
GetFileVersionInfo(PChar(filename),0,InfoSize,Pt);
VerQueryValue(Pt,'\',InfoPtr,puLen);
move(InfoPtr^,VerBlk,sizeof(VS_FIXEDFILEINFO));
FreeMem(Pt);
result := true;
end else result := false;
end;
// GetProductVerStr formats the version info into a readable string
function GetProductVerStr(VerBlk : VS_FIXEDFILEINFO) : string;
begin
result := Format('%u.%u.%u.%u',
[DWHi(VerBlk.dwProductVersionMS),
DWLo(VerBlk.dwProductVersionMS),
DWHi(VerBlk.dwProductVersionLS),
DWLo(VerBlk.dwProductVersionLS)]);
end;
procedure TForm1.Button1Click(Sender: TObject);
var fileinfo : VS_FIXEDFILEINFO;
begin
if GetFileVersion(application.ExeName,fileinfo) then begin
label1.caption := GetProductVerStr(fileinfo);
end else label1.caption := 'File did not contain version info';
end;