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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
" f" q' b# [2 x/ P2 k7 h$ S* k; H% j! c0 q
2 d, D  k" N: S, s2 H/ M
--------------------------------------------------------------------------------; `0 z( Q. u5 R
WMI: Windows Management Instrumentation (Windows 管理工具)- X: E' C. p2 H( k
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 , f* M# M3 O  w/ f/ j$ |, ~
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。' w/ }  _. x& z0 f0 l2 n; E
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
% x$ ]/ O4 I0 U% i) [9 w3 l
2 I- V! z! M. _4 M2 n/ d, B--------------------------------------------------------------------------------
( `2 M9 O8 _3 k3 W2 @6 D3 BBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面8 t3 M6 w& Z; x+ g! I
, b9 {  ^' x! a$ E
--------------------------------------------------------------------------------. J# o* M3 i2 g8 ^
① 初始化 COM 接口:
% U8 k6 G$ R0 l3 s0 ?   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。# W* H% r8 [, C! g' T. @7 _
  这两个函数在 #include <comdef.h> 里面定义。
' c2 U  @8 r$ M% z7 K3 [( [. m/ E$ a  T7 H" }
② 获取访问 WMI 权限:
. x5 Z" Z5 W: e6 J/ N$ t$ u   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);8 Y6 [3 v2 U8 |
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。
9 `* V; v! V9 ?9 b5 v5 s% h+ a$ G( z% ?) F# |& T- ]$ t0 |
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
. U: [' D8 c. i5 A9 r" p) Z! p6 B- L   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
( @* d) L: `/ J1 k4 M
, X$ T& }5 I7 ]$ |void GetWmiInfo(TStrings *lpList, WideString wsClass)9 a9 B' i0 F0 d  f: ~0 X; K# ]7 i$ C. n
{
; ^1 m0 W2 ]$ |1 m  ^  IWbemLocator *pWbemLocator = NULL;2 x: n) ~2 ^- b
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
2 ~; y/ c) O8 c) Y; `! Y- _& Z: U   {% B0 M3 K1 ~7 v- F& z+ U  M
    IWbemServices *pWbemServices = NULL;
% {6 v; p* f3 m# Y- t! P7 M' c( C     WideString wsNamespace = (L"root\\cimv2");, [5 u9 ~6 l6 Y; l$ R/ B
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
: k, Q) f# d( @      {" N2 K9 W  B, F7 Y8 H$ g
       IEnumWbemClassObject *pEnumClassObject = NULL;. H: A  F4 a3 w8 m2 D% Q
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;6 W0 V% g0 H' A5 P# p
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK), j3 e( \) y# h/ ~5 n
        {' y' P4 ?, S& T8 B5 ^/ V
          IWbemClassObject *pClassObject = NULL;
! t8 t% t8 A/ U( t' `' {           ULONG uCount = 1, uReturned;+ P- l6 t; J! q2 p- R( j
          if(pEnumClassObject->Reset() == S_OK)
