如何检测计算机是32位还是64位?

4

如何确定您所使用的计算机是32位还是64位?

最好使用vba进行操作。


1
你为什么需要它?这可以通过使用#IfWin64 #Else #End If来确定编译相关版本并使用公共变量Win = 64或Win = 32来实现。 - user688334
@osknows:假设OP使用的是Office 2010。VBA7Win64编译常量是在那个版本的Office中引入的。 - Jean-François Corbett
但我现在可以看到,OP接受了您的这个答案,所以我猜测它是Office 2010。 - Jean-François Corbett
6个回答

4
@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


是的,看起来更好了。已经太久了!;-) - Wouter Simons

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


1
NB: Option Compare Database 只能在 Access-VBA 中使用。除此之外似乎可以正常工作;在我的电脑上可以正确返回 False。+1 - Jean-François Corbett

2

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

要确定 Windows 是 64 位还是 32 位:

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

2

我认为最简单的方式是:

#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软件。 - Mike T
2
第二个部分显然是不正确的。#Win64编译器常量表示您正在运行64位办公室,如果是这样,#VBA7也总是正确的。#VBA7常量表示您正在运行Office 2010或更高版本,并且与检测32位与64位无关。在Win 64和Office 32上运行此代码将返回“Win 32和Office 32”。Falo的答案可用于区分在Office 32中运行时计算机是否为64位。 - Erik A
我不知道你的代码在测试什么。它回复说我的64位机器是32位的。 - webs

1

条件编译非常有用,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

0

我认为VBA可能与正在运行的Office版本相关联,而且真的很重要的是正在运行的进程类型。此代码片段可能会有所帮助(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未定义 =/ - if_zero_equals_one
GetCurrentProcess()也不是。 - if_zero_equals_one
你是否使用了带有声明的更新示例?该函数位于kernel32库(操作系统)中,可能无法在VBA代码中使用。相反,您需要创建一个VBScript文件并使用普通的VB6代码。 - Wouter Simons
我目前在宏中使用这个,但是我无法扩展超过那个范围。 - if_zero_equals_one
那么可能无法检测平台。您需要能够从内核dll中加载IsWow64Process函数。它可能在VBA中无法正常工作。 - Wouter Simons

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接