kernel32 GetSystemInfo not returning info



在下面的代码中调用GetSystemInfo后,所有的SI字段都是0,这就是我在这里试图解决的问题。

这是关于相同的代码作为几个例子,可以找到一个快速的网络搜索GetSystemInfo,所以我不认为代码本身是错误的:

Private Structure SYSTEM_INFO
Public dwOemID As Long
Public dwPageSize As Long
Public lpMinimumApplicationAddress As Long
Public lpMaximumApplicationAddress As Long
Public dwActiveProcessorMask As Long
Public dwNumberOfProcessors As Long
Public dwProcessorType As Long
Public dwAllocationGranularity As Long
Public wProcessorLevel As Integer
Public wProcessorRevision As Integer
End Structure
Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
Private Sub GetProcessorInfo()
Dim SI As SYSTEM_INFO
Dim tmp As String
Call GetSystemInfo(SI)   
Select Case SI.dwProcessorType
Case PROCESSOR_INTEL_386 : tmp = "386"
Case PROCESSOR_INTEL_486 : tmp = "486"
Case PROCESSOR_INTEL_PENTIUM : tmp = "Pentium"
Case PROCESSOR_MIPS_R4000 : tmp = "MIPS 4000"
Case PROCESSOR_ALPHA_21064 : tmp = "Alpha"
End Select    
Select Case SI.wProcessorLevel
Case PROCESSOR_LEVEL_80386 : tmp = "Intel 80386"
Case PROCESSOR_LEVEL_80486 : tmp = "Intel 80486"
Case PROCESSOR_LEVEL_PENTIUM : tmp = "Intel Pentium"
Case PROCESSOR_LEVEL_PENTIUMII : tmp = "Intel Pentium Pro, II, III or 4"
End Select
End Sub

我在单元测试的调试会话中测试此代码。操作系统为Windows 10, VS Enterprise 2019。

编辑:代码由VB6转换为VB。Net中,Structure可能有问题,它最初是Type,或者包含数据的类型,或者传递给GetSystemInfo的方式。

您的定义中有许多错误

  • DWORD应该是Integer
  • WORD应该是Short
  • LPVOIDDWORD_PTR应该是IntPtr
  • 你需要传递一个引用到你的结构,使用<Out()> ByRef
Private Structure SYSTEM_INFO
Public dwOemID As Integer
Public dwPageSize As Integer
Public lpMinimumApplicationAddress As IntPtr
Public lpMaximumApplicationAddress As IntPtr
Public dwActiveProcessorMask As IntPtr
Public dwNumberOfProcessors As Integer
Public dwProcessorType As Integer
Public dwAllocationGranularity As Integer
Public wProcessorLevel As Short
Public wProcessorRevision As Short
End Structure
Private Declare Sub GetSystemInfo Lib "kernel32" (<Out()> ByRef lpSystemInfo As SYSTEM_INFO)

最新更新