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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)6 w' q2 o" d4 H" C- q' ~9 g
- O  F# A+ v% C
$ a2 L$ B& q  O" f
--------------------------------------------------------------------------------
& B/ a5 G$ `. c4 lWMI: Windows Management Instrumentation (Windows 管理工具)
- P8 z! |' P3 [9 G4 c( Q' F   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
6 I6 f, S5 ^  r* K# \   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
/ W0 t" n  B" l: Q8 w0 W0 f   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
- x) ]: b$ X5 a5 |! S! M0 I2 Y5 q' Z( A4 R3 E
--------------------------------------------------------------------------------& n+ l, X% e& [6 S4 s7 U
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
( x* C5 h( R' K2 n0 u7 L% y7 n0 R8 @
--------------------------------------------------------------------------------
. M5 ~- Z! t9 E- G2 @① 初始化 COM 接口:- ]4 V" B' w7 }# z4 W
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
/ d9 n8 i5 |% D   这两个函数在 #include <comdef.h> 里面定义。8 G% f3 F3 ^% x; Q. n
+ H. j' F& P# E$ e# Q* P7 @
② 获取访问 WMI 权限:5 z( i. k( z% T
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);, @& B3 O% P) o7 P, O" W# p
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
$ m) K. p( }% n8 m9 E8 s+ O
& K6 J% X( y8 v1 g# j& R③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:: y! x; c9 M& E
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。" o& }: ~, H! a2 W  L: R
( L5 i+ M1 Z' u0 C  ?2 q+ m
void GetWmiInfo(TStrings *lpList, WideString wsClass)# ]/ g) K1 k, A) b2 Z
{5 U0 \' o- z8 d$ @
  IWbemLocator *pWbemLocator = NULL;
& l9 u' O" B" I  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)# I0 e7 C# ^. b9 J/ p' y) ?
  {, J, `4 @+ N1 L: q; h1 d
    IWbemServices *pWbemServices = NULL;8 x# A* `" ?+ g9 n: f$ T/ @
    WideString wsNamespace = (L"root\\cimv2");
  S! {- c1 B* Q2 k- Q8 D     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)2 n" e  O8 y8 }: r
     {/ n% S+ c" T( U+ u, t
       IEnumWbemClassObject *pEnumClassObject = NULL;9 _, @/ d; a8 z
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;$ z) `2 p+ }0 h7 c  F
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)6 e: R  g; h9 M8 v
        {( p& S& c9 L0 H3 E. @
          IWbemClassObject *pClassObject = NULL;
2 I/ T& c& E, n           ULONG uCount = 1, uReturned;
# t1 H# i* t2 p* g' ]           if(pEnumClassObject->Reset() == S_OK)
; H$ e; {5 _* ^* t( q5 x            {$ S  N; ]( W5 @8 u
             int iEnumIdx = 0;6 A/ N' B; N6 N0 Z) i* r: X
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)% H* X" L* [" Z2 \; ]
              {
& A' p& v- F6 Y' C! S7 W. c9 g: J) r                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");" O) T) R! x  v/ i

+ h$ x0 k$ m& W                 SAFEARRAY *pvNames = NULL;
4 @* h5 H" m: r# Y                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)0 F& x6 N; m; \. K. U  K' k- I
                 {. R: E+ Y" O8 d9 s
                   long vbl, vbu;
$ n) q7 v7 p7 R: l                    SafeArrayGetLBound(pvNames, 1, &vbl);
4 @4 W& B, j8 L                    SafeArrayGetUBound(pvNames, 1, &vbu);/ O" ]+ N4 A3 K# l' e; H' A' I
                   for(long idx=vbl; idx<=vbu; idx++)
6 u6 K7 o1 V( a, d3 O                     {! S( F( `& h) q3 h, C. Q1 b& L
                      long aidx = idx;, _, c5 F7 P% H2 }. T1 T! m8 S
                      wchar_t *wsName = 0;
$ J' i$ M* D# N. k                       VARIANT vValue;
* D% S5 M3 R8 H! ?3 Y! c                       VariantInit(&vValue);. T* z5 \- O2 r* M# n+ u+ w
                      SafeArrayGetElement(pvNames, &aidx, &wsName);
9 H: f/ \2 S; y
6 {+ o) ~& `  S& x5 P6 @" m                       BSTR bs = SysAllocString(wsName);
7 G. {* f4 @4 `" p. @, Z                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);# C7 {+ N8 R! ^/ b
                      SysFreeString(bs);
