找回密码
 注册
搜索
查看: 12665|回复: 0

[收藏]C++ Builder 通过 WMI 获取系统信息

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)4 M" i' Z" C3 a  B/ f3 }) d, x$ K
- j: b7 t. z7 }0 `  _

( Q5 K% `" P. u--------------------------------------------------------------------------------2 D0 a: }6 ^, J6 y0 e
WMI: Windows Management Instrumentation (Windows 管理工具)+ l& e' c$ o9 ^+ S
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 # k6 i7 T  }- [' ^5 {4 P( L  [
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。" r7 ]( e+ R$ p7 h/ m
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
2 L- {& n# X. X* }; W
7 f3 t% ~) K: @--------------------------------------------------------------------------------
' F8 {' @4 p1 Y7 S+ s% K) B& sBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
" x/ w, F- u9 a- E/ ^% Q: E2 F: P- ^
7 R. ?3 E& u% S6 h3 q' w; h4 O+ ]--------------------------------------------------------------------------------
+ R! V% G9 g# C5 P& [① 初始化 COM 接口:
3 Y! E% D- X9 u. I- ^0 x   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
9 J; x8 E% g4 g. {9 ?( D4 T# J; S   这两个函数在 #include <comdef.h> 里面定义。
+ R# f: Q! m" e  {8 l8 s6 I. |) u7 i& X
② 获取访问 WMI 权限:
- }/ a  C$ n9 G* m   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);5 j) @( u6 T* i" y0 K
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
# K( ?+ E3 j0 P4 K3 a- A! |9 u7 U6 i& E" s5 H' J  I& Y8 X7 {' l
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
! U' N  y# a- D% [9 q   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。6 m, I# Q/ u9 k$ |
! Z+ S0 }) B6 T: k; [/ O% i
void GetWmiInfo(TStrings *lpList, WideString wsClass)
7 b& U  T: p* y8 b{3 S$ Z! q( @6 b; T) r
  IWbemLocator *pWbemLocator = NULL;
$ `! o" x0 J4 v2 U* O& U5 |  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
3 P- G  n, F5 ], }: z  n1 T7 G   {( K( I$ K& C; e4 [7 r% v% }
    IWbemServices *pWbemServices = NULL;* ^' t) P8 B. W
    WideString wsNamespace = (L"root\\cimv2");
  q) F: g1 c! `7 A: D2 P  @) s1 z     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)# V7 z9 _7 z) f3 o5 c! x
     {5 x1 @" K( s% W) c
       IEnumWbemClassObject *pEnumClassObject = NULL;5 ~* G3 e7 u) Z6 _( l
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
5 s1 v5 u5 Y% a+ Y" s* N" B& l        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
1 ]+ P2 d, v5 C9 B  t         {9 z/ |7 J- f  D
          IWbemClassObject *pClassObject = NULL;
' _: J8 F! D6 a6 s4 f$ k. c; v8 Z           ULONG uCount = 1, uReturned;
) H, D+ E) B1 q5 A1 ^/ |5 J           if(pEnumClassObject->Reset() == S_OK)8 Y0 l6 l. ~. t8 x3 |3 t
           {& _& L2 B6 \3 [( s: O7 L+ G' y
             int iEnumIdx = 0;/ V+ [9 v  o2 Q5 z
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)8 Q" o) y( b  g' n
              {
! i# S1 W1 Q, m' P1 `                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
9 y- R+ i, t8 ^7 M% l; K& E, w" i
( ]. K" c" T( ~" C( K* L                 SAFEARRAY *pvNames = NULL;6 B' i) a+ R; o
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)# h+ F, i. q- F) [) a7 `1 V( Z
                 {: W. z) M, z4 Z! U6 ]
                   long vbl, vbu;  O( P7 I7 m( s; h
                   SafeArrayGetLBound(pvNames, 1, &vbl);
6 J. {3 j: ~9 d  j# T9 [( ^8 p# G; [                    SafeArrayGetUBound(pvNames, 1, &vbu);
, l( f, T1 a- _                    for(long idx=vbl; idx<=vbu; idx++)
) \9 u1 j3 r, c; c5 l6 v0 F- F                     {
5 x( w+ V. v, z, J$ B                       long aidx = idx;6 ^( M" n' ~- l( x* i% E
                      wchar_t *wsName = 0;7 Q) P& o* i- w' |; @5 {. V
                      VARIANT vValue;
. j4 f5 I0 P- x$ v0 a: e                       VariantInit(&vValue);+ |/ @& s8 n7 F& Z  q
                      SafeArrayGetElement(pvNames, &aidx, &wsName);
: k# I) A0 W, v! X, B9 E- b. v( w/ h* ]' l
                      BSTR bs = SysAllocString(wsName);: O6 N5 ]6 f2 C6 p5 g: i
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);0 V2 {; m1 M1 m: R% \6 Q- L( G
                      SysFreeString(bs);
# d. U1 t1 h9 s* O! w5 B
' g/ M& Y+ Q+ {8 h) k+ A                       if(hRes == S_OK)
& g8 ~. e! e8 r& l% X1 t' m                        {! \: @8 c% N+ J2 b
                         AnsiString s;  D" B* Y0 r# H' W, E$ P
                         Variant v = *(Variant*)&vValue;" e3 }2 _( J. w: j9 q* ^
                         if(v.IsArray())! U0 r/ }, t9 b; Y
                          {
! o9 }- F6 O% _1 Q) j3 l1 T! b                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++). x* r# Y1 `" b1 J/ H7 _3 F
                             {9 v- n# F+ \- |' C9 C
                               Variant a = v.GetElement(i);
7 ]8 m9 _* F# @  v, I1 L$ {                                if(!s.IsEmpty())5 U" ^" x% O: c$ p8 H7 Z
                                 s+=", ";
* d5 k) r' _+ c+ @" v                                s+=VarToStr(a);# o/ W8 O" @' Z5 l0 y6 ~) J
                             }
" @! D' `0 U$ l5 R) Q                           }
/ X6 l( _5 B5 h) @: m                          else, H- X* M) m: J+ Y* _( K: ~
                          {
: M3 x  j: Z/ ^% k                             s = VarToStr(v);
& v" R9 Q) M. a: v                           }
% m/ c) h" k5 o% I                          lpList->Add(AnsiString(wsName)+"="+s);! z" g- c2 M# l% ?
                       }
2 T/ N. e" B7 N: v( P4 y; y
6 R" c; p/ z* d+ P2 I  J6 T                       VariantClear(&vValue);: Z. A" Q. B* Z& f
                      SysFreeString(wsName);$ l* Q" p1 F; V* i
                    }
. T( Y) F3 T( u2 `' r                  }* N1 r2 d' [) h( V
                if(pvNames)SafeArrayDestroy(pvNames);
' u2 }& R7 f. g! d3 O' v                 iEnumIdx++;9 |5 H; K1 h6 X& A2 ^
              }) b1 s/ Q& X9 M7 q3 Z
           }
1 s2 \/ A/ c2 y: ~5 r. f8 o           if(pClassObject)pClassObject->Release();
" c9 F% P4 h' d         }& x( A$ A! w9 r8 Q0 e
       if(pEnumClassObject)pEnumClassObject->Release();
) @- T5 b3 M7 d  o! o; z      }
2 \/ }# K  g7 i     if(pWbemServices)pWbemServices->Release();/ Q5 D5 f! D6 x2 s
  }
. }0 k  A% N+ r1 Z9 }  G  if(pWbemLocator)pWbemLocator->Release();
0 T. P5 c3 h( T7 _  R( T3 k}! j9 ~$ `9 `8 v# ^+ ^1 |
//---------------------------------------------------------------------------
/ T- g: x+ D5 |7 s! d) y% C) d% r- b+ }" [* R6 H
// 通过 WIN32_bios 获取 BIOS 信息:
2 `- e. x0 O7 ]/ evoid __fastcall TForm1::Button1Click(TObject *Sender)/ l$ g. Y3 k, ]/ t# X
{5 }  t9 T+ o2 M% b6 u" A6 o
   Memo1->Lines->Add("================== [WIN32_bios] =================");1 P& v: X2 Z6 r: F" @5 A2 k1 ?* m
   GetWmiInfo(Memo1->Lines, "WIN32_bios");, u/ w6 h# j7 a! D! E& D
   Memo1->Lines->Add("");$ i- w: B) B! w6 U" H9 [
}4 J( z$ _! q9 K' U2 ]8 J2 r

3 a2 K/ l0 E; t( {0 G" i# i' P--------------------------------------------------------------------------------
% m( s( P1 Z9 G( b+ U# F! k0 n/ h2 i4 ^# Q
WMI 可以访问的信息类型有:+ S- \9 [0 s. y# K) M
  Win32_1394Controller
4 p) S) Y, X# ?$ w  i3 X   Win32_BaseBoard& C# ?3 W) w- T0 Q
  Win32_Battery2 R. W6 F3 g6 `' f# \0 ?
  Win32_BIOS
/ ?2 Q* ]2 V* ~' J: a3 P) e   Win32_Bus
3 K. H8 {4 }8 ^" R! ]- \   Win32_CacheMemory  c2 A7 D2 F1 u( K9 k7 t. x
  Win32_CDROMDrive
$ ^; _$ S1 q2 b- Z   Win32_CurrentProbe2 O! s# m  k( t4 i3 V
  Win32_DesktopMonitor
* P8 a# o* N. x/ k& j- d5 @   Win32_DeviceMemoryAddress9 {+ l1 m- f: J& ~8 D
  Win32_DiskDrive
* p1 r1 h, K) h& i) B$ @. X7 c   Win32_DisplayConfiguration
: E+ b. t, G% j% B4 _   Win32_DisplayControllerConfiguration& g8 b/ t% W# ^: e( ^
  Win32_DMAChannel
, x3 M; E( d1 E# ~0 \9 h   Win32_Fan* @" ^% t$ I1 n% v0 ]
  Win32_FloppyController
6 d+ q& G+ O6 \5 x( {4 T   Win32_FloppyDrive5 x# G. ^: g  a' s3 ]; }
  Win32_HeatPipe
/ Z* j2 T( @7 e   Win32_IDEController
9 V+ }# d1 P' J; n3 M4 j   Win32_InfraredDevice
4 g3 w$ c% ~9 N& }7 N$ p  [! w% M   Win32_IRQResource
. T! r; c, z, ?& i( v  T# u   Win32_Keyboard
0 F2 C6 f: u; V; `' V9 i   Win32_MemoryArray. t5 |' h7 Q3 [" b. u! n
  Win32_MemoryDevice; ^8 j0 u# f* W
  Win32_MotherboardDevice/ @1 j$ m3 i+ h! N8 V0 R$ W
  Win32_NetworkAdapter
+ i) N- f) Q4 x   Win32_NetworkAdapterConfiguration7 ~5 T+ b5 Y0 O4 M3 D( ^. D- }( n; |- L
  Win32_OnBoardDevice
) i& K6 ~% N* d5 ?1 i6 K8 C   Win32_ParallelPort
; q, H* n  u+ ]( T+ Z! x   Win32_PCMCIAController1 E# k( z# H+ u' L2 z! l
  Win32_PhysicalMemory( k3 J( u) t4 L
  Win32_PhysicalMemoryArray
" z3 B! s; }: R- M' X8 P   Win32_PnPEntity/ o( S! r( o  ~) w/ r
  Win32_PointingDevice
; f+ l) p  t3 i- B0 d' L- M   Win32_PortableBattery
  P; [$ N+ Q6 ]/ `# g9 ^; f* O/ |/ e   Win32_PortConnector
3 l. i3 K& H: c* t  y1 m' a2 S8 y   Win32_PortResource& A: ~- B. f) M( m# @. H
  Win32_POTSModem3 ^( D7 y  j! b, |: G. N
  Win32_PowerManagementEvent. J- c# S( H. |9 E
  Win32_Printer& r6 H' T" j- A. K+ l
  Win32_PrinterConfiguration% w3 D7 Z8 W0 u5 y$ n1 h# }
  Win32_PrintJob
& s' c0 [; ~5 m9 C5 B   Win32_Processor
  q: F* ^& W$ g- W/ o% r$ T   Win32_Refrigeration7 ]) }8 t: E! U+ J. `
  Win32_SerialPort. e* M9 n6 P) ?: f! t# w& s
  Win32_SerialPortConfiguration
: V# g$ U. ^/ O6 b   Win32_SMBIOSMemory
1 Q; p, \2 _2 _   Win32_SoundDevice3 C! W! K* a; z! v
  Win32_SystemEnclosure
$ R( ]# M, V. b; C8 \   Win32_SystemMemoryResource
5 }# p, g/ }% z   Win32_SystemSlot. V/ I+ T+ H# \  Y& p' C( m$ M. Q
  Win32_TapeDrive: X- y! I! G$ ?# b8 c
  Win32_TemperatureProbe' e* Q$ `+ r( _9 N% H5 D' L
  Win32_UninterruptiblePowerSupply9 Q' e0 e' e. H, D! [* D* y
  Win32_USBController2 O0 W; Z- [4 N4 ~: b
  Win32_VideoConfiguration
# Z9 a4 L" n  X3 b( T! y   Win32_VideoController* z+ A& e& i+ C7 V1 K
  Win32_VoltageProbe
- W5 X/ S' b& u  M" Z( k: y+ a* ?$ [# p# u; ?( e$ f( z% N+ Q8 G
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|宁德市腾云网络科技有限公司 ( 闽ICP备2022007940号-5|闽公网安备 35092202000206号 )

GMT+8, 2026-1-7 05:52 , Processed in 0.018969 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表