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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
* r, M% X3 ?2 M3 K$ S& E! Q  _0 J' B# Q) Z

/ J' I7 |' B1 D--------------------------------------------------------------------------------8 ?$ L/ L6 o5 Q
WMI: Windows Management Instrumentation (Windows 管理工具)
! d; y) i/ A/ q   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
+ P& E4 a+ Y6 a" B5 `) Q   利用这个工具可以管理本地或客户端系统中几乎所有的信息。+ ^0 }6 j6 v9 L: t
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。   b& q$ D! ]  G. H" Y" J: `/ Q
2 G# k! F, X. \2 Q& u( P
--------------------------------------------------------------------------------
' Z8 v1 f2 U) a% CBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面% v% c* k0 Q/ y3 r' ^
9 [9 \& O' J4 f, I6 H
--------------------------------------------------------------------------------% `0 l8 C6 p% ^; B$ u. O
① 初始化 COM 接口:6 I# B  s% h, F3 y" m
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
' d5 i2 ~9 P6 I+ b* B   这两个函数在 #include <comdef.h> 里面定义。
2 h& p+ c1 g1 G
; T- |& \) @. o4 B: u② 获取访问 WMI 权限:
0 D+ u  f! L0 G( u/ z. A   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
  S; c: S' i: W8 x' {( g   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
1 J) o& j1 |# N* U: W* R2 P% J# L$ y6 t" Q, U
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:) [/ n8 h/ a+ L  n
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
( h& P3 e' `4 ]9 c  {" P
( i- k0 v7 l$ f3 t4 R. u% N# q+ @* jvoid GetWmiInfo(TStrings *lpList, WideString wsClass)( ]7 _' t3 R4 a) n
{; j9 m4 ~, E4 y2 w1 V
  IWbemLocator *pWbemLocator = NULL;
: f& R( t; ?0 W2 }  _) p  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
* w( v: n3 C5 @/ Z$ j+ C   {2 d& T1 h9 t, G0 F% P1 h
    IWbemServices *pWbemServices = NULL;1 Q, R* g) q1 I9 _5 F
    WideString wsNamespace = (L"root\\cimv2");
7 x/ B; W6 g4 f# W) I9 L     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)8 [1 f: V2 d5 E
     {
& ]3 s( i" \2 T# ^. J        IEnumWbemClassObject *pEnumClassObject = NULL;
2 e7 U" G) F0 T        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
. B( l0 ~- d9 Q        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
5 U/ h6 I7 u9 b- V4 F         {# [' j! B! n! A+ m2 c5 ]
          IWbemClassObject *pClassObject = NULL;
1 n9 T( ~/ y2 ?/ A           ULONG uCount = 1, uReturned;
7 k1 g5 p( L. \           if(pEnumClassObject->Reset() == S_OK)2 J6 R8 ?% S* Z. A5 ?* v
           {
. |# J  C, {7 P7 W+ H              int iEnumIdx = 0;
' C" A- f1 b0 y; B# T              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)9 F! Z- y( Q3 m9 I  k' u3 s, b
              {+ B( p/ V2 U5 z: n
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
1 V/ ^; v0 c" `0 w
, R9 \" j3 ]; A( g+ e                 SAFEARRAY *pvNames = NULL;
4 ]2 h- Y; a: f6 S# P) i  n+ V* U                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)1 N3 _9 ^* W% {4 T" V6 Q
                 {
! {  L  b5 l: J, p' x                    long vbl, vbu;
2 E8 S" d8 d% D5 W+ I                    SafeArrayGetLBound(pvNames, 1, &vbl);: Z* Q. u& \6 _' T
                   SafeArrayGetUBound(pvNames, 1, &vbu);
/ M6 E/ Q/ ]. {, G! Q) c, Q                    for(long idx=vbl; idx<=vbu; idx++)4 q6 F/ \0 \6 ~  l% d) i
                    {
! q5 G) @: M* E                       long aidx = idx;; K3 M# z! v9 L9 X& U2 I# N
                      wchar_t *wsName = 0;5 ?) B0 w  ?  O8 E* F' {
                      VARIANT vValue;
- m, T. o$ L3 f/ x7 h                       VariantInit(&vValue);
* X# x4 P0 o$ ^5 ~# Z3 ]  u                       SafeArrayGetElement(pvNames, &aidx, &wsName);! e4 N/ t$ h/ `* {2 {- d3 o

( F/ V0 l/ D9 l, p( _: m3 r                       BSTR bs = SysAllocString(wsName);
: }4 W9 {$ o) }3 g7 G4 {& y1 F                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
2 F; k" o7 W3 v" l                       SysFreeString(bs);
* J( \* Z6 w: a( ^0 E* ~* a9 L3 e# B1 r9 e9 Z# R  l- R
                      if(hRes == S_OK)
8 y/ {4 x4 x0 ?) R0 ~# j                        {( b/ D7 s/ s4 I& G6 u# K+ w/ \: r
                         AnsiString s;
; _4 `) X1 S8 J                          Variant v = *(Variant*)&vValue;: q( @- H7 H  U% G
                         if(v.IsArray())+ A" r. l  r( ?2 t' e) D
                          {
. A( k: I6 x3 ^* K! i/ X                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)8 c0 W; z" D7 B' e- u7 `/ s2 W
                             {
6 v, l# u5 m, ~  e5 ?                                Variant a = v.GetElement(i);: k+ ]6 X8 j- k. S5 N
                               if(!s.IsEmpty())1 @: B5 R( z" k# G, q- |; D
                                 s+=", ";" y7 g. Q3 |( I, S; r+ j
                               s+=VarToStr(a);
: |! c' |7 c5 b2 l' v3 [                              }. ?8 z# T5 Z, j, o( T: e9 S  S+ Q' m
                          }6 X  d% N3 R' ~4 I- d
                         else$ Y9 a" y% T6 c0 w6 W
                          {
' ~1 c4 z/ [- J- x                             s = VarToStr(v);
9 h9 {3 G* C+ \( j5 ^0 M+ B' G* u5 {                           }. E/ n) P0 Y- u$ A3 h9 o& o' t+ @) v
                         lpList->Add(AnsiString(wsName)+"="+s);
2 [8 ~3 A7 P6 m4 b2 H- C                        }
9 H' X1 y5 r; R' r% J3 h2 @8 @0 L: V+ h5 _) i
                      VariantClear(&vValue);" F% t  l: ]  l1 m' t' o
                      SysFreeString(wsName);6 l% h: Y/ [& q, }8 S6 z: _
                    }- J, T, d# `  m) W$ c
                 }
4 t* B+ J+ C  E, [                 if(pvNames)SafeArrayDestroy(pvNames);1 l6 Y! n6 e1 L! y
                iEnumIdx++;
  I; e1 N3 Z3 s# `/ ]" x               }5 n& N! G+ V& ]+ _  o
           }
# C; Z% V: F5 d. f           if(pClassObject)pClassObject->Release();4 B! N) J* k4 ~8 N) O, D
        }$ F0 C; K1 ]# c- M' y5 `9 l
       if(pEnumClassObject)pEnumClassObject->Release();2 X6 N/ V& q) Y/ Z7 r
     }
; Z. J8 l* `2 @" j, q     if(pWbemServices)pWbemServices->Release();; {0 H6 N+ c9 W9 ?1 D! G
  }9 ]2 ?4 {  b* C* _# r: ]8 R9 h
  if(pWbemLocator)pWbemLocator->Release();
5 @. E3 w* {: Y) d. R}: n( K* K; z$ Y; c4 l
//---------------------------------------------------------------------------$ y& z2 X, w8 w$ \9 R+ W& L
: k' U# j! i( |" m' T) z3 e
// 通过 WIN32_bios 获取 BIOS 信息:2 e( C8 k/ a* Y  ^
void __fastcall TForm1::Button1Click(TObject *Sender)
' `/ b; C1 Q. F( c{, R) o, `  c  B# T
   Memo1->Lines->Add("================== [WIN32_bios] =================");$ A/ c8 w+ M6 M0 G- c
   GetWmiInfo(Memo1->Lines, "WIN32_bios");' X( A$ H4 Y6 d5 h/ t; a
   Memo1->Lines->Add("");0 @; }. r1 k$ w
}
: }+ v1 W0 S/ i
( C* h, w5 N% F--------------------------------------------------------------------------------. |! i0 L% n0 ^( i
/ a' g; K; W! B7 J* b! n
WMI 可以访问的信息类型有:
# ^+ b4 c- Y& p# `   Win32_1394Controller) X; p2 c7 B( w7 r$ A3 [5 _
  Win32_BaseBoard
& C: Z% u+ T! u( i5 V. @  i   Win32_Battery6 e7 U3 e: C5 B# Z& f/ y
  Win32_BIOS. b2 X- s8 l8 u& {0 ?
  Win32_Bus
+ f" J/ _  i) y$ L& m. d* M   Win32_CacheMemory
& q' s# R$ a1 l  z' f   Win32_CDROMDrive
* R" x; S0 x. l  U+ r. s   Win32_CurrentProbe( `0 m( R. J& M
  Win32_DesktopMonitor8 T. I7 `$ {7 e" i" ^# c
  Win32_DeviceMemoryAddress
6 L$ x5 H  y( z9 H9 h* [  V   Win32_DiskDrive" a& b; c$ ?0 A6 D
  Win32_DisplayConfiguration, P8 Y0 U1 G- X& n" Z' t5 w+ S
  Win32_DisplayControllerConfiguration
1 [7 G# s. s6 i4 h. a9 N. W   Win32_DMAChannel
5 D+ E/ H( u! h' }6 y  j1 `   Win32_Fan) T9 @/ n' I% `: Z) v
  Win32_FloppyController2 R, J* F/ e" y; ]+ T
  Win32_FloppyDrive. j4 j3 K  R+ |6 O9 @& i& l2 q
  Win32_HeatPipe+ ~( r* Z8 N! s. s* Q. F$ h
  Win32_IDEController
% c/ j) D) b2 O( J   Win32_InfraredDevice
3 [& @0 T( k- n/ r9 G# v   Win32_IRQResource9 j5 o% o2 w% b. b$ F
  Win32_Keyboard) c0 D  K$ X; \6 C
  Win32_MemoryArray
5 d) u% k0 Z- b   Win32_MemoryDevice0 h' y7 d) d6 v1 F3 }% `
  Win32_MotherboardDevice
% h: r1 w5 M2 [' C   Win32_NetworkAdapter- s0 l; S0 t* e, S
  Win32_NetworkAdapterConfiguration' A$ F8 C! t' x6 Z0 W) l' c" T) k
  Win32_OnBoardDevice# w2 [# s5 @8 ~' v+ ^8 a8 F
  Win32_ParallelPort
1 h' u( p7 W6 d7 B. P& g" A   Win32_PCMCIAController2 y2 _/ U3 V$ B" Y
  Win32_PhysicalMemory
9 N2 W8 a3 b' t+ g   Win32_PhysicalMemoryArray
$ u- J& w( ~* `! \   Win32_PnPEntity
1 X2 Q) [! N- k- V* u: ?: j   Win32_PointingDevice
& ~( g) J  [" h( t$ o! f   Win32_PortableBattery7 U# q& o# e' V% ?3 }
  Win32_PortConnector- o3 b2 Z. V# J+ `* H
  Win32_PortResource" n+ S2 r0 d. {/ c
  Win32_POTSModem
: [8 e8 n$ H/ |1 A: q) C! N   Win32_PowerManagementEvent
' h( \3 f: }: |. a! v   Win32_Printer
4 e) {( O# g" e  s5 B8 Y) X   Win32_PrinterConfiguration2 I" t, v# C7 L. M# u1 R
  Win32_PrintJob
. o3 c+ P" U5 ^+ W& `' F7 ]   Win32_Processor; T! n  A1 M6 H9 w9 I* H* W! i, F
  Win32_Refrigeration
! ^. d- P  Q/ ?6 n$ K& N6 `   Win32_SerialPort( G6 g) U* X# p$ d, K% a  Q
  Win32_SerialPortConfiguration0 E0 C: S  o7 w( G# ]' M* e3 h- H
  Win32_SMBIOSMemory
+ L' p: {+ N4 F. p' a! o   Win32_SoundDevice  Y$ K' I2 |, L# g) C; i7 z
  Win32_SystemEnclosure
1 M% B$ ~! j) ]- Q   Win32_SystemMemoryResource" _8 Y6 |1 f9 Y; ]8 U/ e" n: m) G' Q
  Win32_SystemSlot
# [8 X$ {  ^- t/ M- F, n( Q$ |   Win32_TapeDrive
; S5 |; U! J  c5 V' n  f! a/ k  U: u   Win32_TemperatureProbe
3 j/ J4 E  K3 D* L   Win32_UninterruptiblePowerSupply4 g# w/ v8 D4 \+ G+ Y' s
  Win32_USBController: |, o! }. I8 b
  Win32_VideoConfiguration
, G3 x. c, R$ E1 Q: |. m( E   Win32_VideoController. j& e9 ^2 B, {* I6 F
  Win32_VoltageProbe
* N: C2 p& r! U" i
8 L# J/ T- T4 a. R$ Z0 N1 ^! I以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-28 09:08 , Processed in 0.024775 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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