; M& a6 P1 e0 o8 d9 p2 x: I3 R0 c3 m
                      if(hRes == S_OK)
5 v3 S; u- e* N" i& B                        {
4 |! K$ R6 d+ m/ i                          AnsiString s;1 K/ E( y9 ^4 {4 C
                         Variant v = *(Variant*)&vValue;
! h& S8 U  K+ |                          if(v.IsArray())
1 T, C* I( \3 L7 i                           {
9 N% A' p  x9 K, q! B                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)% [* V3 J' Z6 E& s
                             {; Z! V" W3 v0 W" w+ q! }! V
                               Variant a = v.GetElement(i);$ h- A, J6 E3 ~" a% w" y
                               if(!s.IsEmpty())
8 i$ q2 @8 l& T" r. V+ Y: |                                  s+=", ";9 `9 y8 w8 G* t: q/ i" `, A1 {
                               s+=VarToStr(a);6 Z$ X! I8 Z: d
                             }$ W' \1 [7 ?+ v* Y  Z
                          }
# ~" m' F' d4 I* l9 O, ?7 A                          else
/ U  o" \* F) l) n7 ^' I5 b                           {1 r! f0 N2 Y( Y7 E: B$ ^
                            s = VarToStr(v);$ [# K1 V& J: l7 g
                          }
7 U4 `5 {9 e5 _  m                          lpList->Add(AnsiString(wsName)+"="+s);
4 _4 |' U' `6 x' a# y0 [7 m                        }
1 M5 |! v8 \, t8 [# ]. y. J7 T, e2 G
                      VariantClear(&vValue);
1 h( I0 |' L/ b8 b6 Q2 x                       SysFreeString(wsName);
4 h+ t9 H) M) W: y2 w  l9 |                     }
1 R' a# d$ d* h4 E0 |, r( [" ^                  }
+ s& a! f' f0 k  {4 ~                 if(pvNames)SafeArrayDestroy(pvNames);
$ V% }# V) o9 B# f& o8 `7 c                 iEnumIdx++;0 s4 `: P; M5 E' r9 C
              }+ t4 r. m, K& g9 i1 _8 u! v2 z4 f
           }
, z  I6 L6 o0 B! P) B( u           if(pClassObject)pClassObject->Release();( m8 {. Z: I! y) N: r4 P' j7 u
        }
6 p1 p2 L6 b" i+ l0 l9 _; E+ I8 N        if(pEnumClassObject)pEnumClassObject->Release();
; S# {8 M  a9 }+ l$ x0 a6 |; g6 @      }
+ r, ~# }, a$ w! `7 M* h     if(pWbemServices)pWbemServices->Release();
. h8 u" e& r! d) r; \) _* }   }4 i) n# R: F* U; s
  if(pWbemLocator)pWbemLocator->Release();% W' }- q, r- y5 E
}
+ C/ Y. d+ \0 s# r4 q% ?: M+ @" U//---------------------------------------------------------------------------, I; {& X$ A7 N: A& y

  l+ `  o0 i. X- \  c// 通过 WIN32_bios 获取 BIOS 信息:, ]5 |( ~/ @  E2 Q1 {
void __fastcall TForm1::Button1Click(TObject *Sender): u! d/ v; q8 u5 R) J4 B
{
, d0 l1 @/ L' P) ^    Memo1->Lines->Add("================== [WIN32_bios] =================");
1 O/ L* d/ K8 \$ p6 D3 }5 x9 e' i, U' o    GetWmiInfo(Memo1->Lines, "WIN32_bios");
- b5 x5 @/ [5 d6 s1 E( N6 `    Memo1->Lines->Add("");
9 b+ D3 Q, V% M# Z}
( G) v; N4 f7 _& ]1 T! g" j0 b8 W4 ~4 C( j, F2 Z$ U/ q! {- u
--------------------------------------------------------------------------------5 _: N# ]6 N$ D# v& W% d" X

/ ^1 f, V7 S& c/ i) t/ wWMI 可以访问的信息类型有:
# [- r+ n' E: S% R% X' H   Win32_1394Controller" D) |- S# x1 x( f
  Win32_BaseBoard" K6 O$ t9 D0 d1 m, d' B
  Win32_Battery
$ N2 g) d7 r. h' o6 ?4 C1 @/ ?. x   Win32_BIOS1 K% l7 d, ^( x) i0 K
  Win32_Bus
. M& x. W# E4 y3 r9 u   Win32_CacheMemory+ C3 i. K7 Y7 o# A* e2 H, d1 l
  Win32_CDROMDrive
  p; S, r2 x2 A9 \   Win32_CurrentProbe
4 O" @; k! O& A: I, g! J5 x   Win32_DesktopMonitor7 Z* B( s% n% n6 U, |
  Win32_DeviceMemoryAddress  r1 b$ f: k0 F# D
  Win32_DiskDrive7 N/ g) d8 c- W4 S2 b  w4 U  T
  Win32_DisplayConfiguration! v( G2 e! ~3 s7 G  H
  Win32_DisplayControllerConfiguration
+ O1 |& R- v- U7 Z   Win32_DMAChannel
( B1 t/ _" F9 r- Y( }   Win32_Fan5 t% z9 Z, r$ }  U
  Win32_FloppyController8 n- ~* [$ M& j
  Win32_FloppyDrive* _$ R1 x  W0 }( i/ W2 U
  Win32_HeatPipe
+ t7 n4 g) {3 ^9 v7 k* F; ?   Win32_IDEController! Z; \" k4 C) w; e0 I$ G, w
  Win32_InfraredDevice7 X, X. p6 B6 s
  Win32_IRQResource
6 P9 E) e' G3 i' X8 c5 U9 r   Win32_Keyboard
" l. p/ X! y6 ~2 z0 J   Win32_MemoryArray
4 ]( \/ U; s% x$ a8 L2 Q   Win32_MemoryDevice. a8 K1 j4 O- D- v
  Win32_MotherboardDevice
% g7 ]+ P+ d- I$ f+ I. \- N   Win32_NetworkAdapter
( T  D) x9 }/ P   Win32_NetworkAdapterConfiguration1 l# N! T) z* T" K* }# {
  Win32_OnBoardDevice
