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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)3 m; g- q# z  `" o

7 S! i2 g' ~8 K' K
# h/ K! B$ Q4 o* W9 j  I--------------------------------------------------------------------------------
3 w5 x. P( H7 GWMI: Windows Management Instrumentation (Windows 管理工具)% p' t4 c+ U! a/ a: q, q- [* g3 \& \
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
* H/ V: d& a5 u& O   利用这个工具可以管理本地或客户端系统中几乎所有的信息。
) W. \: ~3 s, {- F) e7 I   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 & C' I4 R/ N) i: w8 Y. q* `( Q) s
- V  t4 I/ R. @6 ~& u6 f
--------------------------------------------------------------------------------
" v. q1 }3 l& [9 w$ p: X+ [BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面3 C: |2 _9 X0 a

2 {3 t! z9 O1 a0 M; J5 Z( c6 O--------------------------------------------------------------------------------
6 Y8 Z. l( ?# d# [9 h" E① 初始化 COM 接口:. A/ E+ c) ~; Y7 f! T: m  G2 {- Z
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。& r# {. k9 T+ G7 k1 N- M6 Y2 k
  这两个函数在 #include <comdef.h> 里面定义。. }- x* O2 H- o+ d9 S6 f
) j/ O, y; J  p5 m% v6 ]+ e
② 获取访问 WMI 权限:
! j6 i" S, W3 D. V   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);' [2 u/ Z/ E8 v' {) d8 ~1 ?7 z
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。% G% G: U' Z) _
' b/ ?! Y0 ?) `: \+ F% r. V1 c
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
# X& u0 ?4 [; v+ T, ~6 Y   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。( c& }7 d: X  ^
! G- J  E6 H' F+ U, Z3 n7 B& Q
void GetWmiInfo(TStrings *lpList, WideString wsClass)
3 Z/ a( M: l2 y# T8 I4 K9 G{
) y5 N  x  \' X3 ~5 k- U( c8 l  IWbemLocator *pWbemLocator = NULL;
! R! j5 I6 u- {- g2 z% Z! e3 U  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
1 f; g; R' o- E# ]   {' n2 [7 W1 w3 \4 L. R
    IWbemServices *pWbemServices = NULL;( I' r! V: R' f( i4 S$ \3 u" G
    WideString wsNamespace = (L"root\\cimv2");
& i, H1 `7 G; [# g6 v     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)9 X* T9 g- F' S& S* b* C
     {; m/ x* V8 p# x% {% v1 h4 m( K
       IEnumWbemClassObject *pEnumClassObject = NULL;
- H0 L+ x2 D3 a; u% I        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
1 w* P2 r! L9 F7 B/ A! H8 W& q6 N+ l+ k        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)& a5 B$ c$ J2 I' r! Q
        {
! [5 w. O' i! E" U2 T; L0 @: K           IWbemClassObject *pClassObject = NULL;
; q( s' R+ Q6 z           ULONG uCount = 1, uReturned;( i; ?: O5 Z- v
          if(pEnumClassObject->Reset() == S_OK)6 i0 F  _9 {2 q# h- D9 m5 S% v5 [
           {
  H+ a4 h" d* {) P0 n! r$ h! {" N              int iEnumIdx = 0;* n8 e* M( G1 a7 [: d( \2 k9 O
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
, l. x' B* V3 g8 x) r9 C               {3 @0 a' W1 {7 C5 F/ _. L  @8 n
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");; r# k/ P5 v+ X+ H4 ], o

4 u' \* Z; y. {7 Y5 t                 SAFEARRAY *pvNames = NULL;
4 L$ v# g1 p4 N' Q: r                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
) z: x/ H" t$ D, L2 N8 u9 P                  {1 L% Z* T  h+ Q. v
                   long vbl, vbu;
0 ^0 u8 V9 D" k6 y8 p6 S# k1 k                    SafeArrayGetLBound(pvNames, 1, &vbl);9 Z* w* X+ }$ y- ]" G) \5 V+ b$ |% \
                   SafeArrayGetUBound(pvNames, 1, &vbu);2 W( M0 I& r' p3 _. f/ O2 o1 k4 ^
                   for(long idx=vbl; idx<=vbu; idx++)5 V2 r  f* F: n5 D
                    {
" h3 q, c5 x% V/ M! f" |" b2 Q                       long aidx = idx;
' m5 o3 Z2 D8 q, r, z                       wchar_t *wsName = 0;
: @( i% b! p/ O) ]3 n                       VARIANT vValue;! F% w  w1 Z2 e& u# ^4 S
                      VariantInit(&vValue);
* X0 v( u3 G+ k1 _6 X) {) t5 j                       SafeArrayGetElement(pvNames, &aidx, &wsName);
' o5 J: j4 P- f$ j9 q+ |4 c  G; a- P
$ D" [% H4 X; A/ o                       BSTR bs = SysAllocString(wsName);
* o# Q: ~) q' p5 Q3 L                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);' l' ^% f. a, D! |
                      SysFreeString(bs);
4 T& W3 P3 n. s! f5 X& e* k- Y5 h$ y2 _
% J% ^) c' h( u2 o5 }: \                       if(hRes == S_OK)8 x; N0 F4 s2 K3 g1 y! A1 w0 j" X
                       {
, y/ I) X$ p7 B, |5 B+ ^; w* ^9 Y                          AnsiString s;/ M# e7 x$ `7 G! |- A0 f
                         Variant v = *(Variant*)&vValue;
  ~, D* q4 d/ @: X                          if(v.IsArray())
2 d3 e% e. R2 A6 B- P1 w7 R% r                           {8 h# c- g( J- Z, L7 I4 d& ?& A
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++), }; l9 O% D$ t* l
                             {7 s! e6 Q, s* ~4 |% f" n  J" |
                               Variant a = v.GetElement(i);
& d5 c4 h- R: _2 n+ P% C                                if(!s.IsEmpty())7 x4 }6 O; r; A- L
                                 s+=", ";: d& p6 F- a1 b( }* ?% B" q
                               s+=VarToStr(a);
8 x6 J) T  a5 |4 q                              }) I/ l2 Q. {/ E
                          }
( l$ O& i* }: V$ }) k, A                          else
& |* y" j& U. X9 t# @                           {
8 u9 h3 g3 K; m2 D9 t7 Z                             s = VarToStr(v);
4 v! r  G( L* c5 t                           }
: u3 }$ c1 N5 j& @                          lpList->Add(AnsiString(wsName)+"="+s);1 l* [& ]; V4 j6 S+ V
                       }
