谢谢1楼和2楼的朋友! http://blog.csdn.net/atian2009/article/details/7433006 在网上看到 如下代码: delphi判断windows系统是64位还是32位 function IsWin64: boolean; var Kernel32Handle: THandle; IsWow64Process: function(Handle: Windows.THandle; var Res: Windows.BOOL): Windows.BOOL; stdcall; GetNativeSystemInfo: procedure(var lpSystemInfo: TSystemInfo); stdcall; isWoW64: Bool; SystemInfo: TSystemInfo; const PROCESSOR_ARCHITECTURE_AMD64 = 9; PROCESSOR_ARCHITECTURE_IA64 = 6; begin Result := False; Kernel32Handle := GetModuleHandle('KERNEL32.DLL'); if Kernel32Handle = 0 then Kernel32Handle := LoadLibrary('KERNEL32.DLL'); if Kernel32Handle <> 0 then begin IsWOW64Process := GetProcAddress(Kernel32Handle,'IsWow64Process'); //GetNativeSystemInfo函数从Windows XP开始才有,而IsWow64Process函数从Windows XP SP2以及Windows Server 2003 SP1开始才有。 //所以使用该函数前最好用GetProcAddress。 GetNativeSystemInfo := GetProcAddress(Kernel32Handle,'GetNativeSystemInfo'); if Assigned(IsWow64Process) then begin IsWow64Process(GetCurrentProcess,isWoW64); Result := isWoW64 and Assigned(GetNativeSystemInfo); if Result then begin GetNativeSystemInfo(SystemInfo); Result := (SystemInfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64) or (SystemInfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_IA64); end; end; end; end;
TOSVersion = record public type TArchitecture = (arIntelX86, arIntelX64, arARM32, arARM64); TPlatform = (pfWindows, pfMacOS, pfiOS, pfAndroid, pfWinRT, pfLinux); public const AllArchitectures = [arIntelX86, arIntelX64, arARM32, arARM64]; AllPlatforms = [pfWindows, pfMacOS, pfiOS, pfAndroid, pfWinRT, pfLinux]; private class var FArchitecture: TArchitecture; class var FBuild: Integer; class var FMajor: Integer; class var FMinor: Integer; class var FName: string; class var FPlatform: TPlatform; class var FServicePackMajor: Integer; class var FServicePackMinor: Integer; {$IFDEF LINUX} class var FPrettyName: string; class var FLibCVersionMajor: Integer; class var FLibCVersionMinor: Integer; {$ENDIF LINUX} class constructor Create; public class function Check(AMajor: Integer): Boolean; overload; static; inline; class function Check(AMajor, AMinor: Integer): Boolean; overload; static; inline; class function Check(AMajor, AMinor, AServicePackMajor: Integer): Boolean; overload; static; inline; class function ToString: string; static; class property Architecture: TArchitecture read FArchitecture; class property Build: Integer read FBuild; class property Major: Integer read FMajor; class property Minor: Integer read FMinor; class property Name: string read FName; class property Platform: TPlatform read FPlatform; class property ServicePackMajor: Integer read FServicePackMajor; class property ServicePackMinor: Integer read FServicePackMinor; {$IFDEF LINUX} class property PrettyName: string read FPrettyName; class property LibCVersionMajor: Integer read FLibCVersionMajor; class property LibCVersionMinor: Integer read FLibCVersionMinor; {$ENDIF LINUX} end;
----------------------------------------------
-
Source code in this file is free for personal and commercial use.
#========== } interface
function GetWindowsVersionString: String; function GetWinVersion: String; function IsWin64: boolean;
implementation
uses Windows, SysUtils,CommCtrl;
type TOSVersionInfoExA = packed record dwOSVersionInfoSize: DWORD; dwMajorVersion: DWORD; dwMinorVersion: DWORD; dwBuildNumber: DWORD; dwPlatformId: DWORD; szCSDVersion: array[0..127] of AnsiChar; wServicePackMajor: WORD; wServicePackMinor: WORD; wSuiteMask: WORD; wProductType: Byte; wReserved: Byte; end;
function GetVersionExA(lpVersionInformation: Pointer): BOOL; stdcall; external kernel32 name 'GetVersionExA';
function GetWindowsVersionString: String; var VI: TOSVersionInfoA; begin VI.dwOSVersionInfoSize := SizeOf(TOSVersionInfoA); if GetVersionExA(@VI) then with VI do Result := Trim( Format( '%d.%d build %d %s', [dwMajorVersion, dwMinorVersion, dwBuildNumber, szCSDVersion] ) ) else Result := ''; end;
function IsWin64: boolean; var Kernel32Handle: THandle; IsWow64Process: function(Handle: windows.THandle; var Res: windows.BOOL) : windows.BOOL; stdcall; GetNativeSystemInfo: procedure(var lpSystemInfo: TSystemInfo); stdcall; isWoW64: BOOL; SystemInfo: TSystemInfo; const PROCESSOR_ARCHITECTURE_AMD64 = 9; PROCESSOR_ARCHITECTURE_IA64 = 6; begin Result := false; Kernel32Handle := GetModuleHandle('KERNEL32.DLL'); if Kernel32Handle = 0 then Kernel32Handle := LoadLibrary('KERNEL32.DLL'); if Kernel32Handle <> 0 then begin IsWow64Process := GetProcAddress(Kernel32Handle, 'IsWow64Process');
GetNativeSystemInfo := GetProcAddress(Kernel32Handle, 'GetNativeSystemInfo'); if Assigned(IsWow64Process) then begin IsWow64Process(GetCurrentProcess, isWoW64); Result := isWoW64 and Assigned(GetNativeSystemInfo); if Result then begin GetNativeSystemInfo(SystemInfo); Result := (SystemInfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64) or (SystemInfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_IA64); end; end; end; end;
function GetWinVersion: String; //读取操作系统版本 var AWin32Version: Extended; VIEx: TOSVersionInfoExA; os:string;
begin os:='Windows '; AWin32Version := StrtoFloat(format('%d.%d' ,[Win32MajorVersion, Win32MinorVersion])); if Win32Platform=VER_PLATFORM_WIN32s then Result := os + '32' else if Win32Platform=VER_PLATFORM_WIN32_WINDOWS then begin if AWin32Version=4.0 then Result := os + '95' else if AWin32Version=4.1 then Result := os + '98' else if AWin32Version=4.9 then Result := os + 'Me' else Result := os + '9x' end else if Win32Platform = VER_PLATFORM_WIN32_NT then begin VIEx.dwOSVersionInfoSize := SizeOf(TOSVersionInfoExA); if not GetVersionExA(@VIEx) then VIEx.dwOSVersionInfoSize := 0;
if AWin32Version=3.51 then Result := os + 'NT 3.51' else if AWin32Version=4.0 then Result := os + 'NT 4.0' else if AWin32Version=5.0 then Result := os + '2000' else if AWin32Version=5.1 then Result := os + 'XP' else if AWin32Version=5.2 then begin if (VIEx.wProductType = VER_NT_WORKSTATION) and IsWin64 then Result := os + 'XP' ; if (GetSystemMetrics(SM_SERVERR2) = 0) then Result := os + 'Server 2003' else Result := os + 'Server 2003 R2'; if VIEx.wSuiteMask = VER_SUITE_WH_SERVER then Result := os + 'Home Server' ;
end else if AWin32Version=6.0 then begin if (VIEx.wProductType <> VER_NT_WORKSTATION) then Result := os + '2008' else Result := os + 'Vista' end
else if AWin32Version=6.1 then begin if (VIEx.wProductType <> VER_NT_WORKSTATION) then Result := os + 'Server 2008 R2' else Result := os + '7' end
else if AWin32Version=6.2 then begin if (VIEx.wProductType <> VER_NT_WORKSTATION) then Result := os + 'Server 2012' else Result := os + '8' end else if AWin32Version=6.3 then begin if (VIEx.wProductType <> VER_NT_WORKSTATION) then Result := os + 'Server 2012 R2' else Result := os + '8.1' end
else if AWin32Version=10 then begin if (VIEx.wProductType <> VER_NT_WORKSTATION) then Result := os + 'Server 2016' else Result := os + '10' end
else Result := os ; end else Result := os + '??'; if IsWin64 then Result:=Result + ' 64bit '+GetWIndowsVersionString else Result:=Result + ' '+GetWIndowsVersionString; end;
procedure TForm1.Button1Click(Sender: TObject); begin if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then ShowMessage('64'); if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX86 then ShowMessage('32'); end;