DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: cuiqingbo
今日帖子: 25
在线用户: 12
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 ma9888 (majx) ★☆☆☆☆ -
盒子活跃会员
2017/9/17 15:56:06
标题:
Delphi程序如何判断Windows是64位还是32位? 浏览:2513
加入我的收藏
楼主: 哪位高手请帮忙发个Delphi程序如何判断Windows是64位还是32位的代码 ?先谢谢了!!!
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2017/9/17 16:02:23
1楼: 可以判断 CPU。
if TOSVersion.Architecture = TOSVersion.TArchitecture.arIntelX64 then
----------------------------------------------
(C)(P)Flying Wang
作者:
男 lps (lps) ★☆☆☆☆ -
盒子活跃会员
2017/9/17 16:03:43
2楼: 1楼看清楚点吧!人家是问windows,不是CPU
----------------------------------------------
-
作者:
男 ma9888 (majx) ★☆☆☆☆ -
盒子活跃会员
2017/9/17 16:28:51
3楼: 谢谢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;

可是DELPHI编译时,下面这句的windows无法识别,我加了uses Winapi.Windows,还是不认识windows.
 IsWow64Process: function(Handle: Windows.THandle; var Res: Windows.BOOL): Windows.BOOL; stdcall;   
  GetNativeSystemInfo: procedure(var lpSystemInfo: TSystemInfo); stdcall;
错误提示如下:
[dcc64 Error] Unit7.pas(161): E2003 Undeclared identifier: 'Windows'
请问怎么解决呢?
----------------------------------------------
-
作者:
男 ma9888 (majx) ★☆☆☆☆ -
盒子活跃会员
2017/9/17 16:33:16
4楼: 去掉上面说的windows 编辑能通过,可是会判断错误,我的是Win64,判断出来是Win32.
----------------------------------------------
-
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2017/9/17 16:47:59
5楼: github上有个VersionHelpers
----------------------------------------------
-
作者:
男 hz_2009 (盒子) ★☆☆☆☆ -
普通会员
2017/9/18 9:23:21
6楼: //-- type declarations ----------
#ifndef _WIN64
typedef System::TMetaClass* TCanvasClass;
#else /* _WIN64 */
_DECLARE_METACLASS(System::TMetaClass, TCanvasClass);
#endif /* _WIN64 */

FMX.Graphics.hpp中这样用。
----------------------------------------------
-
作者:
男 djhfwk (djhfwk) ▲▲▲▲▲ -
普通会员
2017/9/18 9:46:18
7楼: 偷懒看看C:\Windows\SysWOW64在不在
----------------------------------------------
-
作者:
男 earthsbest (全能中间件) ▲▲▲▲△ -
普通会员
2017/9/18 9:48:59
7楼: 楼上的只是判断程序是64位还是32位。
其实一楼答案是对的。
----------------------------------------------
Delphi4Linux Delphi三层/FireDAC 技术群:734515869 http://www.cnblogs.com/rtcmw
作者:
男 lsu (lsu) ★☆☆☆☆ -
普通会员
2017/9/18 10:07:48
8楼:   1#方法简单明了。

    XE2之后定义了 System.SysUtils.TOSVersion类.检查系统方面的信息无需自己编程了。

    

  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;
----------------------------------------------
-
作者:
男 wujunping (wujunping) ★☆☆☆☆ -
盒子活跃会员
2017/9/19 10:36:25
9楼: unit WinVerUtils;
{
#==========

# Name:        WinVerUtils.pas
# Author:      Aleksander Oven
# Created:     2007-02-25
# Last Change: 2007-02-25
# Version:     1.0

# Description:

  All about the version of the Windows OS.
  Reference: http://msdn2.microsoft.com/en-us/library/ms724451.aspx

# Warnings and/or special considerations:

  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;

end.
----------------------------------------------
-
作者:
男 lwcvod (lwcvod) ★☆☆☆☆ -
普通会员
2017/9/19 11:39:42
10楼: 关注下,用得着
----------------------------------------------
Delphi
作者:
男 lwcvod (lwcvod) ★☆☆☆☆ -
普通会员
2017/9/19 15:07:22
11楼: 我在几台机器上做了下实验

用,爱吃猪头肉的猫,,不知道现在还爱不爱吃猪头肉不
不看后悔的猫 ,,O(∩_∩)O,老猫,,

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;

用这个判断,,是可行的
在 WIN 7 64  和 WIN7 32  和 WIN2003 32下测试的没问题
----------------------------------------------
Delphi
作者:
男 lsu (lsu) ★☆☆☆☆ -
普通会员
2017/9/20 12:41:49
12楼: GetVersion和GetVersionExW都是调用的NtCurrentPeb这个函数,GetVersionExA还是调用GetVersionExW来实现。获得系统版本号,微软至于搞得这么啰嗦而且改来改去的?

从Windows8.1出来之后,GetVersionExW这个API,微软明文给废弃了,从Windows8.1开始之后(包括Windows10),这个API常规情况下就是返回6.2了。

但是win10 PEB里面的版本则是6.4。所以使用GetVersionExW已经不准了。
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行93.75毫秒 RSS