簡體   English   中英

如何檢測計算機是32位還是64位?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM