WMI Structure a lot, but they are similar. So I just finished part of the work .

How to obtain data from WMI using a C Application??
Refer to  http://stackoverflow.com/questions/1431103/how-to-obtain-data-from-wmi-using-a-c-application


;Save as  Struct.inc
; *******************************************
;  Basic Data Types
VARIANT_BOOL    TYPEDEF     WORD
PVOID           TYPEDEF     DWORD
;VARTYPE         TYPEDEF     WORD
SCODE           TYPEDEF     DWORD
BSTR            TYPEDEF     DWORD  

; -------------------------------------------------------------------------------
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
;************************************************


Posted on 2010-10-04 07:40:03 by G-Spider
The following file  Save as WMI_COM.inc


;********************************************
;COM API for WMI Struct. Just Part!
;By G-Spider 2010.10.4
;Refer to  WbemCli.h  and  http://msdn.microsoft.com/en-us/library/aa389276(v=VS.85).aspx
;********************************************

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 DWORD   ?
       AddRef DWORD   ?
       Release DWORD   ?
IUnknown        ends
;--------------------------------------
IWbemLocatorVtbl    struct
       ; IUnknown methods
       Unknown       IUnknown  <>  
       ; WbemLocator methods
       ConnectServer DWORD   ?
IWbemLocatorVtbl    ends

IWbemLocator        struct
       lpVtbl    DWORD   ?  ;->IWbemLocatorVtbl
IWbemLocator        ends
;--------------------------------------
IWbemServicesVtbl   struct
       ; IUnknown methods
       Unknown       IUnknown  <>  
       ;IWbemServices methods
       OpenNamespace        DWORD   ?
       CancelAsyncCall        DWORD   ?
       QueryObjectSink        DWORD   ?
       GetObject        DWORD   ?
       GetObjectAsync        DWORD   ?
       PutClass        DWORD   ?
       PutClassAsync        DWORD   ?
       DeleteClass        DWORD   ?
       DeleteClassAsync        DWORD   ?
       CreateClassEnum        DWORD   ?
       CreateClassEnumAsync DWORD   ?
       PutInstance        DWORD   ?
       PutInstanceAsync DWORD   ?
       DeleteInstance        DWORD   ?
       DeleteInstanceAsync DWORD   ?
       CreateInstanceEnum DWORD   ?
       CreateInstanceEnumAsync DWORD   ?
       ExecQuery        DWORD   ?
       ExecQueryAsync        DWORD   ?
       ExecNotificationQuery DWORD   ?
       ExecNotificationQueryAsync DWORD   ?
       ExecMethod        DWORD   ?
       ExecMethodAsync        DWORD   ?
IWbemServicesVtbl     ends

IWbemServices           struct
       lpVtbl        DWORD   ?  ;->IWbemServicesVtbl
IWbemServices           ends      

;--------------------------------------
IEnumWbemClassObjectVtbl    struct
       ; IUnknown methods
       Unknown       IUnknown  <>  
       ;IEnumWbemClassObject methods
       Reset        DWORD   ?
       Next        DWORD   ?
       NextAsync DWORD   ?
       Clone        DWORD   ?
       Skip          DWORD   ?
IEnumWbemClassObjectVtbl    ends

IEnumWbemClassObject    struct
       lpVtbl          DWORD   ?;->IEnumWbemClassObjectVtbl
IEnumWbemClassObject    ends      

;--------------------------------------
IWbemClassObjectVtbl        struct
       ; IUnknown methods
       Unknown                 IUnknown  <>
       ;IWbemClassObject methods
       GetQualifierSet DWORD   ?
       Get        DWORD   ?
       Put        DWORD   ?
       Delete        DWORD   ?
       GetNames DWORD   ?
       BeginEnumeration DWORD   ?
       Next        DWORD   ?
       EndEnumeration DWORD   ?
       GetPropertyQualifierSet DWORD   ?
       Clone        DWORD   ?
       GetObjectText DWORD   ?
       SpawnDerivedClass DWORD   ?
       SpawnInstance DWORD   ?
       CompareTo DWORD   ?
       GetPropertyOrigin DWORD   ?
       InheritsFrom DWORD   ?
       GetMethod DWORD   ?
       PutMethod DWORD   ?
       DeleteMethod DWORD   ?
       BeginMethodEnumeration DWORD   ?
       NextMethod DWORD   ?
       EndMethodEnumeration DWORD   ?
       GetMethodQualifierSet DWORD   ?
       GetMethodOrigin DWORD   ?
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(
;               LPCWSTR wszName, //<===Name of the desired property.
;               LONG lFlags,
;              VARIANT *pVal,  //<===assigned the correct type and value for VARIANT
;    CIMTYPE *pvtType,
;    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;
;};
;***************************************************************


Posted on 2010-10-04 07:45:18 by G-Spider
:D


;********************************************
;Call WMI (COM Interface) Simple example  
;By G-Spider 2010.10.4
;ml  /c /coff Speeker.asm  
;link /subsystem:console Speeker.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  Up file
 
  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    Up file

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

       ;/// hr = 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, .IWbemLocator.lpVtbl        ;call eax,
       call    .IWbemLocatorVtbl.ConnectServer  ;call DWORD PTR
 

;/// hr = 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, .IWbemServices.lpVtbl
call    .IWbemServicesVtbl.ExecQuery ;call    DWORD PTR

.if results!=0
mov result,0
mov returnedCount,0
;///    while((hr = results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)) == S_OK)
;///    You should use Loop.
push offset returnedCount
push offset result
push 1
push -1
mov eax, results
push eax
;--------------------------------------------------
mov eax, .IEnumWbemClassObject.lpVtbl ;call eax,
call    .IEnumWbemClassObjectVtbl.Next    ;call DWORD PTR
       ;**************************************************************************************
       .if eax==0
       ;///    hr = 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, .IWbemClassObject.lpVtbl
               call    .IWbemClassObjectVtbl.Get ;call    DWORD PTR
       
               mov esi,offset _name   ;<==Note the _name!
               
               ;xor eax,eax
               ;MOV ax,.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,.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);
               push 0
               push 0    
               push offset speed
               push 0
               push offset stMaxClockSpeed
               mov eax, result
               push eax
       ;----------------------------------------------------
               mov eax, .IWbemClassObject.lpVtbl
               call    .IWbemClassObjectVtbl.Get ;call    DWORD PTR

               mov     esi,offset speed         ;<==Note the speed!
               mov     eax,.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);
               push 0
               push 0    
               push offset ProcessorId
               push 0
               push offset stProcessorId
               mov eax, result
               push eax
       ;----------------------------------------------------
               mov eax, .IWbemClassObject.lpVtbl
               call    .IWbemClassObjectVtbl.Get ;call    DWORD PTR

               mov esi,offset ProcessorId            
               mov eax,.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 eax, DWORD PTR result
               push eax
       ;----------------------------------------------------
               mov eax, .IWbemClassObject.lpVtbl           ;call    eax,
               call    .IWbemClassObjectVtbl.Unknown.Release   ;call    DWORD PTR
       .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, .IEnumWbemClassObject.lpVtbl
       call    .IEnumWbemClassObjectVtbl.Unknown.Release
       
mov eax, DWORD PTR services
push eax
       ;--------------------------
mov eax, .IWbemServices.lpVtbl
call    .IWbemServicesVtbl.Unknown.Release

mov eax, DWORD PTR locator
push eax
       ;--------------------------
mov eax, .IWbemLocator.lpVtbl
call    .IWbemLocatorVtbl.Unknown.Release

;    // 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


Attachments:
Posted on 2010-10-04 08:01:55 by G-Spider