前两天为了写个流量监控的程序,网上的太那个了。。。很不爽,才结识WMI,嘿嘿,研究了一下,还行!!接口找到了,再调用其它的又有何难?
本机显示结果如下:
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;Win32汇编调用WMI (COM组件) 得到name和ProcessorId (抛砖引玉).
; ___By G-Spider 2010-9-19
;Web: http://blog.csdn.net/G_Spider
;QQ : 287014897
;----------------------------------------------------------
;ml /c /coff my.asm
;link /subsystem:console my.obj
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
include user32.inc
include masm32.inc
include ole32.inc
include Oleaut32.inc
includelib kernel32.lib
includelib user32.lib
includelib masm32.lib
includelib ole32.lib
includelib Oleaut32.lib
.data
;-------------------------
;///测试用
buf db 'good',0
szTitleSave db 'good luck',0
;fmt db '%d',0
reLine db 0dh,0ah,'ProcessorId: ',0
;---------------------------
CLSID_WbemLocator GUID <04590f811h, 01d3ah, 011d0h, <089h, 01fh, 000h, 0aah, 000h, 04bh, 02eh, 024h>>
;/// 4590f811-1d3a-11d0-891f-00aa004b2e24
IID_IWbemLocator GUID <0dc12a687h, 0737fh, 011cfh, <088h, 04dh, 000h, 0aah, 000h, 04bh, 02eh, 024h>>
;/// dc12a687-737f-11cf-884d-00aa004b2e24
stresource word 'R','O','O','T','/','C','I','M','V','2',0
stlanguage word 'W','Q','L',0
stquery word 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ','W','i','n','3','2','_','P','r','o','c','e','s','s','o','r',0
;//加以修改可以访问不同字段
stName word 'N','a','m','e',0
stProcessorId word 'P','r','o','c','e','s','s','o','r','I','d',0
.data?
locator dd ?
services dd ?
results dd ?
;--------------------------
resource dd ?
_language dd ?
query dd ?
;--------------------------
result dd ?
returnedCount dd ?
_name dd ?
speed dd ?
DeviceID dd ?
ProcessorId dd ?
stOutName db 100 dup(?)
stNmSize equ $-stOutName
stOutProcessorId db 80 dup(?)
stIdSize equ $-stOutProcessorId
.code
; ---------------------------------------------------------------------------
start:
invoke SysAllocString,offset stresource
mov resource,eax
invoke SysAllocString,offset stlanguage
mov _language,eax
invoke SysAllocString,offset stquery
mov query,eax
;/// initialize COM
invoke CoInitialize,0
invoke CoInitializeSecurity,NULL, -1, NULL, NULL, 0, 3, NULL, 0, NULL
invoke CoCreateInstance,offset CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, offset IID_IWbemLocator,offset locator
;.if eax==S_OK ;//测试成功
; invoke MessageBox,NULL,offset buf,addr szTitleSave,0
;.endif
;/// locator->lpVtbl->ConnectServer(locator, resource, NULL, NULL, NULL, 0, NULL, NULL, &services);
push offset services
push 0
push 0
push 0
push 0
push 0
push 0
mov ecx, resource
push ecx
mov eax, locator
push eax
;-------------------------------------------------
mov eax, DWORD PTR [eax]
call DWORD PTR [eax+12]
;.if eax==0 ;//测试成功
;invoke MessageBox,NULL,offset buf,addr szTitleSave,0
;.endif
;/// services->lpVtbl->ExecQuery(services, language, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &results);
push offset results
push 0
push 0
mov ecx, query
push ecx
mov edx, _language
push edx
mov eax, services
push eax
;--------------------------------------------------
mov eax, [eax]
call DWORD PTR [eax+80]
;.if eax==0 ;//测试成功
; invoke MessageBox,NULL,offset buf,addr szTitleSave,0
;.endif
.if results!=0
mov DWORD PTR result,0
mov DWORD PTR returnedCount,0
;/// results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)
push offset returnedCount
push offset result
push 1
push -1
mov eax, results
push eax
;--------------------------------------------------
mov eax, [eax]
call DWORD PTR [eax+16]
;.if eax==0 ;//成功
; invoke MessageBox,NULL,offset buf,addr szTitleSave,0
;.endif
.if eax==0
;/// result->lpVtbl->Get(result, L"Name", 0, &name, 0, 0);
push 0
push 0
push offset _name
push 0
push offset stName
mov eax, result
push eax
;----------------------------------------------------
mov eax, DWORD PTR [eax]
call DWORD PTR [eax+16]
;.if eax==0 ;//测试成功
;invoke MessageBox,NULL,offset buf,addr szTitleSave,0
;.endif
mov esi,offset _name
;MOV eax,[esi]
;invoke wsprintf,offset buf,offset fmt,eax ;///应该返回VT_BSTR=8 ,正确!!
mov eax,[esi+8] ;/////很关键!!对VARIANT结构体的理解
invoke WideCharToMultiByte,CP_ACP,WC_COMPOSITECHECK,eax,-1,offset stOutName,stNmSize,0,0 ;///将
invoke StdOut,offset stOutName
invoke StdOut,offset reLine ;///另起一行
;/// result->lpVtbl->Get(result, L"ProcessorId", 0, &ProcessorId, 0, 0);
push 0
push 0
push offset ProcessorId
push 0
push offset stProcessorId
mov eax, result
push eax
;----------------------------------------------------
mov eax, DWORD PTR [eax]
call DWORD PTR [eax+16]
mov esi,offset ProcessorId
mov eax,[esi+8]
invoke WideCharToMultiByte,CP_ACP,WC_COMPOSITECHECK,eax,-1,offset stOutProcessorId,stIdSize,0,0 ;///将
invoke StdOut,offset stOutProcessorId
;/// result->lpVtbl->Release(result);
mov eax, DWORD PTR result
push eax
;----------------------------------------------------
mov ecx, DWORD PTR result
mov edx, DWORD PTR [ecx]
mov eax, DWORD PTR [edx+8]
call eax
.endif
.endif
;/// // release WMI COM interfaces
;/// results->lpVtbl->Release(results);
;/// services->lpVtbl->Release(services);
;/// locator->lpVtbl->Release(locator);
mov eax, DWORD PTR results
push eax
;---------------------------
mov eax, DWORD PTR [eax]
call DWORD PTR [eax+8]
mov eax, DWORD PTR services
push eax
;--------------------------
mov eax, DWORD PTR [eax]
call DWORD PTR [eax+8]
mov eax, DWORD PTR locator
push eax
;--------------------------
mov eax, DWORD PTR [eax]
call DWORD PTR [eax+8]
; // unwind everything else we've allocated
;/// CoUninitialize();
invoke CoUninitialize
;/// SysFreeString(query);
;/// SysFreeString(language);
;/// SysFreeString(resource);
invoke SysFreeString, query
invoke SysFreeString, _language
invoke SysFreeString, resource
invoke ExitProcess,eax
end start
;--------------------------------------
; 对VARIANT结构体的注解
;struct tagVARIANT
; {
; union
; {
; struct __tagVARIANT
; {
; VARTYPE vt;
; WORD wReserved1;
; WORD wReserved2;
; WORD wReserved3;
; union
; {
; LONGLONG llVal;
; LONG lVal;
; BYTE bVal;
; SHORT iVal;
; FLOAT fltVal;
; DOUBLE dblVal;
; VARIANT_BOOL boolVal;
; _VARIANT_BOOL bool;
; SCODE scode;
; CY cyVal;
; DATE date;
; BSTR bstrVal; <-------------(此程序中用到)
; IUnknown *punkVal;
; IDispatch *pdispVal;
; SAFEARRAY *parray;
; BYTE *pbVal;
; SHORT *piVal;
; LONG *plVal;
; LONGLONG *pllVal;
; FLOAT *pfltVal;
; DOUBLE *pdblVal;
; VARIANT_BOOL *pboolVal;
; _VARIANT_BOOL *pbool;
; SCODE *pscode;
; CY *pcyVal;
; DATE *pdate;
; BSTR *pbstrVal;
; IUnknown **ppunkVal;
; IDispatch **ppdispVal;
; SAFEARRAY **pparray;
; VARIANT *pvarVal;
; PVOID byref;
; CHAR cVal;
; USHORT uiVal;
; ULONG ulVal;
; ULONGLONG ullVal;
; INT intVal; <--------------
; UINT uintVal;
; DECIMAL *pdecVal;
; CHAR *pcVal;
; USHORT *puiVal;
; ULONG *pulVal;
; ULONGLONG *pullVal;
; INT *pintVal;
; UINT *puintVal;
; struct __tagBRECORD
; {
; PVOID pvRecord;
; IRecordInfo *pRecInfo;
; } __VARIANT_NAME_4;
; } __VARIANT_NAME_3;
; } __VARIANT_NAME_2;
; DECIMAL decVal;
; } __VARIANT_NAME_1;
; } ;
;--------------------------------------------