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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
$ N* o0 n- o+ l- }5 a- S4 a6 S2 x+ E& E6 {4 |
6 o& Y6 I/ @7 k3 a7 ]
--------------------------------------------------------------------------------
1 v% n) W, o, k- o& V% n& }$ k4 XWMI: Windows Management Instrumentation (Windows 管理工具)
1 Y8 |) N& v- [1 S# I   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
& S4 f1 o* R+ d! z: k! Y) t   利用这个工具可以管理本地或客户端系统中几乎所有的信息。: I6 e# W$ k/ z; Q( L$ u
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 ! A; i8 i  e: R
) G; J) J) w- V$ g0 L5 `
--------------------------------------------------------------------------------
( w; V- b" j  L" p# {5 v% q* TBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面1 r3 @" R: C" }4 W
# ^2 M/ e1 a1 x/ S" R3 g2 S
--------------------------------------------------------------------------------1 w: y, n9 z3 p
① 初始化 COM 接口:. n& J7 L9 s/ U4 u
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
& ?; z% ]' @3 Z: b   这两个函数在 #include <comdef.h> 里面定义。
! N+ p* @7 V) U! }3 j* Q" r3 C. k
+ n$ d7 I6 V/ C+ ^2 y: ]1 j7 ?- t② 获取访问 WMI 权限:
0 w* G7 k# b2 C   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);+ Y6 n. r. _  l0 A$ h! v5 K* b) G
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
4 k( ]; q* g) |
( g! h! Q# c! |# g1 ^, ]% V5 b③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:9 J2 l; _9 A  }; ]0 V. T
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
8 b% A- V6 U7 S
2 \0 }  c& L" f& c) H5 s# N6 Mvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
6 _6 s# a/ _- p- W! E# I9 t{8 q# G4 C' F4 E4 q$ F
  IWbemLocator *pWbemLocator = NULL;9 |) K* C* M( {( [: S' e7 p
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)2 ?1 O$ c2 W  h4 v4 A( E1 Y* W8 K
  {( g* p' P- {5 z% \& ~- `: h- ^
    IWbemServices *pWbemServices = NULL;" W5 l, c; ~3 {( G
    WideString wsNamespace = (L"root\\cimv2");
/ I" }) J% r5 {% c9 y+ k) L2 G% C& x     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK); ?. F; [% n" L! ~: X
     {
1 R: {' G4 M0 Q7 p6 R" W  m& O        IEnumWbemClassObject *pEnumClassObject = NULL;
! y" c# J3 H$ B. W; S        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;) `8 N3 {# t- X4 w& }; e
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
6 N* r) _6 ?3 X3 F         {
7 x- Z! Q& d. S0 ~           IWbemClassObject *pClassObject = NULL;
, r- x; O/ h  M+ x" W3 t6 |# {           ULONG uCount = 1, uReturned;
/ T9 m) ?' @+ a/ ~5 J/ C           if(pEnumClassObject->Reset() == S_OK)
+ X: v$ V+ s& Y) _5 Y: K            {
% s$ n9 k* @/ u3 k6 U5 H              int iEnumIdx = 0;
- A1 A( ^8 T& i% Y3 v              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
4 d+ y, C4 e8 r5 \# L% |               {: X8 Y& Y9 R- s/ w
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
" G+ t4 U0 ~/ w8 v6 L( j. j5 j& v/ U, v8 Q
                SAFEARRAY *pvNames = NULL;1 h) V8 x% Y8 w- u
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
9 O2 U# D" h) [                  {
2 L: c& d% _/ [3 J) D5 Z                    long vbl, vbu;6 D+ ~* A5 S& W! P; ^' T, `
                   SafeArrayGetLBound(pvNames, 1, &vbl);
/ G) Z- \8 \- ?3 ]& I/ U' S9 D                    SafeArrayGetUBound(pvNames, 1, &vbu);5 ]" r- _* ]  f  }5 g7 K6 [
                   for(long idx=vbl; idx<=vbu; idx++)( Y# L5 T+ C& T- b$ U
                    {
5 ]: M1 x. M6 x4 p1 N* p                       long aidx = idx;9 i! H0 J' f" J7 V. r: |
                      wchar_t *wsName = 0;
" ^' X. E: z# h) [" ^                       VARIANT vValue;
$ Y: {+ o9 `! U0 k                       VariantInit(&vValue);
/ ]: @" s2 A& u3 G: f2 B3 G0 P                       SafeArrayGetElement(pvNames, &aidx, &wsName);! |3 ^& G; i9 P' D8 f0 V* B
( J- Y) {1 m: x- w* ?# i# P( h
                      BSTR bs = SysAllocString(wsName);+ w- z5 h# }! A
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
, @. q3 p2 q! N. b5 b) m                       SysFreeString(bs);+ J/ \3 J+ b+ K& _+ h, g6 ^

' i, u2 t/ r8 [0 e% P4 }) u  b                       if(hRes == S_OK)
  h5 B: @2 g* ~7 }# ~2 Z- b& d                        {
: e/ A8 z( D- K  \/ y/ R                          AnsiString s;. [0 g, @# R& B. \: M  F4 `4 i
                         Variant v = *(Variant*)&vValue;
; c: k, J4 h& f, _0 N                          if(v.IsArray())) d' s: O$ g. o+ h
                          {! s( ^0 y# A$ l' J
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
) t+ E. ]7 V  d' c+ A! p4 M0 q                              {
; q6 R! Y: w6 X( G) C                                Variant a = v.GetElement(i);
4 p: ^, I6 C+ d                                if(!s.IsEmpty())
4 a+ L7 _" f' F' s                                  s+=", ";* n0 W- l& d$ d) k0 x
                               s+=VarToStr(a);
0 _4 k% M1 T4 A9 b: Z  H0 \( @                              }
- K$ K( \0 f, \1 T                           }
% h* Y( b. H' K& ^& k0 C9 c                          else
2 u5 c7 C; x- G                           {
5 u6 A/ w0 x8 _" K$ t0 Z% q, C                             s = VarToStr(v);
% t( z2 ~7 O& h                           }
0 N3 I: q" S) C8 S1 ]+ P                          lpList->Add(AnsiString(wsName)+"="+s);5 h- F2 C, w# S- G
                       }  K6 p$ N% q' `
& w0 S  B+ t! \. b9 F3 j' V& ?
                      VariantClear(&vValue);
' I; i5 j$ z! l% z  Z) U                       SysFreeString(wsName);  B  ?2 |, @. E- d1 q9 m
                    }
4 m% v) o7 q' `9 Q; ~9 q: L                  }
9 t* @9 n" ]- B- ?/ W                 if(pvNames)SafeArrayDestroy(pvNames);
0 h' W- m' _# k  ?5 I( ^1 ^                 iEnumIdx++;/ s8 H. [) y. o7 O
              }
