To get detailed information about another Windows program, such as the product name and version info, from a Lazarus application, you can utilize the Windows API to extract version information from the executable file of that application. Below is a step-by-step guide on how you can achieve this:
Include Necessary Units: Make sure to include the required units for handling Windows API calls. You will typically need Windows and SysUtils.
Access the File: Obtain the file path of the executable whose details you wish to retrieve.
Use Windows API Functions: You will use a combination of Windows API functions like GetFileVersionInfoSize, GetFileVersionInfo, and VerQueryValue.
Extract Version Data: Parse the version data to get the desired information such as product name, version number, etc.
Here's an example of how you might implement this in a Lazarus application:
pascal
uses
Windows, SysUtils;
function GetProgramVersionInfo(const FilePath: string): string;
var
Size, Handle: DWORD;
Buffer, Value: Pointer;
ValueSize: UINT;
FI: PVSFixedFileInfo;
Trans: LPBYTE;
TransLen: UINT;
VerStr: string;
begin
Result := '';
Size := GetFileVersionInfoSize(PChar(FilePath), Handle);
if Size = 0 then Exit;
GetMem(Buffer, Size);
try
if GetFileVersionInfo(PChar(FilePath), Handle, Size, Buffer) then
begin
if VerQueryValue(Buffer, '\', Pointer(FI), ValueSize) then
begin
VerStr := Format('Version: %d.%d.%d.%d',
[HIWORD(FI.dwFileVersionMS), LOWORD(FI.dwFileVersionMS),
HIWORD(FI.dwFileVersionLS), LOWORD(FI.dwFileVersionLS)]);
Result := VerStr;
end;
// Also extract the product name
if VerQueryValue(Buffer, '\VarFileInfo\Translation', Pointer(Trans), TransLen) then
begin
VerStr := Format('\StringFileInfo\%0.4x%0.4x\ProductName',
[LoWord(Integer(Trans^)), HiWord(Integer(Trans^))]);
if VerQueryValue(Buffer, PChar(VerStr), Value, ValueSize) then
Result := Result + sLineBreak + 'Product Name: ' + PChar(Value);
end;
end;
finally
FreeMem(Buffer);
end;
end;
// Usage
procedure TForm1.Button1Click(Sender: TObject);
var
FilePath: string;
Info: string;
begin
FilePath := 'C:\Path\To\Your\Program.exe'; // Specify the path to the executable
Info := GetProgramVersionInfo(FilePath);
ShowMessage(Info);
end;
Explanation:
GetFileVersionInfoSize: This function retrieves the size of the version information block from the executable.
GetFileVersionInfo: This function retrieves version information for the specified file.
VerQueryValue: This function retrieves various values from the version information block, including fixed file information and string information like product name.
Notes:
Ensure the executable you are trying to query is accessible, and you have permission to read it.
Error handling can be added to gracefully manage scenarios where the information cannot be retrieved.
This code should help you display product and version information for an executable within a Lazarus application.