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

时间:2011-06-06 18:01:40

标签: vba x86 x86-64

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

我需要在vba中完成这项工作。

6 个答案:

答案 0 :(得分:3)

@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

答案 1 :(得分:3)

我认为最简单的方法是:

#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

答案 2 :(得分: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

答案 3 :(得分:1)

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

确定Windows是64位还是32位:

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

答案 4 :(得分: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

答案 5 :(得分:0)

我认为VBA可能与正在运行的办公室版本相关联,而且正在运行的进程类型非常重要。此代码段可能有帮助(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