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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
( r5 Q3 a6 U/ Q1 s9 a* x$ R0 ]$ M; B; M
# n4 ]" F, A* u( a( ~+ ^7 k
--------------------------------------------------------------------------------* G, a' J1 A) ]9 l; m
WMI: Windows Management Instrumentation (Windows 管理工具)  U, d& [5 X6 q# V% u
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 7 u4 p. ]# ]5 r' ^( S
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。2 u' M$ r) b/ p- b
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
0 G* h8 q9 |+ \+ m; ?% O! t( ^  k
--------------------------------------------------------------------------------
  t8 U: o+ Q" L6 W2 T) ^+ ^1 TBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
$ f% D+ U! S. v+ K
" O/ z8 X. Z8 d# }8 S--------------------------------------------------------------------------------
1 @9 t, U  P8 W! S1 H' P① 初始化 COM 接口:3 h' p3 |( T7 \; l' A8 [
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。* A  J- J% q# E1 M1 B
  这两个函数在 #include <comdef.h> 里面定义。
! @# k3 y+ U7 f5 M9 R9 E+ Q0 T+ d1 ]5 k! u. n. [* n- @
② 获取访问 WMI 权限:! D% f; D8 y2 K; m  l; n7 [+ D
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
3 X" K& w5 u5 j   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
  G' E4 x  _! N7 w# g2 n; ~2 B; b, t0 c
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
' n  H. c7 V; k% y: t   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
& X4 l; M6 n% Z- V  D, \, f. ^0 w7 L- }/ V  }) r4 o
void GetWmiInfo(TStrings *lpList, WideString wsClass)) a2 O; k; k' B7 d4 Y7 H! F7 l
{6 W' [4 n5 p+ y) h, A/ }- q
  IWbemLocator *pWbemLocator = NULL;
' n: Y; |# X4 `: J/ E. U  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)- I* V; u3 D( w. s
  {
' n1 S- f/ E: U5 ?! M! W     IWbemServices *pWbemServices = NULL;
; T* j% k* Z5 N, Q     WideString wsNamespace = (L"root\\cimv2");
. @9 w8 v4 H9 [( U     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK); o4 p) C- v2 {! Z: t
     {
2 u/ x) O0 i# g: e! V" W        IEnumWbemClassObject *pEnumClassObject = NULL;- t$ q6 p. M8 a; U. p
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;* ^( q  t1 N* P: `$ N1 j& D% O  l. }
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
6 ]; H' @5 X7 G, u/ L         {/ H# N- n2 S$ D' @! ~; A4 c
          IWbemClassObject *pClassObject = NULL;; H. f# q2 B" Y) g7 W5 `/ I
          ULONG uCount = 1, uReturned;1 t- g+ v" T% Q
          if(pEnumClassObject->Reset() == S_OK)
) w7 H! [! e* X3 n            {; A$ k1 d, Y$ C
             int iEnumIdx = 0;
; S6 N/ _, r2 w% S- t              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)  K( S8 m  g/ B4 Z( D$ N
              {) q( ^0 g* M/ z6 P6 x$ g
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");7 H1 D6 F' G4 ~2 S2 I# \$ P
  [, w; K8 r$ g
                SAFEARRAY *pvNames = NULL;
# h3 w1 o: _0 m* E9 Z3 ~5 S( b; L                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)0 x1 N  ^9 B1 B0 I6 }
                 {
- a, S. A6 t6 G$ e4 C/ x                    long vbl, vbu;
4 w" a. A. A4 E& q4 f                    SafeArrayGetLBound(pvNames, 1, &vbl);/ V/ h6 l" p3 }* M1 M/ s
                   SafeArrayGetUBound(pvNames, 1, &vbu);
: L7 l2 ]# P8 m# c7 ^# s2 K                    for(long idx=vbl; idx<=vbu; idx++)
: r3 s$ F! n) z                     {
2 y5 u" T( F+ x) R! ^  Q6 Z: D                       long aidx = idx;) @0 L- |/ Z5 x2 s( S$ N
                      wchar_t *wsName = 0;2 {( J- h: Z+ D3 Z  U0 ?! F5 q8 W3 O
                      VARIANT vValue;
2 H& W0 ?! ?1 \3 R                       VariantInit(&vValue);! D: F2 }( s& J. J" }; ^  b
                      SafeArrayGetElement(pvNames, &aidx, &wsName);& L& c3 M8 O- t, R
/ P* u2 ]' B1 Z; q- ^1 p
                      BSTR bs = SysAllocString(wsName);
' s0 v& i" r# T8 ^7 O1 f                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
, O! }9 w1 d/ \* u2 Y( n7 a: y2 v                       SysFreeString(bs);
, q) w0 u- W! c( T6 G* Q
) b, e) p2 o: C' j& u7 f& B9 U                       if(hRes == S_OK)
$ A) e+ p9 s  B- _. J2 `                        {6 x% S" m' Y3 b3 F2 a" D4 G2 H
                         AnsiString s;  f. ]* {! b$ T0 {  r
                         Variant v = *(Variant*)&vValue;
0 W9 ?- w, ^! L, }' V                          if(v.IsArray())
7 b6 X* J5 W8 }- w1 t% f* `; i                           {
2 o& E. P* z5 c                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
# h- b% M+ o1 c# e0 J                              {
0 Z1 c1 @" _& K3 d4 C  }                                Variant a = v.GetElement(i);
) }- Q5 S0 h% P- O  t/ Y                                if(!s.IsEmpty())
$ I9 f# G/ s: k' T5 b9 s                                  s+=", ";! k! J6 t1 c1 c& ?
                               s+=VarToStr(a);
6 e, t2 }# A4 Z$ x: S                              }  r3 {0 z' D, C
                          }
8 N  a3 j+ G4 }: |) q" t' K9 F                          else8 I" s' B: I$ ~' d4 B" V0 N
                          {2 g7 [7 l, g. K
                            s = VarToStr(v);' ?) Y5 x" _  L1 @9 o
                          }! B/ e" u9 j6 f+ @
                         lpList->Add(AnsiString(wsName)+"="+s);
0 P$ Z0 T" C1 o* Q2 t                        }7 V" y/ Q: t, n: T, _/ o! E$ }
( O$ w2 x9 x- p
                      VariantClear(&vValue);. D- M$ _( Z  r
                      SysFreeString(wsName);
! B3 K, O0 r7 J4 E: f& M                     }
0 u. F: P$ T% s& z; ^, r9 m& ]: |                  }
5 j' Z% V, [) l* y" L1 S                 if(pvNames)SafeArrayDestroy(pvNames);
8 d! O2 H& p) I: a1 j                 iEnumIdx++;% e0 @, l/ A* R0 ?2 e# d9 H$ d
              }3 J! F7 ^- ^, c
           }
