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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
, X$ c+ E" {% G7 U& t
+ p9 N/ E: V- q( O' x7 l
4 Q0 u( p, W2 n--------------------------------------------------------------------------------" M  d2 \  f# N9 g( m3 l# x
WMI: Windows Management Instrumentation (Windows 管理工具)1 p  U4 r. R1 P1 A; G
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 ; _+ a2 j  v9 l
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
1 V( S7 s" ?* s; x   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 4 [+ f. \. j+ K. S1 ^, [$ b
" z" s  M9 j. C$ |0 y) Z
--------------------------------------------------------------------------------4 H8 i% B4 w$ a* D/ z3 T
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面3 D: w' \! B) q/ U& k% T
" {$ Y- c/ D* ?0 k) U
--------------------------------------------------------------------------------7 v) g! w# h; }0 C
① 初始化 COM 接口:
& ^9 E. G" }: _1 l# P9 ?$ [/ U) a   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
* y2 ]# ?9 T) f: @8 `- ~   这两个函数在 #include <comdef.h> 里面定义。6 g. A4 H: R+ w3 K
) R9 t1 v. U* R7 Y& |; }
② 获取访问 WMI 权限:+ c2 G  X: w3 Z6 @8 B: [
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
' z% X" Y4 I+ E1 V   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
: D" s$ A! L! X2 a, T% M% l6 Z2 I$ t& K5 `1 w
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:# B- x5 L, p+ d9 v! x6 h
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
/ X) P# m( W6 J9 i# R
& p  k9 Z7 ?$ p- v  A) w# u5 lvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
7 H$ u& \& w+ N4 W* [{2 s, Y( V9 `9 r" x, x
  IWbemLocator *pWbemLocator = NULL;
7 s( j0 z  K7 ~! \! A  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)8 U% w3 U8 u4 n* n9 ~
  {
' T& _  w. C# |1 }# \) Y0 W     IWbemServices *pWbemServices = NULL;# d4 a: ^6 Z! @' h
    WideString wsNamespace = (L"root\\cimv2");
; v6 n% h% W0 [6 J" |/ |8 J  E1 N     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
; ^+ f: N' ^* M      {
* p" c( S9 V1 T, Y        IEnumWbemClassObject *pEnumClassObject = NULL;
$ p- a: N1 q& I2 `$ S2 S( J& u        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
5 _# P0 A' x4 F: `3 z  y1 y        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)  x0 Z5 z- P/ D( ]
        {
& G, r2 b5 F% h5 N6 @           IWbemClassObject *pClassObject = NULL;
. A, B6 `3 {2 ?+ w0 d           ULONG uCount = 1, uReturned;
. ~6 F2 {2 k% _! w1 g5 S/ W           if(pEnumClassObject->Reset() == S_OK)" U' {6 z2 ?% I0 t: K
           {
2 J% n* n! i8 G, k              int iEnumIdx = 0;& \5 Y* K6 W5 ]0 U4 s! U+ u; K
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)9 y7 l  o5 q% t$ F
              {
- \) ?1 Y( e6 P7 R* o                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");; x, g1 K. @5 y

  }# ^* j8 e, B5 N# c1 }                 SAFEARRAY *pvNames = NULL;! f* s; O! r8 P0 C. `
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)( o: {' M* t! ^, i5 ?4 a) p
                 {. R: o& ]7 p" ]/ X# U% w* Z- ?# Z- f
                   long vbl, vbu;0 g  _+ j) V7 a7 }+ @6 G: J
                   SafeArrayGetLBound(pvNames, 1, &vbl);
6 @4 I4 Y1 Z2 D                    SafeArrayGetUBound(pvNames, 1, &vbu);
( |/ F( y2 ?# m0 b% K0 q9 I% b                    for(long idx=vbl; idx<=vbu; idx++)* X& R5 k( z6 _" Q" r5 ~8 r
                    {9 v' b, d% w  H; \
                      long aidx = idx;/ N" f% f1 v4 A+ D( M6 n
                      wchar_t *wsName = 0;4 f- }7 V1 P2 }
                      VARIANT vValue;
! y$ C( z, j% A2 h& n. X6 m                       VariantInit(&vValue);9 A0 @- z/ U# }  @: r  L
                      SafeArrayGetElement(pvNames, &aidx, &wsName);
) X+ Z4 w( A9 Y) L" U$ ]: H, ~3 W& a/ x  @, a
                      BSTR bs = SysAllocString(wsName);2 {' P. C( X& i  j' n4 \
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);5 S2 W& d2 h7 }* v; M
                      SysFreeString(bs);
& o+ z7 _( p" j& k% `  c! |' d
                      if(hRes == S_OK)
" V! H6 F7 Z4 M, J/ S# Q7 |                        {
% i+ H2 ]. m5 I( t8 |                          AnsiString s;
( ?$ G6 d5 O; Q* S' ~                          Variant v = *(Variant*)&vValue;
* F' Q) o- ^1 Y+ J! Z7 C                          if(v.IsArray())  G8 q, d; I7 C0 R; a
                          {1 X0 L8 n# E$ _; E7 P. b
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)1 X& a8 F' e6 u* i2 A
                             {  V8 a0 Q6 b* [7 r. m8 t
                               Variant a = v.GetElement(i);( @2 T: z  W* N( `" g. l1 h6 p# d
                               if(!s.IsEmpty())1 d: ]6 E' ?2 `& n
                                 s+=", ";
( W  j7 z% B& G2 |                                s+=VarToStr(a);" j0 R) x. X$ C9 m- V( P) H3 G5 q* p% d
                             }
4 t/ }% Y  {- b9 @7 k+ d                           }/ C8 ~% y' f/ c, W- J
                         else
& X0 r# g4 I" }+ K8 r& L- ^                           {. l) l, Y( g  ]* s4 [" J; \$ ?
                            s = VarToStr(v);
0 e1 l- k2 g) f6 P                           }
% o' |' F, u& g/ U+ A0 R" |                          lpList->Add(AnsiString(wsName)+"="+s);1 K- s& A6 X$ G: T
                       }
- d" F$ D( e; v5 Z/ @! d1 w; Z4 M. D% I6 ]
                      VariantClear(&vValue);, D, O6 D: b) B3 c0 n8 X
                      SysFreeString(wsName);* [; J& [) O' x+ q
                    }. I3 s$ A0 X8 v# G* ]7 |5 O
                 }' \/ d+ P4 @" S2 c" ?. L
                if(pvNames)SafeArrayDestroy(pvNames);
5 k# A" o. S, [6 ], I( o) o                 iEnumIdx++;0 m; o# J$ u( t+ ^* b  L; s
              }
$ J4 c+ E0 Z" Q+ `/ D! O            }5 i$ a& |( `$ A8 `9 [) _( E
          if(pClassObject)pClassObject->Release();
. d) u8 e: Y! l6 ^. X+ t         }, d" V2 _7 `3 i3 J1 L* J; D
       if(pEnumClassObject)pEnumClassObject->Release();3 u* A& T. ?& ~. b+ W7 x
     }7 L& Q& |3 Q& k& Z1 A$ G
    if(pWbemServices)pWbemServices->Release();
