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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
  f3 |7 y, I: {/ A2 Z! k2 d) y  i7 [, ?4 E

7 A$ d+ |% X5 O6 l0 N5 ?1 C4 V--------------------------------------------------------------------------------
+ g4 T6 h; F: t6 HWMI: Windows Management Instrumentation (Windows 管理工具)8 H; z" o7 [! r9 H7 Z: G
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
$ x* r2 x+ U; n$ z  d; k, q   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
1 F' F; U# D! o$ a# I9 m   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
( S* q4 Z  O; E/ U/ ?# o8 W4 x) V3 Q/ ?' C) y( Q; W' ]
--------------------------------------------------------------------------------1 u" l7 a* F1 W- ]0 ]) o: a" y
BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
0 x4 b# U' U6 C  P( v! J7 _% g# ~% t) ^  w6 l1 f4 `) t
--------------------------------------------------------------------------------" F& o( \8 {! S+ s
① 初始化 COM 接口:
6 w% L' q2 c9 L( K7 v   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。+ Y* w, R9 k& m- r6 F% f
  这两个函数在 #include <comdef.h> 里面定义。
6 ?* w9 l7 E( c% |/ ?- U& p& m* e* P
3 p/ L; e  X' y( ?; K5 h$ g② 获取访问 WMI 权限:
/ C  x) u* r; R, O4 [0 [0 a9 K   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
% S, i: G/ g# ^+ V9 r/ m( a   如果这个函数返回 S_OK 获取权限成功, 否则为失败。
+ k- }3 ~( [/ R/ a
, N5 U- M6 O6 {9 T③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
4 e& M1 C6 y( C# @   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
4 @4 ]) r, E3 s1 X4 w
+ @4 A0 w, a$ a! E$ Yvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
7 M7 J5 U/ @- R5 }6 y$ }{( ?, d% B/ {, E& x) C, m, T& G
  IWbemLocator *pWbemLocator = NULL;: s" x) Q+ g, G
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)' N% {4 l2 x0 O* ?8 S) Y
  {
6 g$ h# k) O& L+ ?+ e+ r     IWbemServices *pWbemServices = NULL;
3 B# t5 |' y5 \- m1 _     WideString wsNamespace = (L"root\\cimv2");
7 _  l" D7 u  x+ A/ s, k5 h, o9 e/ H     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK); L9 Q: A( c7 x( r8 L  v, O
     {" B; T- ]: t: q0 W. Z: y( v
       IEnumWbemClassObject *pEnumClassObject = NULL;
+ p' I- t, H! h  u- k8 _" ~. k        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;5 R  n: I" Z' l
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)) N$ J9 @* w8 v1 {$ v7 M" S  N6 G
        {6 P7 y$ r% O4 U( F
          IWbemClassObject *pClassObject = NULL;
. j. t6 V# S0 A0 N& U4 S           ULONG uCount = 1, uReturned;( y$ ~- V! l7 F; A8 O2 j; A0 U
          if(pEnumClassObject->Reset() == S_OK)0 N& N  l  H$ q( F; i
           {
( u1 l& X( f- ^              int iEnumIdx = 0;: E7 m* f1 y4 p9 G
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)8 T) W- d/ ?2 g/ S. Z  B% A
              {: X* b9 {' Z" L* f
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
8 d: Q2 c2 Q$ v9 g" X/ |" ~9 U5 A* X5 `
* b) |! W0 z5 j2 o( J9 P" F4 k                 SAFEARRAY *pvNames = NULL;% D6 b1 k) F* h2 d
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)4 A8 U; T% `, ?( I9 P/ C6 c$ x
                 {) @5 y: C* O) B
                   long vbl, vbu;
3 g& @3 x! m! f) f( v                    SafeArrayGetLBound(pvNames, 1, &vbl);
: q1 A& b" l( l% A! m" l% g                    SafeArrayGetUBound(pvNames, 1, &vbu);
' e  t0 i4 d5 z) e5 L6 n! _; J* i' Y                    for(long idx=vbl; idx<=vbu; idx++)' ^, b; X! g# Z* _
                    {) P1 _( L+ ?2 E2 C; ^( f6 y
                      long aidx = idx;5 D( _2 F; U, c, `7 y+ l' |
                      wchar_t *wsName = 0;* Q1 ^, ~; l0 R8 ]! O0 i# I( A0 s
                      VARIANT vValue;& H& M! B2 K/ ]1 g
                      VariantInit(&vValue);
5 S- x1 d( m0 J2 F" D2 P: |                       SafeArrayGetElement(pvNames, &aidx, &wsName);
; w* X: a4 ]+ T; h& S" ^
$ m; r  S% _- A* ?; P: b0 V5 s                       BSTR bs = SysAllocString(wsName);8 W1 k- @+ c% D/ O- D. @
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
3 ]" \& j- `+ f' m                       SysFreeString(bs);
* Z+ P! j$ l/ k: t9 F- c) F( y- K/ ~# ]$ M( N, l
                      if(hRes == S_OK)  r" U( X3 N, y. P4 ]
                       {
4 k. @/ {* t; V9 C( U+ s+ ~                          AnsiString s;
, h0 S3 N, ?% g0 d; E! t: o                          Variant v = *(Variant*)&vValue;8 I' d0 h0 q: _- _
                         if(v.IsArray())/ }( F# ^7 t) p& D) Q9 h' K
                          {
, E, r$ R7 P/ a$ }/ I                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
3 {. |. ^: H% o  n7 `                              {
9 |$ ^$ ~, J% f/ q                                Variant a = v.GetElement(i);
. j/ p8 A# {6 ]. r8 T                                if(!s.IsEmpty())
: Y# [" ^# @' h6 J* k5 P* b                                  s+=", ";
. f- a: C! P. R# p7 h* u                                s+=VarToStr(a);: p) ~/ R4 C) h3 I. I# ?
                             }+ A% m3 S8 O1 N' z9 v# t9 {# m
                          }
* [) g9 L) h$ o2 |9 I' q3 n                          else2 E8 @: `. s& \: }/ ]% Q
                          {! |2 l+ K& s% N( {" A& \" o% q: `( X
                            s = VarToStr(v);
; s6 `/ n/ F( l5 _8 F; L, G7 _                           }
, j/ p  ?) Z2 ?5 J9 T                          lpList->Add(AnsiString(wsName)+"="+s);
3 y/ r- m; Z' ?% y& Z2 ]9 V+ ~+ z( P                        }' g5 H8 J5 v7 B$ s- a

2 B& F  c) ~' g: T! o9 C                       VariantClear(&vValue);* @. m" r% s' D+ Z0 x$ \! w
                      SysFreeString(wsName);0 X% }; E5 q) U& g1 `# S
                    }5 G( b. s' H5 ~
                 }# h  r5 D8 a2 V6 |3 }9 C
                if(pvNames)SafeArrayDestroy(pvNames);
1 J2 n6 r7 o& v) c/ G' |1 a                 iEnumIdx++;+ `0 ^7 u3 p# L! @( ]6 p( [
              }
1 m8 ]; [9 E  a% P4 ^3 ]% u. ~            }% A* }/ t( r8 d! t. B1 p
          if(pClassObject)pClassObject->Release();
6 d; P8 l  j. Q$ q) C  ]0 C         }; e  R! H8 S, Z3 r
       if(pEnumClassObject)pEnumClassObject->Release();
3 L" F4 O" N: e" i7 s      }
" |$ M  @  p/ X$ V% y, `     if(pWbemServices)pWbemServices->Release();
! I; I6 U& |* B/ D2 w1 N   }
: o( T1 V2 P4 P# O& X2 Y& ^. F  if(pWbemLocator)pWbemLocator->Release();
: U7 |/ }3 P1 ^1 U9 {3 i2 E! K}8 G3 r# B( \8 l( C8 J( ~
//---------------------------------------------------------------------------
7 Z& n! I+ D  @& o0 _, `
8 m9 l4 z- t1 ^% H, p1 ]// 通过 WIN32_bios 获取 BIOS 信息:
5 [0 ]2 P* K6 }void __fastcall TForm1::Button1Click(TObject *Sender)
/ C& y% D3 r- ]1 _2 N1 K& V{  a$ n% m, V2 `6 F& G
   Memo1->Lines->Add("================== [WIN32_bios] =================");
. H$ a% i4 N# {9 Y4 E4 @3 m: U& G: u    GetWmiInfo(Memo1->Lines, "WIN32_bios");" e7 H' e# R3 e4 I' @8 H
   Memo1->Lines->Add("");
: a' [% B! K7 w7 c4 U}
: R% \! `) K' G( S# N/ Q$ K* h% ^
" ^: n# B1 z" }8 F--------------------------------------------------------------------------------
" J7 T- x6 }" K* g9 C
2 w% x; P; `- {WMI 可以访问的信息类型有:' b& P1 q! H$ Z* y
  Win32_1394Controller. o/ }; a. G2 q% X7 \
  Win32_BaseBoard$ E5 \% F6 B" R7 Q0 a: J3 D- ~
  Win32_Battery
! F1 B9 P& b" v% W" z( {   Win32_BIOS) h( Z5 d7 V% A
  Win32_Bus
% [* ~8 u' W3 |! L7 U& M- \: d" a   Win32_CacheMemory
1 P7 C  ^% w# _, ?4 L& P   Win32_CDROMDrive2 m- T$ O9 i7 h7 }+ Q
  Win32_CurrentProbe0 ~/ }0 e' G$ p) f; a
  Win32_DesktopMonitor
6 y4 u9 L6 ^* R! b0 n. w$ c5 t   Win32_DeviceMemoryAddress3 v8 m) j9 g; ~) _
  Win32_DiskDrive5 y. n5 `, L+ l. \5 I; i" g  Z
  Win32_DisplayConfiguration4 X, \1 I" G$ T, A; Y
  Win32_DisplayControllerConfiguration. `  `7 c# u0 z9 ^, t, x0 M
  Win32_DMAChannel- A) q7 X1 r3 D3 n
  Win32_Fan
$ F9 {' b% N8 k3 c   Win32_FloppyController. s  A% Q% h7 s. P  n# j/ I
  Win32_FloppyDrive
' _8 X' T8 f2 {" e" P5 K   Win32_HeatPipe
# R+ N! G! t7 p   Win32_IDEController
3 V$ y& l% m3 O- j/ g   Win32_InfraredDevice
/ ]6 ^; Z" a2 V/ e4 W' u+ @   Win32_IRQResource: u  v0 `! r4 g6 X1 d- X  a8 {9 S  m6 x
  Win32_Keyboard
( h( y0 T/ p+ |0 r) X+ A) h   Win32_MemoryArray: W2 k( E, V+ n6 R) T; E  j
  Win32_MemoryDevice. t( @. u' `( c) l: J' U2 v
  Win32_MotherboardDevice
& Y2 f: _. l, u: J/ ?. b' A   Win32_NetworkAdapter
7 N4 n+ l' N" L* \' t4 \  v6 F   Win32_NetworkAdapterConfiguration" i! X  [: q; m1 T8 w+ `1 m
  Win32_OnBoardDevice
$ _' H4 B- H! g   Win32_ParallelPort
- Z9 W4 W7 n6 \. s1 r   Win32_PCMCIAController
8 C# @  L9 p' }: L$ G: _1 w   Win32_PhysicalMemory
1 K+ }  h: J& _; N- g9 U, L& j3 u   Win32_PhysicalMemoryArray* E( D; v. }* }. ~9 e
  Win32_PnPEntity8 \  L" B5 N# Q' q
  Win32_PointingDevice
% b3 t( U9 b2 K6 T/ s1 o5 g6 B" H   Win32_PortableBattery+ K( ?% K( @6 J% U
  Win32_PortConnector0 ^, J8 I- }" J" F* z) `+ U8 [5 L
  Win32_PortResource
( ~# y  N/ W! J, |. E   Win32_POTSModem: i  f6 k8 }" |' U
  Win32_PowerManagementEvent# Y8 G# _6 s8 i) N' `6 E( K7 X2 E
  Win32_Printer
( B1 z& p  \% v! G0 C/ Z2 S   Win32_PrinterConfiguration% u8 k% @$ v3 H% h, A  F
  Win32_PrintJob
! J. O2 h. I+ i3 K* d0 Y0 k   Win32_Processor
' U/ X  x% m7 M. T: i; U2 V0 ^   Win32_Refrigeration9 h+ z, T. b" N% M0 z
  Win32_SerialPort
4 o" h- v8 @! A; n6 _) V   Win32_SerialPortConfiguration, b1 h& y$ F4 N' t3 ^
  Win32_SMBIOSMemory( V4 ~" F1 d5 {7 _
  Win32_SoundDevice/ d  S( k' B) Y* X
  Win32_SystemEnclosure8 u8 `5 j# S  P4 \1 ]+ z: S
  Win32_SystemMemoryResource: j8 ]+ A; V1 Y' X4 g. P
  Win32_SystemSlot" h/ ]' b7 j' W: S3 j
  Win32_TapeDrive
/ i# k! E6 x" _* j" V   Win32_TemperatureProbe
6 K' u1 k( k8 t7 \5 T( z   Win32_UninterruptiblePowerSupply
- h8 E" e- ]- G7 q' y   Win32_USBController
8 i1 P: t/ P6 d9 |5 u   Win32_VideoConfiguration' e9 R9 M* f# }. K0 o
  Win32_VideoController" {. R8 S$ X) b9 g! u& F% Z$ v
  Win32_VoltageProbe# F9 s$ B1 m. W
6 u( q6 n" V2 k% z; r1 c
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-28 20:13 , Processed in 0.018506 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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