2012年7月5日星期四

IsWow64Process.Asm


;本文改编自msdn.
;不知这个程序能否判断出cpu的位数,应该可以判断出操作系统的位数。
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib

.data?
buffer db 512 dup (0)

.code

hstdout dd 0
hstdin dd 0
x dd 0

bIsWow64 dd 0 
szWOW64 db "The process is running under WOW64",13,10,0
szwin32 db "The process is not running under WOW64",13,10,0
correy db "made by correy",0
szkernel32 db "kernel32.dll",0
szIsWow64Process db "IsWow64Process",0
szerr db "函数运行失败!",0
notice db "按enter键退出!",13,10,0

start:
invoke GetStdHandle,-10
mov hstdin,eax
invoke GetStdHandle,-11
mov hstdout,eax

invoke SetConsoleTitle,addr correy
invoke SetConsoleScreenBufferSize,hstdout,01000099h;高字是高度,低字是宽度。

invoke GetModuleHandle,offset szkernel32
invoke GetProcAddress,eax,addr szIsWow64Process
mov x,eax

invoke GetCurrentProcess
push offset bIsWow64
push eax
call x
.if eax == 1
  .if bIsWow64 == 1 ;32的程序运行在64位系统下返回这个。
    invoke WriteFile,hstdout,addr szWOW64,sizeof szWOW64-1,addr x,0
  .else ;运行在32位系统下,或者64位的程序运行在64的系统下。
    invoke WriteFile,hstdout,addr szwin32,sizeof szwin32-1,addr x,0
  .endif
.else
  invoke WriteFile,hstdout,addr szerr,sizeof szerr-1,addr x,0
.endif

invoke WriteFile,hstdout,addr notice,sizeof notice-1,addr x,0
invoke ReadFile,hstdin,addr buffer,sizeof buffer,addr x,0

invoke ExitProcess,0
end start
;made at 2011.09.16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;QQ:112426112
;Email:leguanyuan at 126 dot com
;Homepage:http://correy.webs.com
;本来已经写过这个函数,在Programming Applications for Microsoft Windows 第十四章程序例子代码的汇编实现:VirtualQuery.asm中。
;今天这个来自微软的msdn的例子,和上面他们的解释是不完全一样的。我稍加修改。
;我觉得,此函数可以判断你的cpu是64位的还是32位的。
;后来发现,我是错的,但这个编程的方法还可以学习借鉴,所有保留。
;再后来发现GetNativeSystemInfo函数可以完成此功能,用法与此相同,所以就不再写了。
;不当之处,敬请指教!
.386
.model flat,stdcall
option casemap:none

include windows.inc
include kernel32.inc
includelib kernel32.lib

include user32.inc
includelib user32.lib

.code
hstdout dd 0
hstdin dd 0
x dd 0

correy db "made by correy",0
entry db 13,10,0
align 4
psi SYSTEM_INFO <>
buffer db 512 dup (0)

sztitle db "以下是你电脑上的一些信息:",13,10,0    

szx86 db "ProcessorArchitecture:x86,当然啦!这个32位。",0 ;在c/c++中可以测试int的大小,在汇编中还可以用别的办法。
szia64 db "ProcessorArchitecture:Intel Itanium-based,当然啦!这个64位。",0
szamd64 db "ProcessorArchitecture:x64 (AMD or Intel),当然啦!这个64位。",0
szunknow db "ProcessorArchitecture:Unknown architecture",0

szPageSize db "Page size: %lxh",0
szMinimumApplicationAddress db "Minimum application address: %lxh",13,10,0
szMaximumApplicationAddress db "Maximum application address: %lxh",13,10,0
szActiveProcessorMask db "Active processor mask: %lxh",13,10,0
szNumberOfProcessors db "Number of processors: %lxh",13,10,0
szProcessorType db "Processor type: %lu",13,10,0
szAllocationGranularity db "AllocationGranularity: %lxh",13,10,0
szProcessorLevel db "ProcessorLevel: %xh",13,10,0
szProcessorRevision db "ProcessorRevision: %xh",13,10,0

notice db "按enter键退出!",13,10,0

start:
invoke GetStdHandle,-10
mov hstdin,eax
invoke GetStdHandle,-11
mov hstdout,eax

invoke SetConsoleTitle,addr correy
invoke SetConsoleScreenBufferSize,hstdout,01000064h;高字是高度,低字是宽度。

invoke WriteFile,hstdout,addr sztitle,sizeof sztitle-1,addr x,0

invoke GetSystemInfo,addr psi

.if word ptr psi == 0
  invoke WriteFile,hstdout,addr szx86,sizeof szx86-1,addr x,0
  invoke WriteFile,hstdout,addr entry,2,addr x,0
.elseif word ptr psi == 6
  invoke WriteFile,hstdout,addr szia64,sizeof szia64-1,addr x,0
  invoke WriteFile,hstdout,addr entry,2,addr x,0
.elseif word ptr psi == 9
  invoke WriteFile,hstdout,addr szamd64,sizeof szamd64-1,addr x,0
  invoke WriteFile,hstdout,addr entry,2,addr x,0
.else ;word ptr psi == 0ffffh
  invoke WriteFile,hstdout,addr szunknow,sizeof szunknow-1,addr x,0
  invoke WriteFile,hstdout,addr entry,2,addr x,0
.endif

invoke wsprintf,addr buffer,addr szPageSize,psi.dwPageSize
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0
invoke WriteFile,hstdout,addr entry,2,addr x,0

invoke wsprintf,addr buffer,addr szMinimumApplicationAddress,psi.lpMinimumApplicationAddress
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

invoke wsprintf,addr buffer,addr szMaximumApplicationAddress,psi.lpMaximumApplicationAddress
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

invoke wsprintf,addr buffer,addr szActiveProcessorMask,psi.dwActiveProcessorMask
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

invoke wsprintf,addr buffer,addr szNumberOfProcessors,psi.dwNumberOfProcessors
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

invoke wsprintf,addr buffer,addr szProcessorType,psi.dwProcessorType
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

invoke wsprintf,addr buffer,addr szAllocationGranularity,psi.dwAllocationGranularity
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

mov eax,0
mov ax,psi.wProcessorLevel
invoke wsprintf,addr buffer,addr szProcessorLevel,eax;psi.wProcessorLevel ;这样获取成功,但不会显示。
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

mov eax,0
mov ax,psi.wProcessorRevision
invoke wsprintf,addr buffer,addr szProcessorRevision,eax;psi.wProcessorRevision
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0

invoke WriteFile,hstdout,addr notice,sizeof notice-1,addr x,0
invoke ReadFile,hstdin,addr buffer,sizeof buffer,addr x,0
invoke ExitProcess,0
end start
;made at 2011.09.01
;扩展阅读,请看以下函数:
;IsProcessorFeaturePresent
;GetLogicalProcessorInformation GetLogicalProcessorInformationEx

没有评论:

发表评论