; N5 l* D8 g. \% N! c            {
4 t5 O: h$ J- `( u3 S4 a7 p( _              int iEnumIdx = 0;
& u0 k2 X" T- n* n. {. n; Q$ V  s0 O              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)- {( w4 B: D' k9 T: N
              {
1 G/ A! D' ?4 B; ]  u                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");( F% d, J: T  y; J* Z2 k! H$ Y6 c% Y

8 Y9 n9 _7 R$ b' }- r$ q                 SAFEARRAY *pvNames = NULL;8 m; J6 [1 h1 w
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK): Y( u9 _% t, W2 R2 q( s6 n% k
                 {
) ~' |" z7 D+ _" J" F" M" r9 A                    long vbl, vbu;
8 B/ ?& {+ A, M  |                    SafeArrayGetLBound(pvNames, 1, &vbl);* N2 w: g# A0 v. [
                   SafeArrayGetUBound(pvNames, 1, &vbu);
. ?, ?5 a- v  b8 ]' {9 ]                    for(long idx=vbl; idx<=vbu; idx++)- B# y; ^+ v/ k3 p" g4 ]$ N
                    {. g  Q0 ~% T! |; f
                      long aidx = idx;7 I& p6 [! L/ w& K# r
                      wchar_t *wsName = 0;
+ A0 |( w+ l- N                       VARIANT vValue;
/ C& K9 S/ t$ F- A# D" I3 o* s4 X                       VariantInit(&vValue);
4 R; [; m, c# H, G                       SafeArrayGetElement(pvNames, &aidx, &wsName);
& f8 X" t( V+ y  Z+ {
' N1 W' j6 g$ i9 ~: i5 S7 B! y                       BSTR bs = SysAllocString(wsName);
  f+ g8 P' z7 W6 g+ F  o2 L& n                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
7 o: v+ C) N& t4 Y5 h; |' l                       SysFreeString(bs);
0 g/ H" ~1 w$ g; N9 z+ y- Q  S  E  t
                      if(hRes == S_OK)' V/ z; x- F% I5 O6 V' s' N4 ?% @' m
                       {
7 b6 S7 z4 W% ?8 s* y4 S% n* y; c* I                          AnsiString s;
$ w& r; y# s, S/ V1 C5 D6 T                          Variant v = *(Variant*)&vValue;  j; K& n+ o4 r  b
                         if(v.IsArray())# T* A8 U5 v* i% _& ~" @5 b
                          {! ^7 `& N4 E( \9 x/ C3 ^3 i
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)+ v+ A6 Q. A$ O" i
                             {) N# |% s' Z; t8 Q
                               Variant a = v.GetElement(i);
- @! k2 u. [3 \. B* f* X4 T                                if(!s.IsEmpty())/ }% B6 Y1 I! `# t7 u. c
                                 s+=", ";/ z9 E- K- x2 s2 s; g7 C# l
                               s+=VarToStr(a);
* ?9 b+ q( e0 o& S                              }
8 v; X. C6 r( ^) W5 c$ w( f                           }& s: k' P3 b) t/ O2 \3 X
                         else
7 s$ Z  N) v3 r0 P" L5 t- C                           {1 L# m2 n: p: @9 |
                            s = VarToStr(v);
+ C3 y8 d" D- T1 t* X/ T                           }
6 {( X' S& `4 q5 J) s9 |1 S: s                          lpList->Add(AnsiString(wsName)+"="+s);
4 U7 P6 Q8 L% `' L9 D' N, K                        }
) d/ A1 x, v0 N) v' O; `3 E2 }  x) k4 T8 }. }' x
                      VariantClear(&vValue);9 M% n+ n3 E; F5 J1 R
                      SysFreeString(wsName);7 H- e- r1 G) _$ [4 P7 S# w
                    }; J: n$ @( M0 z, R+ E2 I
                 }
; J6 ]6 H0 R. N( n: g+ G                 if(pvNames)SafeArrayDestroy(pvNames);6 K# H4 `6 s- u  b) R! B4 G6 }- P, s
                iEnumIdx++;) z" Q. t+ u! M. L& w
              }) J  h9 G# g9 @" ~# b4 J" C
           }
( c/ Z* m9 T- H0 q8 K2 @% d           if(pClassObject)pClassObject->Release();
# X4 m. M  L5 k7 H         }
) `$ W& Z* N. H% W/ S, x$ N        if(pEnumClassObject)pEnumClassObject->Release();
( ^8 e7 y. T7 I5 e7 s      }1 ?0 d; `4 h( }% N/ I& u8 g
    if(pWbemServices)pWbemServices->Release();% z4 x! ]5 u" I4 @  h1 I
  }
1 p/ W9 w7 S* v( u6 ?4 T- P  if(pWbemLocator)pWbemLocator->Release();1 ^) l# P. P$ i2 O# G6 I
}/ A6 N7 J) G* y$ n& C4 @
//---------------------------------------------------------------------------
% a" v6 ]9 I7 g. K" n5 q- t, W6 f) h# ^$ ~9 G4 R
// 通过 WIN32_bios 获取 BIOS 信息:( A; p9 }5 e- _3 `. J  t
void __fastcall TForm1::Button1Click(TObject *Sender)
- K. f9 ^3 ~% d4 J{
9 s; T8 A+ N& B7 Y6 S- s3 b    Memo1->Lines->Add("================== [WIN32_bios] =================");' [4 D9 o; ~4 x6 M; q% b
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
/ g2 v- \  q* K; B/ O$ d    Memo1->Lines->Add("");
+ B  Y2 Y6 ~% y}$ [% e& o& W4 u) M
2 F! J7 E+ U' h, c( K+ ~  z
--------------------------------------------------------------------------------
' f9 ^! S" Y5 T/ N: p  J$ P; M$ K* X$ F' W: Q' \
WMI 可以访问的信息类型有:
6 R. V" ^; T, x) g) o   Win32_1394Controller5 r3 ]  ?6 t( Q% \1 ]. ~6 b2 T3 W
  Win32_BaseBoard