9 Y1 L( K2 y. ]( W) O: ^           if(pClassObject)pClassObject->Release();
6 R" O( D& U' Q, q- Y3 m+ S         }
) ?+ k$ W* D0 ?        if(pEnumClassObject)pEnumClassObject->Release();+ L% ^# u3 ~! w0 [: `; ~
     }# D. Q; w4 U- C% w9 \
    if(pWbemServices)pWbemServices->Release();* z' z8 I/ f+ P- |
  }2 Q+ V7 K# U% G: J
  if(pWbemLocator)pWbemLocator->Release();
+ D( y0 y' _$ M. L}
  D9 e" r7 P0 I0 O//---------------------------------------------------------------------------. B: v4 c2 g2 k+ S' I

' a- d9 p8 V4 o// 通过 WIN32_bios 获取 BIOS 信息:
" ]; _- ~% q7 }* Uvoid __fastcall TForm1::Button1Click(TObject *Sender)
, o6 E/ v4 b) `5 w; R{1 [! F" U" {5 b# S( j' L7 D
   Memo1->Lines->Add("================== [WIN32_bios] =================");
- f  b$ s3 d( c$ n7 V7 g% r    GetWmiInfo(Memo1->Lines, "WIN32_bios");1 O7 d& Q% S* w- Q& f
   Memo1->Lines->Add("");
' W' e6 M  z/ f9 {}- ~) L5 T, F' y1 v: p! w2 D

5 A! W) ]( C5 E  P8 M--------------------------------------------------------------------------------3 Z$ [' a& A" D  e

( \2 M9 ^, E* HWMI 可以访问的信息类型有:- p' Z+ d2 o, D! e
  Win32_1394Controller) |$ b% k! h4 u0 _. t: \8 j* q3 p4 D
  Win32_BaseBoard
/ x2 Y( u& V* T( c/ p1 v/ V& D' v, a   Win32_Battery1 b; H' a9 L- h$ f& p
  Win32_BIOS: \! w# W9 Z7 L
  Win32_Bus" t+ }" U$ _5 O) L7 `9 A
  Win32_CacheMemory3 _- h5 N" h- q$ A0 r  V$ f. G4 u
  Win32_CDROMDrive# b/ |* z- y' l% y. m$ }: P" {
  Win32_CurrentProbe3 a/ }+ p6 S8 ~' K& b; q
  Win32_DesktopMonitor
- x- c6 n- b/ |" |  y, t   Win32_DeviceMemoryAddress
, a4 X) h" S& ]4 X   Win32_DiskDrive& i4 Q/ X( w  C2 e
  Win32_DisplayConfiguration/ _( N  T% W* D. A' c$ V) K
  Win32_DisplayControllerConfiguration% Y% D; b* M+ ~) T. ]3 Z+ }1 Q0 |0 {
  Win32_DMAChannel6 z2 u$ D7 [- H
  Win32_Fan
5 _- ]" A/ U4 Y3 @6 H# O   Win32_FloppyController6 j7 p6 @* H" X- e( V4 c
  Win32_FloppyDrive  I0 [4 B8 m- H3 t# V4 I" v
  Win32_HeatPipe, X9 d. y* M" A! L" K' H( V7 [
  Win32_IDEController8 ^7 ~2 S: k& [. p& F& e
  Win32_InfraredDevice2 m4 U% s5 R2 v% L4 D$ i9 c/ _1 ~
  Win32_IRQResource
4 b* o3 P% X4 d, X6 M& P   Win32_Keyboard
, V3 X+ o0 L7 p$ ^   Win32_MemoryArray; e/ p0 O* U) y  I
  Win32_MemoryDevice
: J: `8 }9 q3 z% ~   Win32_MotherboardDevice0 u; S. A! ?( [+ j" q( Z
  Win32_NetworkAdapter3 ?0 W) |0 |3 i$ _  c  e$ D' Q
  Win32_NetworkAdapterConfiguration6 C1 Y; ?; w- S6 z0 i6 y
  Win32_OnBoardDevice
; o' a6 b7 \$ P: O" x   Win32_ParallelPort
7 q5 P8 s) ^6 {% }& O- R! B5 Y0 X   Win32_PCMCIAController
+ ^8 y6 K! K5 b  \   Win32_PhysicalMemory
, ~: r7 M' v0 O% O4 Z1 ?   Win32_PhysicalMemoryArray
8 F& g5 g% v7 w7 |   Win32_PnPEntity1 m: [( C+ C2 {0 ]
  Win32_PointingDevice
! L4 s1 L3 ^# c, o+ h   Win32_PortableBattery0 M6 Z3 b  ?- o* H8 y, i3 [/ X
  Win32_PortConnector0 B  \# R; g* }+ r. x( Y/ A! E/ {
  Win32_PortResource
* c; i4 M0 u% _* x   Win32_POTSModem7 }7 y, }: l$ o. w. j
  Win32_PowerManagementEvent3 J! Z; q% [) s: u5 M
  Win32_Printer7 X) e% v& I- K: t
  Win32_PrinterConfiguration
* h6 C7 Y% g- Y' U5 [2 ?3 _   Win32_PrintJob
9 a/ k* Y+ P& ^5 B6 c. m   Win32_Processor" {. R* @# E5 U. j" A
  Win32_Refrigeration- N5 b& U/ E# h6 i9 a' q. R
  Win32_SerialPort, x. u" _: b& A: S, A, E, a
  Win32_SerialPortConfiguration, ?/ Q* b& B, O+ G8 P' p; _# B
  Win32_SMBIOSMemory& M  C9 X! R3 E2 D3 S% G- c+ e7 K
  Win32_SoundDevice
2 O  t/ _: w! m! x. u   Win32_SystemEnclosure
$ h# |  @! b5 H) Z$ s: }* t   Win32_SystemMemoryResource# m( Q9 J$ r+ q; [
  Win32_SystemSlot9 x! J1 t* [# i. x
  Win32_TapeDrive
& _" t4 N0 @( h0 j) R: p1 E) z5 _   Win32_TemperatureProbe( S, H- q6 u3 O# _
  Win32_UninterruptiblePowerSupply3 f6 {9 h; H# H5 O8 |
  Win32_USBController; ^8 p+ M  }% O* ^4 S% X* O4 U: l
  Win32_VideoConfiguration
6 j; S& a( r0 P8 q5 p   Win32_VideoController
6 z' K. H) s$ R   Win32_VoltageProbe
' f( v" ~9 R  ]4 n- c
& C% j0 N5 n0 z5 M: Z7 J( x% q以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-30 20:15 , Processed in 0.019739 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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