How to determine Delphi Application Version

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

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

6条回答
  •  粉色の甜心
    2020-12-08 05:20

    I most strongly recommend not to use GetFileVersion when you want to know the version of the executable that is currently running! I have two pretty good reasons to do this:

    1. The executable may be unaccessible (disconnected drive/share), or changed (.exe renamed to .bak and replaced by a new .exe without the running process being stopped).
    2. The version data you're trying to read has actually already been loaded into memory, and is available to you by loading this resource, which is always better than to perform extra (relatively slow) disk operations.

    To load the version resource in Delphi I use code like this:

    uses Windows,Classes,SysUtils;
    var
      verblock:PVSFIXEDFILEINFO;
      versionMS,versionLS:cardinal;
      verlen:cardinal;
      rs:TResourceStream;
      m:TMemoryStream;
      p:pointer;
      s:cardinal;
    begin
      m:=TMemoryStream.Create;
      try
        rs:=TResourceStream.CreateFromID(HInstance,1,RT_VERSION);
        try
          m.CopyFrom(rs,rs.Size);
        finally
          rs.Free;
        end;
        m.Position:=0;
        if VerQueryValue(m.Memory,'\',pointer(verblock),verlen) then
          begin
            VersionMS:=verblock.dwFileVersionMS;
            VersionLS:=verblock.dwFileVersionLS;
            AppVersionString:=Application.Title+' '+
              IntToStr(versionMS shr 16)+'.'+
              IntToStr(versionMS and $FFFF)+'.'+
              IntToStr(VersionLS shr 16)+'.'+
              IntToStr(VersionLS and $FFFF);
          end;
        if VerQueryValue(m.Memory,PChar('\\StringFileInfo\\'+
          IntToHex(GetThreadLocale,4)+IntToHex(GetACP,4)+'\\FileDescription'),p,s) or
            VerQueryValue(m.Memory,'\\StringFileInfo\\040904E4\\FileDescription',p,s) then //en-us
              AppVersionString:=PChar(p)+' '+AppVersionString;
      finally
        m.Free;
      end;
    end;
    

提交回复
热议问题