+ E4 z; ~2 [6 Z: [' q% L; d: ]4 {   Win32_Battery
; x; |8 w: j6 I0 B8 A/ p   Win32_BIOS
9 ?  Y# c4 G" A  r3 ]8 _+ e   Win32_Bus
: S. a2 Q6 ^' k! j6 B   Win32_CacheMemory( E' @, [9 T) t- i6 H; |
  Win32_CDROMDrive( J$ n1 {5 K- t
  Win32_CurrentProbe- k+ L7 q+ c+ ]( M3 Y! f  F8 {) w
  Win32_DesktopMonitor
6 C3 m; [5 ~, P, m. J; ~   Win32_DeviceMemoryAddress% L( {$ Y" T. K, z2 E; I  W- h; j
  Win32_DiskDrive2 F8 ~' ~+ x+ B5 \" T
  Win32_DisplayConfiguration  {5 S; s1 v  L5 O+ B2 _4 [
  Win32_DisplayControllerConfiguration0 n( O% o, T. W9 T
  Win32_DMAChannel
8 m3 d" e& s0 M- m  K   Win32_Fan
& s8 h# x. i0 q! @) A   Win32_FloppyController( r# A, m# n6 d# K; `
  Win32_FloppyDrive, n7 Z7 U% d8 a9 X  S/ k3 O! Y$ y. V7 Z
  Win32_HeatPipe1 d/ W3 k2 C) h  c$ R0 v
  Win32_IDEController' G" r) y1 `8 z% N& `% p: B/ G
  Win32_InfraredDevice2 t, B7 r& f# @" }4 m
  Win32_IRQResource
3 y' \9 \' {  X- D" F   Win32_Keyboard
4 C: `5 ?& {( E$ ~2 W   Win32_MemoryArray
# p$ m- f; r+ x   Win32_MemoryDevice
2 n) B' ?. n5 X( N: v1 t4 R1 @$ t   Win32_MotherboardDevice
  F, Z* j, _1 ?- Q6 N$ ]   Win32_NetworkAdapter9 ?- \$ M  z0 |  [( V
  Win32_NetworkAdapterConfiguration' x* U' g  I! K; Z$ z+ Q! Q$ b
  Win32_OnBoardDevice: v& H8 C) {' Y3 w
  Win32_ParallelPort
5 o8 U+ y/ Q' q   Win32_PCMCIAController
1 s: z# I; S  M* u0 F   Win32_PhysicalMemory6 O3 e+ c0 e+ C# m
  Win32_PhysicalMemoryArray
7 v, V$ c* G, J   Win32_PnPEntity$ Y4 J* B) h' |
  Win32_PointingDevice
, y, [+ X) g1 Q# ~- @, v+ v9 [/ e   Win32_PortableBattery
+ |0 c! v/ O/ i7 G+ `   Win32_PortConnector
, H2 e/ V3 L0 f0 o$ E# x! n   Win32_PortResource; O2 C% y% c$ g: D3 t
  Win32_POTSModem
) r+ R; E& v2 F3 A& ~   Win32_PowerManagementEvent
: e1 o# p- L4 e9 X0 Z   Win32_Printer
+ E) s* q4 O7 _   Win32_PrinterConfiguration6 C) \4 U) t- e# b: G2 d
  Win32_PrintJob
6 S$ W- p, f+ B6 h6 l$ k" X   Win32_Processor) Z* b5 r* O1 s1 M  r
  Win32_Refrigeration/ I. Q5 ]0 q" [% u$ X/ S  ]8 @; A" o
  Win32_SerialPort2 E. L. H) L: U8 p
  Win32_SerialPortConfiguration
8 @" V% ^1 ~& H6 w   Win32_SMBIOSMemory
5 d+ V2 d% D5 d   Win32_SoundDevice: m- o; J- z! z
  Win32_SystemEnclosure
; w# q7 k8 e2 _; Y8 T   Win32_SystemMemoryResource7 a  O" l' ^$ Q
  Win32_SystemSlot& k/ {+ B  {1 M* Q6 O% T
  Win32_TapeDrive* |. ?& w6 h" Y* ]4 R; G
  Win32_TemperatureProbe( f% t- s  [8 ^8 i
  Win32_UninterruptiblePowerSupply
0 w3 A3 \6 B+ ~& i7 V2 v   Win32_USBController5 u" P% N$ C0 X4 A
  Win32_VideoConfiguration0 D7 ?* C9 `+ X; x
  Win32_VideoController
0 D) X7 G! i4 E' a7 g$ H   Win32_VoltageProbe% G7 B1 B4 U* s& W+ T  F1 H. X: {. [$ ?

* k  w0 g9 U; r) ^以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-30 00:56 , Processed in 0.017481 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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