RSS订阅 | 匿名投稿
您的位置:网站首页 > 程序 > 正文

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

作者:admin 来源: 日期:2015/4/26 21:44:45 人气: 标签:

//------------------------------------------------------------------------------
// by [龟山]阿卍 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.

读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址:
下一篇:没有资料