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

Vba 如何检测计算机是32位还是64位?,vba,x86,x86-64,Vba,X86,X86 64,如何确定您所在的计算机是32位计算机还是64位计算机 我需要在vba中更好地完成此操作。我认为vba可能与正在运行的office版本相链接,而运行的进程类型确实很重要。此代码段可能有帮助(VB6代码) 从你那里得到的 . 好像它在我的身上起作用 Option Compare Database Type SYSTEM_INFO wProcessorArchitecture As Integer wReserved As Integer dwPageSize As Long lpMinimumAp

如何确定您所在的计算机是32位计算机还是64位计算机


我需要在vba中更好地完成此操作。

我认为vba可能与正在运行的office版本相链接,而运行的进程类型确实很重要。此代码段可能有帮助(VB6代码)

从你那里得到的 . 好像它在我的身上起作用

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

@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也包含此函数


要确定运行的Office是64位还是32位: 使用ISWOW64流程(Jean-François Corbett的回答)

要确定Windows是64位还是32位,请执行以下操作:

Public Function isWin64bit() As Boolean
  isWin64bit = 0 < Len(Environ("ProgramW6432"))
End Function
公共函数isWin64bit()为布尔值
isWin64bit=0
我认为最简单的方法是:

#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

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

GetModuleHandle未定义=/GetCurrentProcess()也未定义您是否将更新的示例用于声明?该函数位于内核32库(OS)中,在VBA代码中可能不可用。相反,您必须创建一个vbscript文件并使用普通的VB6代码。我目前正在宏中使用此代码,无法扩展超过此值,因此可能无法检测平台。您需要能够从内核dll加载ISWOW64进程函数。它在VBA中可能不起作用你为什么要它?这可以使用
#IfWin64
#Else
#End If
编译相关版本并使用公共变量Win=64或Win=32来确定。@osknows:。。。假设OP拥有Office 2010。该版本的office中引入了
VBA7
Win64
编译常量。。。。但我现在可以看到您的OP已被接受,因此我想可以安全地假设它是Office 2010。NB:
选项比较数据库
仅在Access VBA中有效。否则看起来效果不错;在我的计算机上正确返回
False
+是的,看起来好多了。太久了!;-)条件“win32”不一定是真的,因为在64位Windows上查找32位Excel是很常见的。第二部分显然是不正确的。#Win64编译器常量表示您正在运行64位office,如果是这样的话#VBA7也总是这样。#VBA7常量表示您正在运行Office 2010或更高版本,与检测32位与64位完全无关。运行此代码会在Win 64和Office 32上返回“Win 32和Office 32”。Falo的答案可以用来区分计算机在Office 32中运行时是否为64位。我不知道您的代码正在测试什么。我的64位机器是32位。
   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