频道分类

Delphi android windows IOS通用获取程序版本

作者:admin 来源: 日期:2019/10/8 10:49:44 人气: 标签:

 
{$IFDEF MSWINDOWS}

uses Winapi.Windows, ShlObj, FMX.PlatForm.Win, ShellAPI;
{$ENDIF}
{$IFDEF ANDROID}

uses Androidapi.JNI.JavaTypes, Androidapi.Helpers,
  Androidapi.JNI.GraphicsContentViewText;
{$ENDIF}

//上面引用仅供参考

{$IFDEF ANDROID}

function GetAppVersion: String;
var
  PackageManager: JPackageManager;
  PackageInfo: JPackageInfo;
begin
  PackageManager := TAndroidHelper.Context.getPackageManager;
  PackageInfo := PackageManager.getPackageInfo
    (TAndroidHelper.Activity.getPackageName, 0);
  Result := JStringToString(PackageInfo.versionName);
end;
{$ENDIF}
// {$IFDEF MACOS}
// begin
// Result := '';
// end;
// {$ENDIF}

{$IFDEF IOS}

function GetAppVersion: String;
begin
  Result := string(TNSString.Wrap(CFBundleGetValueForInfoDictionaryKey
    (CFBundleGetMainBundle, kCFBundleVersionKey)).UTF8String);
end;
{$ENDIF}
{$IFDEF MSWINDOWS}

function GetAppVersion: String;
const
  Fmt = '%d.%d.%d.%d';

var
  sFileName: String;
  iBufferSize: DWORD;
  iDummy: DWORD;
  pBuffer: Pointer;
  pFileInfo: Pointer;
  iVer: array [1 .. 4] of Word;
begin
  // set default value
  Result := '';

  // prepare buffer for path and terminating #0
  SetLength(sFileName, MAX_PATH + 1);
  SetLength(sFileName, GetModuleFileName(hInstance, PChar(sFileName),
    MAX_PATH + 1));

  // get size of version info (0 if no version info exists)
  iBufferSize := GetFileVersionInfoSize(PChar(sFileName), iDummy);
  if (iBufferSize > 0) then
  begin
    GetMem(pBuffer, iBufferSize);
    try
      // get fixed file info (language independent)
      GetFileVersionInfo(PChar(sFileName), 0, iBufferSize, pBuffer);
      VerQueryValue(pBuffer, '\', pFileInfo, iDummy);
      // read version blocks
      iVer[1] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
      iVer[2] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionMS);
      iVer[3] := HiWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
      iVer[4] := LoWord(PVSFixedFileInfo(pFileInfo)^.dwFileVersionLS);
    finally
      FreeMem(pBuffer);
    end;
    // format result string
    Result := Format(Fmt, [iVer[1], iVer[2], iVer[3], iVer[4]]);
  end;
end;
{$ENDIF}