You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Delphi 12中获取FMX项目版本号及代码访问构建号

在Delphi 12中获取FMX项目的版本号与构建号

获取版本号(主版本/次版本/修订号)

Delphi 12的FMX项目版本信息存储在程序资源段中,可通过以下两种方式获取:

方式一:Windows平台专用(API调用)

通过系统API读取程序自身的版本资源:

uses
  Winapi.Windows, System.SysUtils;

function GetAppVersion: string;
var
  VerInfoSize: DWORD;
  VerInfo: Pointer;
  VerValueSize: DWORD;
  VerValue: PVSFixedFileInfo;
  FileName: string;
begin
  Result := '';
  FileName := ParamStr(0);
  VerInfoSize := GetFileVersionInfoSize(PChar(FileName), nil);
  if VerInfoSize > 0 then
  begin
    GetMem(VerInfo, VerInfoSize);
    try
      if GetFileVersionInfo(PChar(FileName), 0, VerInfoSize, VerInfo) then
      begin
        if VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize) then
        begin
          Result := Format('%d.%d.%d', [
            HiWord(VerValue.dwFileVersionMS),
            LoWord(VerValue.dwFileVersionMS),
            HiWord(VerValue.dwFileVersionLS)
          ]);
        end;
      end;
    finally
      FreeMem(VerInfo);
    end;
  end;
end;

方式二:跨平台通用方法

利用Delphi自带的TVersionInfo类适配多平台:

uses
  System.Version, System.SysUtils;

function GetCrossPlatformAppVersion: string;
var
  Version: TVersionInfo;
begin
  Version := TVersionInfo.Create(ParamStr(0));
  try
    Result := Format('%d.%d.%d', [Version.Major, Version.Minor, Version.Release]);
  finally
    Version.Free;
  end;
end;

获取构建号

构建号对应版本信息中的低位标识,可通过以下方式获取:

Windows平台API扩展

在之前的API方法中,扩展返回值即可提取构建号:

// 修改GetAppVersion函数的返回逻辑
Result := Format('%d.%d.%d.%d', [
  HiWord(VerValue.dwFileVersionMS),
  LoWord(VerValue.dwFileVersionMS),
  HiWord(VerValue.dwFileVersionLS),
  LoWord(VerValue.dwFileVersionLS) // 此处为构建号
]);

跨平台通用方式

直接读取TVersionInfo的Build属性:

uses
  System.Version, System.SysUtils;

function GetAppBuildNumber: Integer;
var
  Version: TVersionInfo;
begin
  Result := 0;
  Version := TVersionInfo.Create(ParamStr(0));
  try
    Result := Version.Build;
  finally
    Version.Free;
  end;
end;

快捷方式:预定义常量

如果在【Project | Options | Application | Version Info】中勾选了**"Define constants"**选项,可直接使用自动生成的常量:

// 直接输出版本号与构建号
ShowMessage(Format('版本号: %d.%d.%d 构建号: %d', [MAJORVERSION, MINORVERSION, RELEASEVERSION, BUILDVERSION]));

内容的提问来源于stack exchange,提问作者Mike at Bookup

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 03:22:55