On 22.02.2015 20:37, Mattias Gaertner wrote:
Post a complete example and someone can find out.

Mattias
code block is not mine, I was find in this link.
http://lists.lazarus.freepascal.org/pipermail/lazarus/2013-November/084198.html

function AnyProgramVersion(const sProgram: string; Out vv: PTVersion) :Boolean;
const
  WIN_EXE: String = 'MZ';
  LINUX_EXE: String = #127 + 'ELF';
var
  wFileStr: TFileStream;
  RS : TResources;
  E : TWinPEImageResourceReader=nil;
  EL : TElfResourceReader=nil;
  VR : TVersionResource;
  I : Integer;
  {$IFDEF WINCE}
  wProgram: WideString;
  {$ELSE}
  wProgram: String;
  {$ENDIF}
  ar: array[0..4] of byte;
  ExecType: char;
begin
  Result := False;
  vv := New(PTVersion);
  with vv^ do begin
    Major:= -1;
    Minor := 0;
    Release := 0;
    Build := 0;
  end;
  if sProgram = EmptyStr then
    wProgram := ParamStr(0)
  else
    wProgram := sProgram;
  if not FileExists(wProgram) then begin
    // program name without path, assume directory of executing program
    if Pos(DirectorySeparator, wProgram) = 0 then begin
      wProgram := IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 
sProgram;
      if not FileExists(wProgram) then
        Exit;
    end else
      // no path delimiter, get out
      Exit;
  end else
    wProgram := sProgram;
  wFileStr := TFileStream.Create(sProgram, fmOpenRead);
  try
    // check what kind of executable an get resources accordingly
    wFileStr.Read(ar, 4);
  Finally
    wFileStr.Free;
  end;
  if CompareMem(@(ar), Pchar(WIN_EXE), 2) then
    ExecType := 'W'
  else
  if CompareMem(@(ar), Pchar(LINUX_EXE), 4) then
    ExecType := 'L'
  else
    Exit;
  RS:=TResources.Create;
  try
    try
      if ExecType = 'W' then begin
        E := TWinPEImageResourceReader.Create;
        Rs.LoadFromFile(wProgram, E);
      end else begin
        EL := TElfResourceReader.Create;
        Rs.LoadFromFile(wProgram, EL);
      end;
    finally
      if Assigned(E) then
        E.Free;
      if Assigned(EL) then
        EL.Free;
    end;
    VR:=Nil;
    I:=0;
    While (VR=Nil) and (I < RS.Count) do begin
      if RS.Items[i] is TVersionResource then
        VR := TVersionResource(RS.Items[i]);
      Inc(I);
    end;
    if VR <> Nil then begin
      with vv^ do begin
        Major:= VR.FixedInfo.FileVersion[0];
        Minor := VR.FixedInfo.FileVersion[1];
        Release := VR.FixedInfo.FileVersion[2];
        Build := VR.FixedInfo.FileVersion[3];
      end;
      Result := True
    End ;
  Finally
    RS.FRee;
  end;
end;

{$IFDE


--
_______________________________________________
Lazarus mailing list
[email protected]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to