[英]How to detect if the computer is 32-bit or 64-bit?
如何确定你所在的计算机是 32 位机器还是 64 位机器?
我最好在 vba 中完成此操作。
我认为最直接的方法是:
#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
高温高压
@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
警告:
为了兼容不支持这个function的操作系统,调用GetProcAddress来检测Kernel32.dll中是否实现了IsWow64Process。 如果 GetProcAddress 成功,调用这个 function 是安全的。 否则,WOW64 不存在。 Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function.
http://msdn.microsoft.com/en-us/library/ms684139%28v=vs.85%29.aspx
从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
要确定正在运行的 Office 是 64 位还是 32 位:使用 IsWow64Process(Jean-François Corbett 的回答)。
要确定 Windows 是 64 位还是 32 位:
Public Function isWin64bit() As Boolean
isWin64bit = 0 < Len(Environ("ProgramW6432"))
End Function
条件编译可能非常有用, 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
我认为 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.