2010年7月14日

実行環境のアーキテクチャを取得する

実行環境のアーキテクチャはGetSystemInfo(ja)で取得したSYSTEM_INFO構造体のwProcessorArchitectureで知ることができるはずです。ところが実際には32bitのアプリケーションからは64bit版WindowsでもwProcessorArchitectureがPROCESSOR_ARCHITECTURE_INTEL(0)となってしまいます。GetSystemInfoの説明をよく見るとTo retrieve accurate information for an application running on WOW64, call the GetNativeSystemInfo function.ということでGetNativeSystemInfoを使用する必要があるようです。GetNativeSystemInfoはWindows XP/Server 2003以降でしか使えませんので、それ以前の環境ではGetSystemInfoを使用します。
uses
  Windows;

const
  PROCESSOR_ARCHITECTURE_AMD64   =     9;   // x64 (AMD or Intel)
  PROCESSOR_ARCHITECTURE_IA64    =     6;   // Intel Itanium Processor Family (IPF)
  PROCESSOR_ARCHITECTURE_INTEL   =     0;   // x86
  PROCESSOR_ARCHITECTURE_UNKNOWN = $FFFF;   // Unknown architecture

type
  TGetSystemInfoFunc = procedure (var lpSystemInfo: TSystemInfo); stdcall;

function GetProcessorArchitecture: Word;
var
  SI: TSystemInfo;
  GetSystemInfoFunc: TGetSystemInfoFunc;
begin

  FillChar(SI,SizeOf(SI),0);

  { Call GetNativeSystemInfo or GetSystemInfo }
  @GetSystemInfoFunc := GetProcAddress(GetModuleHandle(kernel32),
                                       'GetNativeSystemInfo');
  if Assigned(GetSystemInfoFunc) = True then
  begin
    GetSystemInfoFunc(SI);
  end
  else
  begin
    GetSystemInfo(SI);
  end;

  Result := SI.wProcessorArchitecture;

end;


2012/07/07追記: Delphi XE2以降であればSystem.SysUtilsユニットのTOSVersion.Architectureで実行環境のアーキテクチャを知ることができます。ねた元はZarko GajicさんのIs Your 32bit Delphi Applications Running On x86 (Win 32) OR x64 (Win 64)?

0 件のコメント: