为了检测Windows操作系统是32位还是64位的,用MASM32编写了一个调用Windows API函数GetNativeSystemInfo的程序(完整代码附后),GetNativeSystemInfo函数会将指向SYSTEM_INFO结构体的地址存到GetNativeSystemInfo传递的参数中。
微软官网中的STEM_INFO定义为:
typedef struct _SYSTEM_INFO {
union {
DWORD dwOemId;
struct {
WORD wProcessorArchitecture;
WORD wReserved;
} DUMMYSTRUCTNAME;
} DUMMYUNIONNAME;
DWORD dwPageSize;
LPVOID lpMinimumApplicationAddress;
LPVOID lpMaximumApplicationAddress;
DWORD_PTR dwActiveProcessorMask;
DWORD dwNumberOfProcessors;
DWORD dwProcessorType;
DWORD dwAllocationGranularity;
WORD wProcessorLevel;
WORD wProcessorRevision;
} SYSTEM_INFO, *LPSYSTEM_INFO;
SYSTEM_INFO中的wProcessorArchitecture存储了已安装操作系统的处理器体系结构。
访问代码为:
mov eax, g_lpSI
mov ax, (SYSTEM_INFO ptr [eax]).DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture
and eax, 0ffffh
.if (eax==PROCESSOR_ARCHITECTURE_AMD64) || (eax==PROCESSOR_ARCHITECTURE_IA64) || (eax==PROCESSOR_ARCHITECTURE_ARM64)
mov eax, OFFSET g_sz64Bit
.else
mov eax, OFFSET g_sz32Bit
.endif
需要注意的两点,一是wProcessorArchitecture是word型数据,只有16位。二是MASM32中没有定义PROCESSOR_ARCHITECTURE_ARM64,在微软网官上其值为12。
PROCESSOR_ARCHITECTURE_ARM64 equ 12 ;ARM64
但是在连接程序时总是提示 32or64.asm(54) : error A2006: undefined symbol : u
第54行代码就是
mov ax, (SYSTEM_INFO ptr [eax]).DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture
里面并没有使用符号u。
MASM32中的SYSTEM_INFO结构定义存在windows.inc中,居然跟微软官网上的不同:
SYSTEM_INFO STRUCT
wProcessorArchitecture WORD ?
wReserved WORD ?
dwPageSize DWORD ?
lpMinimumApplicationAddress DWORD ?
lpMaximumApplicationAddress DWORD ?
dwActiveProcessorMask DWORD ?
dwNumberOfProcessors DWORD ?
dwProcessorType DWORD ?
dwAllocationGranularity DWORD ?
wProcessorLevel WORD ?
wProcessorRevision WORD ?
SYSTEM_INFO ENDS
MASM32中的SYSTEM_INFO结构定义代码中省略了微软官网其中的DUMMYUNIONNAME联合体和DUMMYSTRUCTNAME结构体,难怪使用.DUMMYUNIONNAME.DUMMYSTRUCTNAME.wProcessorArchitecture来访问在连接时会出错,奇怪的是出错信息里没有提示DUMMYUNIONNAME.DUMMYSTRUCTNAME未定义,而是莫明奇妙的u
将第54行代码改为
mov ax, (SYSTEM_INFO ptr [eax]).wProcessorArchitecture
后顺利完成连接,生成了EXE文件,但是更大的麻烦来了……
附完整·代码:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; 32or64.asm
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.586
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
;sssssssssssssssssssssssssssssssssssssss
;.const
;sssssssssssssssssssssssssssssssssssssss
;PROCESSOR_ARCHITECTURE_INTEL equ 0 ;x86
;PROCESSOR_ARCHITECTURE_ARM equ 5 ;ARM
;PROCESSOR_ARCHITECTURE_IA64 equ 6 ;基于 Intel Itanium 的
;PROCESSOR_ARCHITECTURE_AMD64 equ 9 ;x64 (AMD 或 Intel)
PROCESSOR_ARCHITECTURE_ARM64 equ 12 ;ARM64
;PROCESSOR_ARCHITECTURE_UNKNOWN equ 0ffffh ;未知体系结构。
_SYSTEM_INFO STRUCT
UNION
dwOemId DWORD ?
STRUCT
wProcessorArchitecture WORD ?
wReserved WORD ?
ENDS
ENDS
dwPageSize DWORD ?
lpMinimumApplicationAddress DWORD ?
lpMaximumApplicationAddress DWORD ?
dwActiveProcessorMask DWORD ?
dwNumberOfProcessors DWORD ?
dwProcessorType DWORD ?
dwAllocationGranularity DWORD ?
wProcessorLevel WORD ?
wProcessorRevision WORD ?
_SYSTEM_INFO ENDS
;sssssssssssssssssssssssssssssssssssssss
.data
;sssssssssssssssssssssssssssssssssssssss
g_szCaption db "Windows",0
g_sz32Bit db "32位",0
g_sz64Bit db "32位",0
g_szGetNativeSystemInfo db "GetNativeSystemInfo", 0
g_szkernel32Dll db "kernel32.dll", 0
g_szFailGetModuleHandle db "GetModuleHandle失败", 0
g_szFailGetProcAddress db "GetProcAddress失败", 0
g_lpfnGetNativeSystemInfo dword ?
g_lpSI dword ? ; SYSTEM_INFO
;sssssssssssssssssssssssssssssssssssssss
.code
;sssssssssssssssssssssssssssssssssssssss
start:
invoke GetModuleHandle, OFFSET g_szkernel32Dll
.if eax==NULL
mov eax, OFFSET g_szFailGetModuleHandle
.else
invoke GetProcAddress, eax, OFFSET g_szGetNativeSystemInfo
.IF eax==NULL
mov eax, OFFSET g_szFailGetProcAddress
.ELSE
mov g_lpfnGetNativeSystemInfo, eax
push OFFSET g_lpSI
call g_lpfnGetNativeSystemInfo
mov eax, g_lpSI
mov ax, (SYSTEM_INFO ptr [eax]).wProcessorArchitecture
and eax, 0ffffh
;mov eax, (_SYSTEM_INFO ptr [eax]).dwOemId
.if (eax==PROCESSOR_ARCHITECTURE_IA64) || (eax==PROCESSOR_ARCHITECTURE_AMD64) || (eax==PROCESSOR_ARCHITECTURE_ARM64)
mov eax, OFFSET g_sz64Bit
.else
mov eax, OFFSET g_sz32Bit
.endif
.ENDIF
.endif
invoke MessageBox, NULL, eax, OFFSET g_szCaption, MB_OK
invoke ExitProcess, NULL
end start