& t; U" r) O: \6 V( X; d8 l# l            }
  ?/ D6 O& g! o/ S1 J" N' S% G. v           if(pClassObject)pClassObject->Release();, z2 I- s1 U$ r5 U% ^0 t* Y
        }
- Y$ u6 f/ U, D0 y) `        if(pEnumClassObject)pEnumClassObject->Release();
( c: V+ ]+ d" u" p; @      }% `7 P$ |/ X3 Y7 B
    if(pWbemServices)pWbemServices->Release();% P* O1 \$ {: i# G- n
  }* J3 g' R1 q' O) w
  if(pWbemLocator)pWbemLocator->Release();
3 v- x$ ~2 R0 j: y. A* G* l6 r3 s}
$ v9 C' Y& Z6 y' }//---------------------------------------------------------------------------
4 s& g5 s: w. F
# T8 f' B/ x! @7 h1 J9 x// 通过 WIN32_bios 获取 BIOS 信息:
! x+ r5 `! M# ~4 Q! ~; i( @void __fastcall TForm1::Button1Click(TObject *Sender)! D/ {) u3 b) D& `2 m. y2 y
{# {; T7 N# q8 t6 g( e
   Memo1->Lines->Add("================== [WIN32_bios] =================");( Y1 p# z3 x9 J  Q) t# s1 G
   GetWmiInfo(Memo1->Lines, "WIN32_bios");3 [7 p' b; r7 M3 O, O- Q
   Memo1->Lines->Add("");
+ b0 l+ x2 k9 A- H: ~# j* n}
' X" v+ G2 ?- V7 s# {, i6 g7 R* v/ R
--------------------------------------------------------------------------------
$ e. H# O1 K, a
% e) G- ]- z6 ^3 s+ a6 NWMI 可以访问的信息类型有:: Q; |6 c" v& v
  Win32_1394Controller
& p# T: l! l; Y% F  B   Win32_BaseBoard; r2 s: F0 e  W3 b
  Win32_Battery+ y* [7 o* z2 Z9 S
  Win32_BIOS* r) z+ D* X; b3 a/ r
  Win32_Bus
& z! c( Q  U5 V; K1 ~, E- ]   Win32_CacheMemory
- e6 Y2 T" K9 o5 z: a; r4 w   Win32_CDROMDrive
# W, f) g9 N) }8 f/ N5 e& z  g   Win32_CurrentProbe# N5 k& \8 Y  Y$ a$ D$ l
  Win32_DesktopMonitor
9 D+ ~* J) X) [- j9 P   Win32_DeviceMemoryAddress% f# n, O/ ?: j+ K0 ]
  Win32_DiskDrive# c8 F$ T3 l7 x5 O
  Win32_DisplayConfiguration5 p" B2 }6 K! H; `3 p* b; g
  Win32_DisplayControllerConfiguration
  d$ k% a4 T4 N8 q5 M2 m6 B" h   Win32_DMAChannel
: u6 p1 q/ W' J, l   Win32_Fan5 ~( T: d! y* P* w% D( h& c( p, |" D. \
  Win32_FloppyController9 F0 U3 |+ Y3 D+ u) t9 M
  Win32_FloppyDrive5 X7 S$ y( ]' P$ P# M# r
  Win32_HeatPipe
# F0 ~& e0 [% n7 ]+ L+ L/ J9 C$ r   Win32_IDEController9 f3 |. s0 _  |; S2 ]8 @
  Win32_InfraredDevice
  \/ R* Y' U: L- m   Win32_IRQResource
6 g/ n! P, o- M( C7 N: P   Win32_Keyboard3 T/ Y! j) k# I7 e% d+ s" ^
  Win32_MemoryArray0 z% ^2 K6 G, i) n( z6 [2 J
  Win32_MemoryDevice$ u7 \+ b# d, ?+ Q
  Win32_MotherboardDevice
- n% [7 a: p( d( |  i  L/ A7 G   Win32_NetworkAdapter* s8 l' s/ E5 E! M
  Win32_NetworkAdapterConfiguration
0 t$ Z3 {% X, {2 d' F1 E$ r   Win32_OnBoardDevice: i) k' \. j: W3 t. B1 T
  Win32_ParallelPort
. N$ O) m- [- k+ W   Win32_PCMCIAController! \( H  m5 }' o2 p8 A
  Win32_PhysicalMemory
5 t! _2 V) e) u$ R/ H3 X   Win32_PhysicalMemoryArray. _+ d4 D4 N1 i5 H+ j8 e& n
  Win32_PnPEntity) i, [- F4 k9 \! h
  Win32_PointingDevice- I& a/ u$ S9 j9 e% C
  Win32_PortableBattery4 c  f* @3 o3 W. s/ J+ g
  Win32_PortConnector
% ^# D" X% n3 O0 `& {, [- p1 L5 A; }   Win32_PortResource
1 [3 S( e/ `( r& ~) n   Win32_POTSModem$ l5 o; D# N7 O& J) b6 N, ]
  Win32_PowerManagementEvent
5 r" I0 Y( N# m9 Y: P* A# a, K" P   Win32_Printer& y- b$ c/ ]; I
  Win32_PrinterConfiguration8 P5 q& D+ `; j* `: x, W4 Q
  Win32_PrintJob
, ]4 n9 C; B" t   Win32_Processor$ d1 M; E6 ]/ E! ]9 [: R* g& r( f+ _
  Win32_Refrigeration
. ^) w& x& x- U, [   Win32_SerialPort/ n! y; r# c2 u3 T  ~
  Win32_SerialPortConfiguration
+ R! P) N* L; S! Q- f   Win32_SMBIOSMemory
# G# w" w7 n6 p: j   Win32_SoundDevice* n7 H7 w0 j$ V: E% `
  Win32_SystemEnclosure
9 Q% C0 s9 ^6 P5 i( ?0 b; \) G/ e   Win32_SystemMemoryResource
. q0 f  q$ r# F' ^, D   Win32_SystemSlot) ^* r' g4 b+ C: J5 P# k' Y% H
  Win32_TapeDrive
4 i  H7 G: S' Y   Win32_TemperatureProbe
; p9 W( n) P# P2 t$ l* u) j* Z   Win32_UninterruptiblePowerSupply
( y0 b0 K, I# f) H4 F& _6 D  a/ ?" \   Win32_USBController
: b% y+ z& ?! q8 Z9 K" o% n   Win32_VideoConfiguration( e1 ^+ C$ b: V7 F" H
  Win32_VideoController- f- V3 V# _6 t- e. Z8 `/ t, ~( r
  Win32_VoltageProbe
0 w. |8 [4 l# T7 i2 A% r- g1 f/ D" Y8 b; R. L4 n0 Z
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-31 02:23 , Processed in 0.019509 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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