' ?- [/ i) L2 }4 i/ x6 W0 x* s# x5 L3 K8 V5 k( \1 b: Z
                      VariantClear(&vValue);
) p7 Q4 c! M# ~& w                       SysFreeString(wsName);
+ \* P2 N) ]2 z3 L8 V! Y4 n                     }
4 t, n2 F1 T. f/ V, i, ?! J                  }
% b1 v$ m8 l6 W0 v% C" P+ r                 if(pvNames)SafeArrayDestroy(pvNames);
9 [' _2 ^2 ]& Y% G" |  E                 iEnumIdx++;* i0 O6 Z4 Y: c+ X2 G- f
              }
& j' ]- l3 _9 ^            }. o% T8 P' l+ V
          if(pClassObject)pClassObject->Release();
* ^  C! f7 F# x% w' Q7 O) O0 q& ]         }5 J$ |7 g  Q/ X3 F: _
       if(pEnumClassObject)pEnumClassObject->Release();( v/ q2 K. c: _1 N3 k( C' h
     }
- ]- B, i4 L9 y: \5 R     if(pWbemServices)pWbemServices->Release();2 P% p* x$ Z3 G% H- J, q
  }
0 ]( q3 [" H: B0 }  if(pWbemLocator)pWbemLocator->Release();
0 }* {" Z" X) }, V" O2 p}, n% J4 c" k1 w4 i* |
//---------------------------------------------------------------------------
2 K* }+ i- }8 }5 Y1 Q! o8 X4 p1 h" K/ ]: O7 ?
// 通过 WIN32_bios 获取 BIOS 信息:- y0 Y7 P; y$ _" W3 p
void __fastcall TForm1::Button1Click(TObject *Sender)1 Y0 S1 q! h1 ^) G, w5 x
{
2 e3 f( `8 f* J9 ~: r    Memo1->Lines->Add("================== [WIN32_bios] =================");1 [$ |4 ?0 m$ n) x& H8 m$ w
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
& G. J! T9 Y" n1 I/ B$ _    Memo1->Lines->Add("");& w) f. H' M* M3 C0 Z
}& f2 b* P. z  r( }' q+ O
9 S. e2 f" U6 T% z. g; ?
--------------------------------------------------------------------------------/ ~) |1 ^+ C6 q
) c+ {% j. V: ~( p1 m, G5 d
WMI 可以访问的信息类型有:
# t# W/ ]& V9 P6 v; S   Win32_1394Controller
% m, Y2 D# m1 E! R   Win32_BaseBoard
( K6 f9 B5 V* }7 g! Q- ]; k   Win32_Battery
& ~0 F! {, v0 B" L# R) ?   Win32_BIOS% S, l& D" p7 J. y* K
  Win32_Bus. h; T! g% g2 a$ @
  Win32_CacheMemory& l9 @/ F. u- d& Y: [+ d
  Win32_CDROMDrive# y" R8 P! h5 O' l( @2 R+ U+ q
  Win32_CurrentProbe
6 \5 v1 p) m5 A# q, T" U   Win32_DesktopMonitor
4 n0 x, J$ V8 b   Win32_DeviceMemoryAddress- V3 \; r2 R+ u* Q3 z; {! l
  Win32_DiskDrive+ K5 ^1 g( H& r+ m
  Win32_DisplayConfiguration
* A1 J& M& U; H4 J$ K   Win32_DisplayControllerConfiguration
: C5 y8 ~  T- m. ?: f' n( O: D. K   Win32_DMAChannel9 ~3 [1 {3 _: X8 W+ c2 M+ R8 L" a
  Win32_Fan
0 V1 Z& c0 S3 v% R2 n   Win32_FloppyController& k0 U' \2 M6 G. A: m
  Win32_FloppyDrive/ N7 n) x& Y% q
  Win32_HeatPipe' \* C: s# d2 L$ ?! l* g
  Win32_IDEController6 a5 F+ C# B: D: e4 [
  Win32_InfraredDevice
; m( C9 h, I3 k5 {" W' }   Win32_IRQResource
/ T' Z4 L% Z/ w* {: y8 ?' M   Win32_Keyboard
0 e1 {$ e  j: W+ y/ R) w& H7 \, f   Win32_MemoryArray! e2 M6 P( ]6 ^# f' o: D
  Win32_MemoryDevice
6 r" j( y9 k. W+ g3 x+ k3 s   Win32_MotherboardDevice
6 v( K3 ~" _3 Z% z: ?/ z/ t, C$ g   Win32_NetworkAdapter
, A; F( p- l0 X4 _+ J$ r9 B% q3 |   Win32_NetworkAdapterConfiguration% B1 r6 [- O, h! t4 [8 `: E
  Win32_OnBoardDevice3 n% s* K$ d: P7 b* k5 \
  Win32_ParallelPort: c+ o- U# ^3 z/ T
  Win32_PCMCIAController
# U2 \" _/ d- `2 B, x, x4 q. M' _% \   Win32_PhysicalMemory, s, P* x- ~7 f& s6 Y
  Win32_PhysicalMemoryArray* o3 h+ A6 V4 J/ [$ i
  Win32_PnPEntity
5 U" U4 o3 V) L1 N   Win32_PointingDevice
, N1 e# ?1 k, T! v  }   Win32_PortableBattery7 `; E! r" Z* V- z# e; V
  Win32_PortConnector
( C- o& ?1 g+ {+ K$ P8 B: Y   Win32_PortResource, g, u2 r6 P3 F
  Win32_POTSModem5 i4 \! X. ?: x1 M" P) ?
  Win32_PowerManagementEvent! u7 R/ Y( n3 R6 R  G& Z
  Win32_Printer
4 I0 o( Q7 Y! [/ I" a5 }: h7 X5 c   Win32_PrinterConfiguration
. a6 }! F7 E$ F# J) s1 e0 D0 }   Win32_PrintJob& d. G# R( J  c$ Q9 Z
  Win32_Processor
, }. F9 z  H, \. @/ [3 X, b   Win32_Refrigeration
: ]; M$ ]4 p. B4 x   Win32_SerialPort
  M9 W/ j, ]2 O7 A5 i- M   Win32_SerialPortConfiguration
+ C$ z. j5 L' f2 [   Win32_SMBIOSMemory6 p1 l8 C  e: u( K  V
  Win32_SoundDevice
  y# @: O9 e0 _  k. R   Win32_SystemEnclosure
0 i) K* z- l" }% J/ T5 s' D  _   Win32_SystemMemoryResource
6 F8 n5 x7 f. }' t% s1 O. N/ @   Win32_SystemSlot( r1 K; C4 t- [3 ?; u5 I# [
  Win32_TapeDrive: t9 Z: l7 j2 \1 V. q+ Y4 @; t
  Win32_TemperatureProbe
! q* b6 A3 j. n: q. T   Win32_UninterruptiblePowerSupply3 _6 t1 k, \  E" c3 f
  Win32_USBController! e& N6 C2 t2 Y  j/ z! V! R
  Win32_VideoConfiguration
6 I$ Q; @! A4 O2 A# Z& R9 A1 M   Win32_VideoController
' g  [7 n, g0 Q. p  o   Win32_VoltageProbe8 \/ C0 `# V" K4 K3 g( @+ Q7 |

0 K7 l3 n5 D3 v9 Z6 n8 Z- |以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-30 02:30 , Processed in 0.021349 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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