本文整理自网络,侵删。
介绍
本代码实现了程序在不同平台里得到当前应用的版本号,兼容:windows、ios、android、以及MACOS,通过{$编译指令来得到程序运行在什么平台上,然后调用不同的函数来实现。
具体代码
AndRoid得到版本号的代码:
{$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}
IOS得到版本号的代码:
{$IFDEF MACOS}
var AppNameKey: Pointer;
AppBundle: NSBundle;
NSAppName: NSString;
begin
Memo1.BeginUpdate;
{$IFDEF IOS}
Memo1.Lines.Add('OS : iOS');
{$ELSE}
Memo1.Lines.Add('OS : Msc');
{$ENDIF}
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}
windows下得到版本号的代码
{$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}
相关阅读 >>
Delphi strtofloat 将“字符型”转换成“浮点型”
Delphixe4 版本中,已针对移动平台 引入了 arc 模型
Delphi trestclient + trestrequest + trestresponse
Delphi now、date、time、currentyear - 当前日期
Delphi firedac 下的 sqlite [8] - 自定义函数
更多相关阅读请进入《Delphi》频道 >>