【问题标题】:How to detect if the computer is 32-bit or 64-bit?如何检测计算机是32位还是64位?
【发布时间】:2016-03-12 00:23:08
【问题描述】:

如何判断你所在的电脑是 32 位机还是 64 位机?

我最好在 vba 中完成。

【问题讨论】:

  • 你为什么想要它?这可以使用#IfWin64 #Else #End If 来编译相关版本并使用公共变量 Win = 64 或 Win = 32 来确定。
  • @osknows: ... 假设 OP 有 Office 2010。VBA7Win64 编译常量是在该版本的 office 中引入的。
  • ...但我现在可以看到 OP 接受了你的 this answer,所以我想可以安全地假设它是 Office 2010。

标签: vba x86 x86-64


【解决方案1】:

@Wouter Simon 的回答有点正确,但确实不完整。它缺少一些Declare 语句以及某种解释。

因此,我认为值得在这里展示一个更完整、更有效的版本。

Private Declare Function GetProcAddress Lib "kernel32" _
    (ByVal hModule As Long, _
    ByVal lpProcName As String) As Long

Private Declare Function GetModuleHandle Lib "kernel32" _
    Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long '()

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function IsWow64Process Lib "kernel32" _
    (ByVal hProcess As Long, ByRef Wow64Process As Long) As Long

Sub CheckWhetherIts64()

    Dim Its64 As Long
    Dim handle As Long

    handle = GetProcAddress(GetModuleHandle("kernel32"), _
                   "IsWow64Process")

    If handle > 0 Then ' IsWow64Process function exists
        ' Now use the function to determine if
        ' we are running under Wow64

        IsWow64Process GetCurrentProcess(), Its64
    End If
    If Its64 = 1 Then
        MsgBox "it's a 64 bit process."
    End If
End Sub

警告:

为了兼容不支持该功能的操作系统,调用GetProcAddress来检测Kernel32.dll中是否实现了IsWow64Process。如果 GetProcAddress 成功,则调用此函数是安全的。否则,WOW64 不存在。请注意,此技术不是检测操作系统是否为 64 位版本 Windows 的可靠方法,因为当前 32 位 Windows 版本中的 Kernel32.dll 也包含此功能。

http://msdn.microsoft.com/en-us/library/ms684139%28v=vs.85%29.aspx

【讨论】:

  • 是的,看起来更好。时间太长了! ;-)
【解决方案2】:

http://www.msoffice.us/Access/PDF/Extending%20VBA%20with%20APIs.pdf。好像对我有用。

Option Compare Database

Type SYSTEM_INFO
wProcessorArchitecture As Integer
wReserved As Integer
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOrfProcessors As Long
dwProcessorType As Long
dwAllocationGranularity As Long
wProcessorLevel As Integer
wProcessorRevision As Integer
End Type

Declare Sub GetNativeSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
Declare Function GetCurrentProcess Lib "kernel32" () As Long

Public Function Is64BitProcessor() As Boolean
Const PROCESSOR_ARCHITECTURE_AMD64 As Integer = 9
Const PROCESSOR_ARCHITECTURE_IA64 As Integer = 6
Dim si As SYSTEM_INFO
' call the API
GetNativeSystemInfo si
' check the struct
Is64BitProcessor = (si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64 _
Or _
si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_IA64)
End Function

http://msdn.microsoft.com/en-us/library/ms724340(v=vs.85).aspx

【讨论】:

  • 注意:Option Compare Database 仅适用于 Access-VBA。否则似乎工作正常;在我的电脑上正确返回False。 +1
【解决方案3】:

要确定正在运行的 Office 是 64 位还是 32 位: 使用 IsWow64Process(Jean-François Corbett 的回答)。

判断 Windows 是 64 位还是 32 位:

Public Function isWin64bit() As Boolean
  isWin64bit = 0 < Len(Environ("ProgramW6432"))
End Function

【讨论】:

    【解决方案4】:

    我认为最直接的方法是:

    #If Win64 Then
        MsgBox "Win 64"
    #Else
        MsgBox "Win 32"
    #End If
    

    有时检查您的 Office 是 32 还是 64 并使用此信息访问注册表中的正确键也很有用。所以你可以这样做:

    #If Win64 Then
        #If VBA7 Then
            MsgBox "Win 64 and Office 64" ' HKEY_LOCAL_MACHINE\SOFTWARE\YourApp
        #Else
            MsgBox "Win 64 and Office 32" ' HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\YourApp
        #End If
    #Else
        MsgBox "Win 32 and Office 32" ' HKEY_LOCAL_MACHINE\SOFTWARE\YourApp
    #End If
    

    HTH

    【讨论】:

    • 条件“Win 32”不一定为真,因为在 64 位 Windows 上找到 32 位 Excel 很常见。
    • 第二部分显然不正确。 #Win64 编译器常量表示您正在运行 64 位办公室,如果这是真的 #VBA7 也总是如此。 #VBA7 常量表示您正在运行 Office 2010 或更高版本,并且与检测 32 位和 64 位完全无关。运行此代码在 Win 64 和 Office 32 上返回 "Win 32 and Office 32"。Falo 的答案可用于区分在 Office 32 中运行时计算机是否为 64 位。
    • 我不知道你的代码在测试什么。它正在回复我的 64 位机器是 32。
    【解决方案5】:

    条件编译可能非常有用,WinXX 检测环境但不检测硬件属性,示例如下:

       Dim mVers   As String
    
    Sub Init()
    
        #If Win64 Then
            mVers = "Win64" ' Win64=true, Win32=true, Win16= false
            Call VerCheck
        #ElseIf win32 Then
            mVers = "Win32"  ' Win32=true, Win16=false
            Call VerCheck
        #ElseIf win16 Then
            mVers = "Win16"  ' Win16=true
            Call VerCheck
        #End If
    
    End Sub
    
    Sub VerCheck()
        MsgBox "Version: " & mVers, vbInformation, "Version"
    End Sub
    

    【讨论】:

      【解决方案6】:

      我认为 VBA 可能与正在运行的 Office 版本相关联,并且运行哪种类型的进程非常重要。此代码 sn-p 可能会有所帮助(VB6 代码)

      Private Declare Function GetProcAddress Lib "kernel32" _
          (ByVal hModule As Long, _
          ByVal lpProcName As String) As Long
      
      Private Declare Function GetModuleHandle Lib "kernel32" _
          Alias "GetModuleHandleA" _
      
      handle = GetProcAddress(GetModuleHandle("kernel32"), _
                     "IsWow64Process")
      
      If handle > 0 Then ' IsWow64Process function exists
          ' Now use the function to determine if 
          ' we are running under Wow64
          IsWow64Process GetCurrentProcess(), bolFunc
      End If
      

      【讨论】:

      • GetModuleHandle 未定义 =/
      • GetCurrentProcess() 也不是
      • 您是否在声明中使用了更新后的示例?该函数位于 kernel32 lib (OS) 中,在 VBA 代码中可能不可用。相反,您必须制作一个 vbscript 文件并使用普通的 VB6 代码。
      • 我目前在宏中使用它,我无法扩展它
      • 那么可能检测不到平台。您需要能够从内核 dll 加载 IsWow64Process 函数。它可能在 VBA 中不起作用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2010-10-10
      • 2014-07-04
      • 2015-12-03
      相关资源
      最近更新 更多