How to determine Delphi Application Version

后端 未结 6 909
悲哀的现实
悲哀的现实 2020-12-08 04:31

Want to obtain Delphi Application build number and post into title bar

6条回答
  •  情话喂你
    2020-12-08 05:06

    Thanks to the posts above, I made my own library for this purpose.

    I believe that it is a little bit more correct than all other solutions here, so I share it - feel free to reuse it...

    unit KkVersion;
    
    interface
    
    function FileDescription: String;
    function LegalCopyright: String;
    function DateOfRelease: String; // Proprietary
    function ProductVersion: String;
    function FileVersion: String;
    
    implementation
    
    uses
      Winapi.Windows, System.SysUtils, System.Classes, Math;
    
    (*
      function GetHeader(out AHdr: TVSFixedFileInfo): Boolean;
    
      var
      BFixedFileInfo: PVSFixedFileInfo;
      RM: TMemoryStream;
      RS: TResourceStream;
      BL: Cardinal;
    
      begin
      Result := False;
      RM := TMemoryStream.Create;
      try
      RS := TResourceStream.CreateFromID(HInstance, 1, RT_VERSION);
      try
      RM.CopyFrom(RS, RS.Size);
      finally
      FreeAndNil(RS);
      end;
    
      // Extract header
      if not VerQueryValue(RM.Memory, '\\', Pointer(BFixedFileInfo), BL) then
      Exit;
    
      // Prepare result
      CopyMemory(@AHdr, BFixedFileInfo, Math.Min(sizeof(AHdr), BL));
      Result := True;
      finally
      FreeAndNil(RM);
      end;
      end;
    *)
    
    function GetVersionInfo(AIdent: String): String;
    
    type
      TLang = packed record
        Lng, Page: WORD;
      end;
    
      TLangs = array [0 .. 10000] of TLang;
    
      PLangs = ^TLangs;
    
    var
      BLngs: PLangs;
      BLngsCnt: Cardinal;
      BLangId: String;
      RM: TMemoryStream;
      RS: TResourceStream;
      BP: PChar;
      BL: Cardinal;
      BId: String;
    
    begin
      // Assume error
      Result := '';
    
      RM := TMemoryStream.Create;
      try
        // Load the version resource into memory
        RS := TResourceStream.CreateFromID(HInstance, 1, RT_VERSION);
        try
          RM.CopyFrom(RS, RS.Size);
        finally
          FreeAndNil(RS);
        end;
    
        // Extract the translations list
        if not VerQueryValue(RM.Memory, '\\VarFileInfo\\Translation', Pointer(BLngs), BL) then
          Exit; // Failed to parse the translations table
        BLngsCnt := BL div sizeof(TLang);
        if BLngsCnt <= 0 then
          Exit; // No translations available
    
        // Use the first translation from the table (in most cases will be OK)
        with BLngs[0] do
          BLangId := IntToHex(Lng, 4) + IntToHex(Page, 4);
    
        // Extract field by parameter
        BId := '\\StringFileInfo\\' + BLangId + '\\' + AIdent;
        if not VerQueryValue(RM.Memory, PChar(BId), Pointer(BP), BL) then
          Exit; // No such field
    
        // Prepare result
        Result := BP;
      finally
        FreeAndNil(RM);
      end;
    end;
    
    function FileDescription: String;
    begin
      Result := GetVersionInfo('FileDescription');
    end;
    
    function LegalCopyright: String;
    begin
      Result := GetVersionInfo('LegalCopyright');
    end;
    
    function DateOfRelease: String;
    begin
      Result := GetVersionInfo('DateOfRelease');
    end;
    
    function ProductVersion: String;
    begin
      Result := GetVersionInfo('ProductVersion');
    end;
    
    function FileVersion: String;
    begin
      Result := GetVersionInfo('FileVersion');
    end;
    
    end.
    

提交回复
热议问题