如何确定Delphi应用程序版本

时间:2009-11-11 20:27:29

标签: delphi version

想获得Delphi Application内部版本号并发布到标题栏

6 个答案:

答案 0 :(得分:26)

我是这样做的。我把它放在几乎所有的小工具中:

procedure GetBuildInfo(var V1, V2, V3, V4: word);
var
  VerInfoSize, VerValueSize, Dummy: DWORD;
  VerInfo: Pointer;
  VerValue: PVSFixedFileInfo;
begin
  VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);
  if VerInfoSize > 0 then
  begin
      GetMem(VerInfo, VerInfoSize);
      try
        if GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo) then
        begin
          VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
          with VerValue^ do
          begin
            V1 := dwFileVersionMS shr 16;
            V2 := dwFileVersionMS and $FFFF;
            V3 := dwFileVersionLS shr 16;
            V4 := dwFileVersionLS and $FFFF;
          end;
        end;
      finally
        FreeMem(VerInfo, VerInfoSize);
      end;
  end;
end;

function GetBuildInfoAsString: string;
var
  V1, V2, V3, V4: word;
begin
  GetBuildInfo(V1, V2, V3, V4);
  Result := IntToStr(V1) + '.' + IntToStr(V2) + '.' +
    IntToStr(V3) + '.' + IntToStr(V4);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Form1.Caption := Form1.Caption + ' - v' + GetBuildInfoAsString;
end;

答案 1 :(得分:21)

当您想知道当前正在运行的可执行文件的版本时,我强烈建议您不要使用GetFileVersion!我有两个很好的理由这样做:

  1. 可执行文件可能无法访问(已断开连接的驱动器/共享)或已更改(.exe已重命名为.bak并由新的.exe替换,而不会停止正在运行的进程)。
  2. 您尝试读取的版本数据实际上已经加载到内存中,并且通过加载此资源可以使用,这总是比执行额外(相对较慢)的磁盘操作更好。
  3. 要在Delphi中加载版本资源,我使用如下代码:

    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;
    

答案 2 :(得分:11)

将EXE的完整文件名传递给此函数,它将返回如下字符串: 2.1.5.9,或者你的版本是什么。

function GetFileVersion(exeName : string): string;
const
  c_StringInfo = 'StringFileInfo\040904E4\FileVersion';
var
  n, Len : cardinal;
  Buf, Value : PChar;
begin
  Result := '';
  n := GetFileVersionInfoSize(PChar(exeName),n);
  if n > 0 then begin
    Buf := AllocMem(n);
    try
      GetFileVersionInfo(PChar(exeName),0,n,Buf);
      if VerQueryValue(Buf,PChar(c_StringInfo),Pointer(Value),Len) then begin
        Result := Trim(Value);
      end;
    finally
      FreeMem(Buf,n);
    end;
  end;
end;

在定义之后,您可以使用它来设置表单的标题,如下所示:

procedure TForm1.FormShow(Sender: TObject);
begin 
  //ParamStr(0) is the full path and file name of the current application
  Form1.Caption := Form1.Caption + ' version ' + GetFileVersion(ParamStr(0));
end;

答案 3 :(得分:8)

感谢上面的帖子,我为此创建了自己的图书馆。

我相信它比其他所有解决方案都更正确,所以我分享它 - 随意重复使用它......

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.

答案 4 :(得分:2)

来自http://www.martinstoeckli.ch/delphi/delphi.html#AppVersion

  

使用此功能,您可以获取包含a的文件版本   版本资源。这样您就可以显示您的版本号   应用于信息对话框中。要包含版本资源   您的Delphi应用程序,在项目选项中设置“Versioninfo”。

答案 5 :(得分:0)

我们为所有应用程序执行此操作,但我们使用Raize组件RzVersioninfo。 工作得很好只需要使用以下代码

表单

上的

标题:= RzVersioninfo1.filedescripion +':'+ RzVersionInfo1.FileVersion;

显然,如果您不希望raize中的任何其他组件使用上述选项之一,因为raize组件会产生成本。

相关问题