5 \6 y, d- R4 S5 [   }5 n9 |+ q7 ~* G' i  R6 h
  if(pWbemLocator)pWbemLocator->Release();
" V5 r3 q- {* Y. K  [5 {}1 G) U# s! z/ T- b
//---------------------------------------------------------------------------
% H1 w, Q8 y0 A  q4 {& }
* C. c/ ?: B  H0 t) h+ u2 N  @. p// 通过 WIN32_bios 获取 BIOS 信息:
/ m2 |: H$ q5 f9 j* @void __fastcall TForm1::Button1Click(TObject *Sender)+ f( _0 ^9 I. L
{
) F$ t, ?. r) T( N    Memo1->Lines->Add("================== [WIN32_bios] =================");4 c8 e! j8 t  K- D- O. E
   GetWmiInfo(Memo1->Lines, "WIN32_bios");8 c4 c2 Z# D- g, W/ N7 U8 ?7 I
   Memo1->Lines->Add("");
, ~0 d# E+ L: \  \! x}
  ]$ M1 U; y$ w  [( x
' @7 X# d! d, m# C/ X2 M--------------------------------------------------------------------------------
; M$ s) u; U0 ~/ f! t. ]: N. C# x* p# q
WMI 可以访问的信息类型有:
0 t! |( U* w8 ?( j; k   Win32_1394Controller9 y: p( }0 x! {5 o2 R' V! G
  Win32_BaseBoard
* T/ F% U, X9 b1 t   Win32_Battery6 e8 n2 S. U0 q$ T9 {7 l1 X& l
  Win32_BIOS
0 f7 W1 F' ^$ I& D2 r' [5 v   Win32_Bus% a, \5 A! C' v+ T$ P
  Win32_CacheMemory3 d- L3 t4 Y- y. Y
  Win32_CDROMDrive
$ B; a3 b  Z2 r( x   Win32_CurrentProbe& h; o) |$ e5 w/ B  ~! b1 X& u2 S
  Win32_DesktopMonitor2 u& H9 q& _( c$ V. z7 x  D
  Win32_DeviceMemoryAddress; N8 w8 r4 g- r  W0 h
  Win32_DiskDrive" ^  g- J$ }, \
  Win32_DisplayConfiguration
" B/ T6 U. V) }- U9 |   Win32_DisplayControllerConfiguration- o( V- [- S- @+ d
  Win32_DMAChannel8 B& }, T( ~/ `6 l+ `  X9 u: D3 M
  Win32_Fan! z6 W5 v( y  C2 V( z0 x
  Win32_FloppyController
% ^- ]0 @$ T! M* E   Win32_FloppyDrive
* o+ |# w7 r! e/ W   Win32_HeatPipe# s# F, K& l: p3 K* ?
  Win32_IDEController
1 S+ ^6 w; K$ A$ |4 D1 a. d. s   Win32_InfraredDevice
+ d+ j0 u: d8 Z, {0 c& L   Win32_IRQResource
' y) r* d) a4 c/ a   Win32_Keyboard
$ {4 i3 x5 Y! ]  N! w) L7 P   Win32_MemoryArray
1 J' `/ p7 P; S. l4 N   Win32_MemoryDevice3 y9 z' B6 Z3 S5 I& D. \# M0 C
  Win32_MotherboardDevice3 _5 w$ u% S: {7 j
  Win32_NetworkAdapter1 D9 J& E  U. f9 }* C7 j- r
  Win32_NetworkAdapterConfiguration& E1 H3 |; b5 y2 P, i% Q2 v* O" P
  Win32_OnBoardDevice; b6 ~7 e( Y# h" R5 U9 ]
  Win32_ParallelPort
3 y7 g; {7 [7 q   Win32_PCMCIAController
' [0 n3 A- n* s  u& V7 P   Win32_PhysicalMemory
/ o, _' N7 J1 I' k# w; `/ j* M$ @   Win32_PhysicalMemoryArray
" Y! Q, d3 @$ R& ]4 Q4 H2 u9 y( O9 N   Win32_PnPEntity
4 f% d9 ?5 K$ O$ z4 c/ F   Win32_PointingDevice
( c5 R1 a% N3 b0 f- X' p0 |   Win32_PortableBattery
' ?% Q' ]0 l6 \/ X- j   Win32_PortConnector
( e( B. P. F- f' k2 z# s  S   Win32_PortResource
8 o/ [5 J: Y# B! ]7 B   Win32_POTSModem3 }% t4 g1 o) Y* z
  Win32_PowerManagementEvent" `; r- s  z' l6 Z- F9 ^) o
  Win32_Printer
2 K- ]* A5 l) j8 [7 c   Win32_PrinterConfiguration
9 A% \! d9 q, e0 b   Win32_PrintJob
" o$ [2 ~4 C0 t& |5 Q0 Y1 j1 H   Win32_Processor
; d" e" h! n; w2 ^; @! c0 m& F/ {   Win32_Refrigeration" |$ w( T9 i. I" ^# X( @
  Win32_SerialPort
6 s- l& G" a8 s, R   Win32_SerialPortConfiguration
' s7 Y1 k- e5 |   Win32_SMBIOSMemory
/ o% [: j( R3 }' \' X% q& x" X# u   Win32_SoundDevice
0 t" H9 ~' J% D0 ]! l2 U   Win32_SystemEnclosure! S* s3 g# U" {5 L/ C
  Win32_SystemMemoryResource
* ~% q! W7 F5 b/ r   Win32_SystemSlot
6 O3 j- }! E8 c   Win32_TapeDrive
  C8 s2 g% h6 J! ~5 L   Win32_TemperatureProbe
+ G2 i( \, c0 q( r% i2 s   Win32_UninterruptiblePowerSupply
$ {! e8 |. a* M( t4 d; g% ^9 p" ]   Win32_USBController/ C" |8 X" Q8 J5 W7 {: R
  Win32_VideoConfiguration
5 x6 T+ `8 G) f9 B, {. T   Win32_VideoController5 ^0 E2 K2 n1 F
  Win32_VoltageProbe
: `4 u. y7 R2 [9 B8 v+ c+ c  w) f' N5 O8 ]5 y; }( u+ w/ V7 x
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-30 21:52 , Processed in 0.018518 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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