;*********************************************************
;*--==--* Call WMI (COM Inte**ce) Simple example
;*--==--* By G-Spider 2010.10.5
;*--==--* Web:http://blog.csdn.net/G_Spider
;*********************************************************
充电:
1.用标准C编写COM
http://wenku.baidu.com/view/31fe42e79b89680203d82562.html (翻译文档)
http://www.codeproject.com/KB/COM/com_in_c1.aspx (原作,包括源码)
2. How to obtain data from WMI using a C Application??
http://stackoverflow.com/questio ... ing-a-c-application (简单用纯c调用wmi的方法)
3. Retrieving hardware information with WMI
http://www.codeproject.com/KB/vbscript/VvvHardwareInfo.aspx (用wmi得到PC机大部分信息的例子)
;-----------------------------------------------------------------------------------------------------------
com无非是结构,结构,结构,成员有结构体的指针,也可以有函数入口的指针,
flash**.ocx核心也是com,试了下,用.swf格式的文件做界面也不错,DirectX...com...
下面说正题
首先是自动化数据类型:
;//保存为Struct.inc
; -------------------------------------------------------------------------------
; Basic Data Types
VARIANT_BOOL TYPEDEF WORD
PVOID TYPEDEF DWORD ;//pointer
SCODE TYPEDEF DWORD
BSTR TYPEDEF DWORD ;//BSTR is a special string_pointer.
;
; -------------------------------------------------------------------------------
VARIANT STRUCT
vt WORD VT_EMPTY
wReserved1 WORD 0
wReserved2 WORD 0
wReserved3 WORD 0
Union ;<==Only some would be sufficient.
lVal SDWORD ? ; VT_I4
bVal WORD ? ; VT_UI1
iVal SWORD ? ; VT_I2
fltVal REAL4 ? ; VT_R4
dblVal REAL8 ? ; VT_R8
boolVal VARIANT_BOOL ? ; VT_BOOL
scode SCODE ? ; VT_ERROR
cyVal QWORD ? ; VT_CY
date QWORD ? ; VT_DATE
bstrVal BSTR ? ; VT_BSTR
punkVal PVOID ? ; VT_UNKNOWN
pdispVal PVOID ? ; VT_DISPATCH
parray PVOID ? ; VT_ARRAY
pbVal PVOID ? ; VT_BYREF|VT_UI1
piVal PVOID ? ; VT_BYREF|VT_I2
plVal PVOID ? ; VT_BYREF|VT_I4
pfltVal PVOID ? ; VT_BYREF|VT_R4
pdblVal PVOID ? ; VT_BYREF|VT_R8
pboolVal PVOID ? ; VT_BYREF|VT_BOOL
pscode PVOID ? ; VT_BYREF|VT_ERROR
pcyVal PVOID ? ; VT_BYREF|VT_CY
pdate PVOID ? ; VT_BYREF|VT_DATE
pbstrVal PVOID ? ; VT_BYREF|VT_BSTR
ppunkVal PVOID ? ; VT_BYREF|VT_UNKNOWN
ppdispVal PVOID ? ; VT_BYREF|VT_DISPATCH
pparray PVOID ? ; VT_BYREF|VT_ARRAY
pvarVal PVOID ? ; VT_BYREF|VT_VARIANT
byref PVOID ? ; Generic ByRef
cVal SBYTE ? ; VT_I1
uiVal WORD ? ; VT_UI2
ulVal DWORD ? ; VT_UI4
intVal SWORD ? ; VT_int
uintVal WORD ? ; VT_uint
pdecVal PVOID ? ; VT_BYREF|VT_DECIMAL
pcVal PVOID ? ; VT_BYREF|VT_I1
puiVal PVOID ? ; VT_BYREF|VT_UI2
pulVal PVOID ? ; VT_BYREF|VT_UI4
pintVal PVOID ? ; VT_BYREF|VT_int
puintVal PVOID ? ; VT_BYREF|VT_uint
ENDS
VARIANT ENDS
;-----------------------------------------------------------------
对接口的规范定义:
;//保存为WMI_COM.inc
;************************************************************************
;COM API for WMI Struct. Just Part!
;By G-Spider 2010.10.5
;Refer to WbemCli.h and
;Refer to http://msdn.microsoft.com/en-us/library/aa389276(v=VS.85).aspx
;************************************************************************
pcomethod1 TYPEDEF PROTO :DWORD
pcomethod2 TYPEDEF PROTO :DWORD, :DWORD
pcomethod3 TYPEDEF PROTO :DWORD, :DWORD, :DWORD
pcomethod4 TYPEDEF PROTO :DWORD, :DWORD, :DWORD, :DWORD
pcomethod5 TYPEDEF PROTO :DWORD, :DWORD, :DWORD, :DWORD , :DWORD
pcomethod6 TYPEDEF PROTO :DWORD, :DWORD, :DWORD, :DWORD , :DWORD ,:DWORD
pcomethod7 TYPEDEF PROTO :DWORD, :DWORD, :DWORD, :DWORD , :DWORD ,:DWORD ,:DWORD
pcomethod8 TYPEDEF PROTO :DWORD, :DWORD, :DWORD, :DWORD , :DWORD ,:DWORD ,:DWORD ,:DWORD
pcomethod9 TYPEDEF PROTO :DWORD, :DWORD, :DWORD, :DWORD , :DWORD ,:DWORD ,:DWORD ,:DWORD ,:DWORD
comethod1 TYPEDEF PTR pcomethod1
comethod2 TYPEDEF PTR pcomethod2
comethod3 TYPEDEF PTR pcomethod3
comethod4 TYPEDEF PTR pcomethod4
comethod5 TYPEDEF PTR pcomethod5
comethod6 TYPEDEF PTR pcomethod6
comethod7 TYPEDEF PTR pcomethod7
comethod8 TYPEDEF PTR pcomethod8
comethod9 TYPEDEF PTR pcomethod9
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
;
;IWbemLocator=>IWbemServices=>IEnumWbemClassObject=>IWbemClassObject
;
;--------------------------------------
IUnknown struct
QueryInterface comethod3 ?
AddRef comethod1 ?
Release comethod1 ?
IUnknown ends
;--------------------------------------
IWbemLocatorVtbl struct
; IUnknown methods
Unknown IUnknown <>
; WbemLocator methods
ConnectServer comethod9 ?
IWbemLocatorVtbl ends
IWbemLocator struct
lpVtbl DWORD ? ;->IWbemLocatorVtbl
IWbemLocator ends
;--------------------------------------
IWbemServicesVtbl struct
; IUnknown methods
Unknown IUnknown <>
; IWbemServices methods
OpenNamespace comethod6 ?
CancelAsyncCall comethod2 ?
QueryObjectSink comethod3 ?
GetObject comethod6 ?
GetObjectAsync comethod5 ?
PutClass comethod5 ?
PutClassAsync comethod5 ?
DeleteClass comethod5 ?
DeleteClassAsync comethod5 ?
CreateClassEnum comethod5 ?
CreateClassEnumAsync comethod5 ?
PutInstance comethod5 ?
PutInstanceAsync comethod5 ?
DeleteInstance comethod5 ?
DeleteInstanceAsync comethod5 ?
CreateInstanceEnum comethod5 ?
CreateInstanceEnumAsync comethod5 ?
ExecQuery comethod6 ?
ExecQueryAsync comethod6 ?
ExecNotificationQuery comethod6 ?
ExecNotificationQueryAsync comethod6 ?
ExecMethod comethod8 ?
ExecMethodAsync comethod7 ?
IWbemServicesVtbl ends
IWbemServices struct
lpVtbl DWORD ? ;->IWbemServicesVtbl
IWbemServices ends
;--------------------------------------
IEnumWbemClassObjectVtbl struct
; IUnknown methods
Unknown IUnknown <>
; IEnumWbemClassObject methods
Reset comethod1 ?
Next comethod5 ?
NextAsync comethod3 ?
Clone comethod2 ?
Skip comethod3 ?
IEnumWbemClassObjectVtbl ends
IEnumWbemClassObject struct
lpVtbl DWORD ? ;->IEnumWbemClassObjectVtbl
IEnumWbemClassObject ends
;--------------------------------------
IWbemClassObjectVtbl struct
; IUnknown methods
Unknown IUnknown <>
; IWbemClassObject methods
GetQualifierSet comethod2 ?
Get comethod6 ?
Put comethod5 ?
Delete comethod2 ?
GetNames comethod5 ?
BeginEnumeration comethod2 ?
Next comethod6 ?
EndEnumeration comethod1 ?
GetPropertyQualifierSet comethod3 ?
Clone comethod2 ?
GetObjectText comethod3 ?
SpawnDerivedClass comethod3 ?
SpawnInstance comethod3 ?
CompareTo comethod3 ?
GetPropertyOrigin comethod3 ?
InheritsFrom comethod2 ?
GetMethod comethod5 ?
PutMethod comethod5 ?
DeleteMethod comethod2 ?
BeginMethodEnumeration comethod2 ?
NextMethod comethod5 ?
EndMethodEnumeration comethod1 ?
GetMethodQualifierSet comethod3 ?
GetMethodOrigin comethod3 ?
IWbemClassObjectVtbl ends
IWbemClassObject struct
lpVtbl DWORD ? ;->IWbemClassObjectVtbl
IWbemClassObject ends
;------------------------------------
;IWbemClassObject::Get method
;Refer to http://msdn.microsoft.com/en-us/library/aa391442(v=VS.85).aspx
;HRESULT Get(
; [in] LPCWSTR wszName, //<===Name of the desired property.
; [in] LONG lFlags,
; [out] VARIANT *pVal, //<===assigned the correct type and value for VARIANT
; [out, optional] CIMTYPE *pvtType,
; [out, optional] LONG *plFlavor
;);
;for example Refer to http://msdn.microsoft.com/en-us/library/aa394373(VS.85).aspx
;class Win32_Processor : CIM_Processor
;{
; uint16 AddressWidth;
; uint16 Architecture;
; uint16 Availability;
; string Caption;
; uint32 ConfigManagerErrorCode;
; boolean ConfigManagerUserConfig;
; uint16 CpuStatus;
; string CreationClassName;
; uint32 CurrentClockSpeed;
; uint16 CurrentVoltage;
; uint16 DataWidth;
; string Description;
; string DeviceID;
; boolean ErrorCleared;
; string ErrorDescription;
; uint32 ExtClock;
; uint16 Family;
; datetime InstallDate;
; uint32 L2CacheSize;
; uint32 L2CacheSpeed;
; uint32 L3CacheSize;
; uint32 L3CacheSpeed;
; uint32 LastErrorCode;
; uint16 Level;
; uint16 LoadPercentage;
; string Manufacturer;
; uint32 MaxClockSpeed; //<=====wszName=L"MaxClockSpeed" ;***.vt=VT_UI4
; string Name; //<=====wszName=L"Name" ;***.vt=VT_BSTR
; uint32 NumberOfCores;
; uint32 NumberOfLogicalProcessors;
; string OtherFamilyDescription;
; string PNPDeviceID;
; uint16 PowerManagementCapabilities[];
; boolean PowerManagementSupported;
; string ProcessorId; //<=====wszName=L"ProcessorId" ;***.vt=VT_BSTR
; uint16 ProcessorType;
; uint16 Revision;
; string Role;
; string SocketDesignation;
; string Status;
; uint16 StatusInfo;
; string Stepping;
; string SystemCreationClassName;
; string SystemName;
; string UniqueId;
; uint16 UpgradeMethod;
; string Version;
; uint32 VoltageCaps;
;};
;***************************************************************
接着是使用上面的结构..
;----------------------------------------------------------------------------------------
;*********************************************************
;*--==--* Call WMI (COM Interface) Simple example
;*--==--* By G-Spider 2010.10.5
;*--==--* Web:http://blog.csdn.net/G_Spider
;*--==--* ml /c /coff wmi.asm
;*--==--* link /subsystem:console wmi.obj
;*********************************************************
.586
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
include user32.inc
include masm32.inc
include ole32.inc
include Oleaut32.inc
include Struct.inc ;<========Struct.inc
includelib kernel32.lib
includelib user32.lib
includelib masm32.lib
includelib ole32.lib
includelib Oleaut32.lib
.data
;---------------------------
;buf db 'good',0 ;Just use to Test
fmt db '%d',0 ;also use output MaxClockSpeed
;---------------------------
include WMI_COM.inc ;<========WMI_COM.inc
stresource word 'R','O','O','T','/','C','I','M','V','2',0
;// L"ROOT//CIMV2"
stlanguage word 'W','Q','L',0
;// L"WQL"
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
;// L"SELECT * FROM Win32_Processor"
;// Note! we just use Win32_Processor,and the same to other Win32 Classes.
;// Refer to http://msdn.microsoft.com/en-us/library/aa394084(v=VS.85).aspx
stName word 'N','a','m','e',0
;// L"Name" <==from Win32_Processor
stProcessorId word 'P','r','o','c','e','s','s','o','r','I','d',0
;// L"ProcessorId" <==from Win32_Processor
stMaxClockSpeed word 'M','a','x','C','l','o','c','k','S','p','e','e','d',0
;// L"MaxClockSpeed" <==from Win32_Processor
reLine db 0dh,0ah,0
reLineId db 0dh,0ah,'ProcessorId :',0
szClockSpeed db 0dh,0ah,'MaxClockSpeed:',0
.data?
locator dd ? ;->IWbemLocator
services dd ? ;->IWbemServices
results dd ? ;->IEnumWbemClassObject
;--------------------------
;lpBSTR
resource dd ?
_language dd ?
query dd ?
;--------------------------
result dd ?
returnedCount dd ?
_name dd ?
speed dd ?
ProcessorId dd ?
;--------------------------
buf1 db 16 dup(?) ;MaxClockSpeed into ASCII_String
stOutName db 100 dup(?)
stNmSize equ $-stOutName
stOutProcessorId db 80 dup(?)
stIdSize equ $-stOutProcessorId
.code
; ---------------------------------------------------------------------------
start:
invoke SysAllocString,offset stresource
mov resource,eax ;Into BSTR
invoke SysAllocString,offset stlanguage
mov _language,eax ;Into BSTR
invoke SysAllocString,offset stquery
mov query,eax ;Into BSTR
;/// initialize COM
invoke CoInitialize,0
invoke CoInitializeSecurity,NULL, -1, NULL, NULL, 0, 3, NULL, 0, NULL
;/// connect to WMI
invoke CoCreateInstance,offset CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, offset IID_IWbemLocator,offset locator
;-------------------------------------------------
;/// locator->lpVtbl->ConnectServer(locator, resource, NULL, NULL, NULL, 0, NULL, NULL, &services);
mov edx, locator
mov eax, [edx].IWbemLocator.lpVtbl
invoke [eax].IWbemLocatorVtbl.ConnectServer,edx,resource,NULL,NULL,NULL,0,NULL,NULL,offset services
;-------------------------------------------------
;/// services->lpVtbl->ExecQuery(services, language, query, WBEM_FLAG_BIDIRECTIONAL, NULL, &results);
mov edx, services
mov eax, [edx].IWbemServices.lpVtbl
invoke [eax].IWbemServicesVtbl.ExecQuery, edx, _language, query, 0, NULL,offset results
;--------------------------------------------------
.if results!=0
mov result,0
mov returnedCount,0
C100:
;/// while((hr = results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)) == S_OK)
;/// You should use Loop.
mov edx, results
mov eax, [edx].IEnumWbemClassObject.lpVtbl ;call eax,[eax]
invoke [eax].IEnumWbemClassObjectVtbl.Next,edx,-1, 1,offset result,offset returnedCount
;--------------------------------------------------
;**************************************************************************************
.if eax==0
;/// hr = result->lpVtbl->Get(result, L"Name", 0, &name, 0, 0);
mov edx, result
mov eax, [edx].IWbemClassObject.lpVtbl
invoke [eax].IWbemClassObjectVtbl.Get,edx,offset stName, 0,offset _name, 0, 0
;----------------------------------------------------
mov esi,offset _name ;<==Note the _name!
;xor eax,eax
;MOV ax,[esi].VARIANT.vt
;invoke wsprintf,offset buf1,offset fmt,eax ;///Test ,return VT_BSTR=8 ,right!!
;invoke MessageBox,NULL,offset buf1,offset buf,0
mov eax,[esi].VARIANT.bstrVal ;///Key!!VARIANT
invoke WideCharToMultiByte,CP_ACP,WC_COMPOSITECHECK,eax,-1,offset stOutName,stNmSize,0,0
invoke StdOut,offset stOutName ;OutPut
invoke StdOut,offset szClockSpeed ;Reline
;/// hr = result->lpVtbl->Get(result, L"MaxClockSpeed", 0, &speed, 0, 0);
mov edx, result
mov eax, [edx].IWbemClassObject.lpVtbl
invoke [eax].IWbemClassObjectVtbl.Get,edx,offset stMaxClockSpeed, 0, offset speed, 0, 0
;----------------------------------------------------
mov esi,offset speed ;<==Note the speed!
mov eax,[esi].VARIANT.ulVal ;///Key!!VARIANT ,eax=MaxClockSpeed
invoke wsprintf,offset buf1,offset fmt,eax
invoke StdOut,offset buf1 ;OutPut
invoke StdOut,offset reLineId ;///Reline
;/// hr = result->lpVtbl->Get(result, L"ProcessorId", 0, &ProcessorId, 0, 0);
mov edx, result
mov eax, [edx].IWbemClassObject.lpVtbl
invoke [eax].IWbemClassObjectVtbl.Get,edx,offset stProcessorId, 0,offset ProcessorId, 0, 0
;----------------------------------------------------
mov esi,offset ProcessorId
mov eax,[esi].VARIANT.bstrVal
invoke WideCharToMultiByte,CP_ACP,WC_COMPOSITECHECK,eax,-1,offset stOutProcessorId,stIdSize,0,0
invoke StdOut,offset stOutProcessorId ;OutPut
invoke StdOut,offset reLine
;/// result->lpVtbl->Release(result);
mov edx, DWORD PTR result
mov eax, [edx].IWbemClassObject.lpVtbl
invoke [eax].IWbemClassObjectVtbl.Unknown.Release,edx
;----------------------------------------------------
jmp C100
.endif
;****************************************************************************************
.endif
;/// release WMI COM interfaces
;/// results->lpVtbl->Release(results);
mov edx, results
mov eax, [edx].IEnumWbemClassObject.lpVtbl
invoke [eax].IEnumWbemClassObjectVtbl.Unknown.Release,edx
;----------------------------------------------------
;/// services->lpVtbl->Release(services);
mov edx, services
mov eax, [edx].IWbemServices.lpVtbl
invoke [eax].IWbemServicesVtbl.Unknown.Release,edx
;----------------------------------------------------
;/// locator->lpVtbl->Release(locator);
mov edx, DWORD PTR locator
mov eax, [edx].IWbemLocator.lpVtbl
invoke [eax].IWbemLocatorVtbl.Unknown.Release,edx
;----------------------------------------------------
;/// CoUninitialize();
invoke CoUninitialize
;/// SysFreeString(query);
;/// SysFreeString(language);
;/// SysFreeString(resource);
invoke SysFreeString, query
invoke SysFreeString, _language
invoke SysFreeString, resource
invoke ExitProcess,eax
end start