Delphi XE 取得 APP 自己的版本号 (狠跨 4 个平台)


本文整理自网络,侵删。

 
//------------------------------------------------------------------------------
// by [龟山]阿?d QQ:1467948783
// http://www.cnblogs.com/onechen/
//------------------------------------------------------------------------------

unit Main;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,

  {$IFDEF MSWINDOWS}
  Winapi.Windows,
  {$ENDIF}

  {$IFDEF ANDROID}
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  FMX.Helpers.Android,
  Androidapi.Helpers, // XE7 需要引入
  {$ENDIF}
  {$IFDEF IOS}
  FMX.Platform.iOS,
  iOSapi.Foundation,
  Macapi.ObjectiveC,
  {$ENDIF}

  {$IFDEF MACOS}
  FMX.Platform.Mac,
  Macapi.Foundation,
  Macapi.ObjectiveC,
  {$ENDIF}

  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.Memo, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    ToolBar1: TToolBar;
    Label1: TLabel;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

{$IFDEF MSWINDOWS}
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;
{$ENDIF}

procedure TForm1.FormCreate(Sender: TObject);
{$IFDEF MSWINDOWS}
begin
     Memo1.BeginUpdate;

     Memo1.Lines.Add('OS : Windows');
     Memo1.Lines.Add('ver : ' + GetBuildInfoAsString);

     Memo1.EndUpdate;
end;
{$ENDIF}

{$IFDEF ANDROID}
var PackageInfo: JPackageInfo;
    PackageName: JString;
begin
     Memo1.BeginUpdate;

     Memo1.Lines.Add('OS : Android');
     Memo1.Lines.Add('applicationLabel : ' + GetApplicationTitle);

     PackageName := SharedActivityContext.getPackageName;
     Memo1.Lines.Add('packageName : ' + JStringToString(PackageName));

     PackageInfo := SharedActivityContext.getPackageManager.getPackageInfo(PackageName, 0);
     Memo1.Lines.Add('versionName : ' + JStringToString(PackageInfo.versionName));

     Memo1.EndUpdate;
end;
{$ENDIF}

{$IF Defined(IOS) or Defined(MACOS)}
var AppNameKey: Pointer;
    AppBundle: NSBundle;
    NSAppName: NSString;
begin
     Memo1.BeginUpdate;

     Memo1.Lines.Add('OS : iOS');
     AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);

     AppNameKey := (NSSTR('CFBundleName') as ILocalObject).GetObjectID;
     NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppNameKey));
     Memo1.Lines.Add('CFBundleName : ' + UTF8ToString(NSAppName.UTF8String));

     AppNameKey := (NSSTR('CFBundleDisplayName') as ILocalObject).GetObjectID;
     NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppNameKey));
     Memo1.Lines.Add('CFBundleDisplayName : ' + UTF8ToString(NSAppName.UTF8String));

     AppNameKey := (NSSTR('CFBundleIdentifier') as ILocalObject).GetObjectID;
     NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppNameKey));
     Memo1.Lines.Add('CFBundleIdentifier : ' + UTF8ToString(NSAppName.UTF8String));

     AppNameKey := (NSSTR('CFBundleVersion') as ILocalObject).GetObjectID;
     NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppNameKey));
     Memo1.Lines.Add('CFBundleVersion : ' + UTF8ToString(NSAppName.UTF8String));

     Memo1.EndUpdate;
end;
{$ENDIF}

end.

相关阅读 >>

Delphi根据不同分隔符获取字符串内容

Delphi 遍历pe文件

Delphi 程序窗体及控件自适应分辨率

Delphi firemonkey的tedit七大变化

Delphi 如何将图片转换成文本

Delphi thread类的创建及使用(关于线程函数的传递例子)

Delphi 中的颜色

Delphi 实现打开文件定位

Delphi 每年、月、周、日的开始与结束的时间

为什么编程是独一无二的职业

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...