Title: Final! all bugs fixed! Get EXE/DLL file version
Question: How do extract file version from an EXE/DLL?
Others I've tried give GPF's or leak memory!
This function will return string in format 'n.n.n.n' and also pushes the values into the variables passed (by reference).
Answer:
// Note: nValue1,2,3,4 MUST be different variables or function will not work!
function GetFileVersion(const sFilename: String; var nValue1,nValue2,nValue3,nValue4: Integer): String;
var
pInfo,pPointer: Pointer;
nSize: DWORD;
nHandle: DWORD;
pVerInfo: PVSFIXEDFILEINFO;
nVerInfoSize: DWORD;
begin
Result:='?.?.?.?';
nValue1:=-1;
nValue2:=-1;
nValue3:=-1;
nValue4:=-1;
nSize:=GetFileVersionInfoSize(PChar(sFilename),nHandle);
if (nSize0) then begin
GetMem(pInfo,nSize);
try
FillChar(pInfo^,nSize,0);
if (GetFileVersionInfo(PChar(sFilename),nHandle,nSize,pInfo)) then begin
nVerInfoSize:=SizeOf(VS_FIXEDFILEINFO);
GetMem(pVerInfo,nVerInfoSize);
try
FillChar(pVerInfo^,nVerInfoSize,0);
pPointer:=Pointer(pVerInfo);
VerQueryValue(pInfo,'\',pPointer,nVerInfoSize);
nValue1:=PVSFIXEDFILEINFO(pPointer)^.dwFileVersionMS shr 16;
nValue2:=PVSFIXEDFILEINFO(pPointer)^.dwFileVersionMS and $FFFF;
nValue3:=PVSFIXEDFILEINFO(pPointer)^.dwFileVersionLS shr 16;
nValue4:=PVSFIXEDFILEINFO(pPointer)^.dwFileVersionLS and $FFFF;
Result:=IntToStr(nValue1)+'.'+IntToStr(nValue2)+'.'+IntToStr(nValue3)+'.'+IntToStr(nValue4);
finally
FreeMem(pVerInfo,nVerInfoSize);
end;
end;
finally
FreeMem(pInfo,nSize);
end;
end;
end;