$ L5 D/ ~0 u+ ?& F3 l" _; h   Win32_ParallelPort
5 P  F! _6 {% ]( K$ W3 D* x2 F   Win32_PCMCIAController
: ?5 O3 @/ f, E2 W2 Y! H1 q   Win32_PhysicalMemory2 p* Q# c/ @0 R3 T. H! Y9 q
  Win32_PhysicalMemoryArray
2 V( `1 q# s! t! x* d: P2 |3 t   Win32_PnPEntity4 W, U: |9 P; T
  Win32_PointingDevice
# j9 G9 W& e2 W# j7 L* \5 H  s2 B   Win32_PortableBattery
, s8 i1 a+ }" _, v   Win32_PortConnector
1 U; t5 s7 v: r) Y   Win32_PortResource  s" V2 f3 C# k6 g$ P
  Win32_POTSModem$ L$ I, N, X  ?4 D( V; ~
  Win32_PowerManagementEvent5 ^- i; h! p0 R1 `
  Win32_Printer3 _" G% A! M: ~9 K
  Win32_PrinterConfiguration, E7 X/ n. }9 N" D! {) y
  Win32_PrintJob
( X9 x/ _0 f1 d   Win32_Processor! z  r; i% s4 d" ]( X
  Win32_Refrigeration
! T1 c  C" v' \7 n   Win32_SerialPort
# L. S1 E+ y" ]# ^   Win32_SerialPortConfiguration" y4 i$ O. [: u  m: x- _
  Win32_SMBIOSMemory$ h& d+ _0 [% }  y- a
  Win32_SoundDevice
# i) |1 q0 @  c" l; T   Win32_SystemEnclosure
: J' l  Y2 R, k! M# N& j6 ?   Win32_SystemMemoryResource  y9 C. J/ J' n" Q
  Win32_SystemSlot$ y- O; X  B7 l
  Win32_TapeDrive4 n) |: s, F9 S% ?/ B
  Win32_TemperatureProbe
! O( ?: d" l/ N( D$ u9 E   Win32_UninterruptiblePowerSupply9 E3 @4 k' M% k; O
  Win32_USBController
" H+ ?# W. _- V/ \   Win32_VideoConfiguration
% h- A% _+ V9 |0 }8 T! d( t   Win32_VideoController5 z4 i8 N; S9 p: r/ h
  Win32_VoltageProbe3 X. j! ~8 B& M: k
  t/ x2 x9 Q. s5 d3 S* p. m% y; l
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-30 07:08 , Processed in 0.019281 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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