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 十进制十六进制转换

Delphi enumwindows 获取窗体句柄 进程id 窗体信息

Delphi savelog 方便的记录日志

Delphi 获取鼠标坐标

Delphi 10.x ide界面

Delphi of 打坐与普通攻击calll调用

Delphi twebbrowser get html source after ajax load

Delphi 简单方法搜索定位treeview项

Delphi webbrowser有关技术

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



打赏

取消

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

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

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

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

评论

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