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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者). G. ^8 i- J5 i+ v) L: g
1 R/ V9 r2 _! _- Y" |
# W2 Q+ v' r) s, ]: u
--------------------------------------------------------------------------------8 k& i/ y8 m9 Q5 m3 z, A* v* j- s2 ]
WMI: Windows Management Instrumentation (Windows 管理工具)
$ l, A& u) w  b& A3 [   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
  ?4 b: J5 h# W4 F: D  N0 U  g& j2 [   利用这个工具可以管理本地或客户端系统中几乎所有的信息。1 x) ]) m! c1 Y& X, c3 W5 W
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 ) n: D. y1 v7 R

' k0 x1 G* e8 C" G: q--------------------------------------------------------------------------------
% x" M4 g1 j% K) e8 ~1 JBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面/ O0 s+ s6 g* c: V7 n  l
4 l. I# U  I3 z1 d( @: h
--------------------------------------------------------------------------------0 ]- ?* b# o4 ?  O, B
① 初始化 COM 接口:
& d8 ]0 e; C, r. |# @% I   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。9 t4 u/ r5 d* f3 w' T, v0 O" x/ _
  这两个函数在 #include <comdef.h> 里面定义。
6 a. U; _$ \/ ~
: M  G7 J/ l& F/ T0 }0 M6 g9 F2 b' R& t② 获取访问 WMI 权限:
& r. F7 ^% R' ^' W8 y1 {   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
: T1 |$ Y, P* t2 r   如果这个函数返回 S_OK 获取权限成功, 否则为失败。2 d2 |+ y' L# o3 d
2 \2 ~( n, |; s" J3 Z/ Y+ ~' `
③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:" a  v1 x7 p1 [5 W6 B- ^
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。+ c9 P. x$ O0 C% u/ {. N# T

9 ~9 B% ^9 n; q6 f' kvoid GetWmiInfo(TStrings *lpList, WideString wsClass)5 q/ F$ E* i7 L7 z
{4 n( w/ J" ?! p/ p0 a! l3 l, d" I
  IWbemLocator *pWbemLocator = NULL;, z. j, p4 k1 u* }4 v
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
" K7 u8 U( M6 V# T: T   {
* I/ M9 N) U4 n6 w- J; A; A     IWbemServices *pWbemServices = NULL;
+ x! E& [2 v1 n" V  Z     WideString wsNamespace = (L"root\\cimv2");
( U/ h% l' S. i0 p     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)2 O8 {& C; Q- f$ Q4 k, q* }$ r, |* r
     {! ?* P9 H' A# N1 t' Z
       IEnumWbemClassObject *pEnumClassObject = NULL;
: U. R( _5 z3 u2 k        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
% n2 s5 L9 R: f- n' J/ E9 K        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
7 Z$ ?. Z& R6 Z& _' w( f         {, s# @! E+ N1 `, z4 T. P
          IWbemClassObject *pClassObject = NULL;$ Q8 a% H2 b4 M5 Y% B+ @/ o4 E% e
          ULONG uCount = 1, uReturned;
* f/ r+ B/ Z% z% r& C9 L2 b6 J           if(pEnumClassObject->Reset() == S_OK)6 C& l+ e' n( h' j9 J( l% z
           {
7 [8 z" Z  M% J: L/ @% r7 S1 @7 d+ E0 J              int iEnumIdx = 0;) H  T6 \& d9 w. d
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)( b  U: ?) @3 a2 A
              {
1 j% W: G' {8 E6 g- F( D8 H                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");- H( A+ `9 v. W& \3 D- y& g' {
% \4 @6 L2 T' \
                SAFEARRAY *pvNames = NULL;$ A9 m! W0 H1 ]4 s0 `
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK); b% V% U. r- I
                 {
1 Q) }* b+ C7 t% j" a1 M  O" w4 b3 B                    long vbl, vbu;
' m; h. L& z, Y- J) E  c                    SafeArrayGetLBound(pvNames, 1, &vbl);
; E- b3 F) t+ e5 U/ }: W                    SafeArrayGetUBound(pvNames, 1, &vbu);
& C0 @9 y0 s; f8 U. l                    for(long idx=vbl; idx<=vbu; idx++)9 O) d* B$ D. i. l; V
                    {
% v: R  Q( N! K7 @- [# n! b                       long aidx = idx;
0 V1 }. m" l' o' W0 l( a, ~                       wchar_t *wsName = 0;& y- ]: H" ], }2 E
                      VARIANT vValue;
4 }( K) ]4 H) r3 O- ~                       VariantInit(&vValue);5 t: e9 p+ N) C5 ^% D- p' j& ]; \
                      SafeArrayGetElement(pvNames, &aidx, &wsName);- g% g  r0 Q, `% m

2 c2 q  @2 [0 _                       BSTR bs = SysAllocString(wsName);
9 x' x+ P+ `2 Y                       HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
1 U% q9 K- A# R+ d+ x+ r& H                       SysFreeString(bs);
; b1 S1 [3 s0 G$ m" p% C  h# [8 q# V/ M1 b; A
                      if(hRes == S_OK)* ^8 a5 O5 ]: E: `; \1 W
                       {
6 V* @! L0 [5 B7 H$ r                          AnsiString s;
2 Y- g/ j5 W4 j5 C2 u; F0 X                          Variant v = *(Variant*)&vValue;
/ N' j' U. u. a! a! a( U                          if(v.IsArray())
8 r4 [6 T; d% s, l( N- X                           {
, a$ \6 f( p# t7 ~- e' q: W                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)7 h3 V7 O4 ^! ?" J$ O2 y! H) d) W
                             {
" ~0 b: F+ |  N5 i/ f                                Variant a = v.GetElement(i);& k- A' n- v  m, {5 w+ O" m5 }9 b
                               if(!s.IsEmpty())
6 ?2 L7 |' @' p1 b* N$ [; G; S6 S                                  s+=", ";: J: \6 M8 D8 X) O1 V: ?
                               s+=VarToStr(a);, _& ]9 F) V+ o5 G, f* Y$ [' T0 Z
                             }
+ Q9 x$ J- V/ H5 J% p                           }8 n7 K- A7 v  r: i1 P
                         else# ^2 {  A" a  K
                          {+ I. Q  o- d4 D0 T' {. S, D
                            s = VarToStr(v);
/ n0 \3 G. t) Y  f$ h# {. H                           }
6 m: S! `4 h4 M& Y, _) e                          lpList->Add(AnsiString(wsName)+"="+s);/ D0 H; w9 ?4 U( C) G
                       }. R' H% b6 K+ X, g8 o' \7 i
; l( f! |7 O9 Y+ J) M' ?8 v
                      VariantClear(&vValue);5 \: N' y1 I/ J: k
                      SysFreeString(wsName);
/ ~  H  w9 h/ d5 F$ G5 @7 }                     }
" n0 k- r  [- L& l  k; k                  }2 B# [& s- O% C7 {$ J% o% v# D; ~6 ?/ \
                if(pvNames)SafeArrayDestroy(pvNames);
' B7 d7 S. |; ?2 A                 iEnumIdx++;
$ V+ t9 j5 g" s8 K+ a, o               }
1 g0 O: J3 X) p( {            }, M! f/ f5 Z) ]: H: m) i" o
          if(pClassObject)pClassObject->Release();
4 b9 X- H" p4 a* b: b4 m' v  X         }
) u" P  U8 s: q$ G9 h) d/ L2 N        if(pEnumClassObject)pEnumClassObject->Release();) Q: N9 i2 x$ G8 U4 ?3 U' i
     }7 G5 H) Y+ w/ I7 H9 C
    if(pWbemServices)pWbemServices->Release();3 `2 O$ X0 K* q& }# |
  }
2 h& |- z( Z& r5 d% i2 H  if(pWbemLocator)pWbemLocator->Release();
* J4 o/ S1 _1 `% z' L}
7 I' g/ \$ u- }) u3 g7 x) m//---------------------------------------------------------------------------( V/ h6 r  Q, E3 w2 r8 i: y

* d. C" F9 m$ H7 @" m$ E* D// 通过 WIN32_bios 获取 BIOS 信息:
6 s) f4 q9 ~/ ^void __fastcall TForm1::Button1Click(TObject *Sender)
# @) b8 B0 z: n0 k{
( D' V; Y) H: ^. ^- |    Memo1->Lines->Add("================== [WIN32_bios] =================");
8 d0 C& E! ?: {# D    GetWmiInfo(Memo1->Lines, "WIN32_bios");
& b% |+ r% [: U! F: N0 i$ G    Memo1->Lines->Add("");
9 ^$ J6 l: H( ]- ?}
. U% K' P7 Z% l/ ~( X$ G  a* z
/ G: y4 ?! h" F; A6 F- X) u--------------------------------------------------------------------------------. R8 U" q3 u' ^

3 U; N# e' J/ ^( B6 }* pWMI 可以访问的信息类型有:: L' s8 g; q& \  C. l8 E: `7 Y8 s
  Win32_1394Controller) f& ~0 \! T2 N+ d6 Q4 a* b. ^) m
  Win32_BaseBoard2 s( k$ o/ k# R" j4 ^) ?* u+ Q
  Win32_Battery: K/ s8 p8 w( |
  Win32_BIOS& L3 O+ h: s, {' [8 @) i
  Win32_Bus
2 i% N. r4 n- n* p! G1 a9 y   Win32_CacheMemory! [( l- n& F: H5 ?" `8 |
  Win32_CDROMDrive
7 l$ w+ w7 z6 I) m% a, ^/ a- P8 V   Win32_CurrentProbe$ z1 t4 c7 N- P# M( ^# F. U
  Win32_DesktopMonitor: O( ~9 l8 X, N  j/ O
  Win32_DeviceMemoryAddress
: \1 n, E; l* M   Win32_DiskDrive- H! Y0 B% a, l
  Win32_DisplayConfiguration
- ]" I) _! `, K) Q/ h, j   Win32_DisplayControllerConfiguration4 C7 d. E6 Z. D, T' Z  V& I! O
  Win32_DMAChannel
& m* X4 r: F2 O' R   Win32_Fan
) k0 N: v+ H- H; n   Win32_FloppyController  v2 j% H. k" {
  Win32_FloppyDrive# ]' u8 ]: W. o% d8 L
  Win32_HeatPipe; ?/ |. D  c, ]% Z5 j
  Win32_IDEController
& ^) s  ?. ]6 U9 P' Q5 t   Win32_InfraredDevice
& e4 A9 t% H$ L   Win32_IRQResource
7 p# i, ^6 G# m( c+ I   Win32_Keyboard
( l$ j4 A% v  {' t8 y$ c+ ^6 e   Win32_MemoryArray
' H- e  G- c8 Z; [; i# W   Win32_MemoryDevice
/ V% |2 H; ~! w( C1 x( U1 v   Win32_MotherboardDevice$ b" z2 M2 F" F- n- d
  Win32_NetworkAdapter
/ v- C# J6 ?# F$ n   Win32_NetworkAdapterConfiguration
+ t( U) }8 {8 k" X  r   Win32_OnBoardDevice
/ Z# T" S/ J& `& O3 @/ r   Win32_ParallelPort
5 d, X: U" k2 o7 ~" C; U" A   Win32_PCMCIAController) S3 C0 ~% ^7 n) X9 o2 |3 E
  Win32_PhysicalMemory# A* L# o; w; U& I4 Q5 [4 g9 Y
  Win32_PhysicalMemoryArray" c" W% T9 j3 \  w  b
  Win32_PnPEntity8 e8 ~0 ~  c4 R# C; Q
  Win32_PointingDevice$ K3 w$ f& z6 u6 }4 u2 ^, t
  Win32_PortableBattery/ a  T% X, i  r
  Win32_PortConnector
& q4 A- W# Y/ a, C9 ~   Win32_PortResource
6 \" \. a  B3 t9 i& h% z   Win32_POTSModem7 I+ \' M8 `& i1 F) Q8 K
  Win32_PowerManagementEvent- z+ R- ^5 D- O' `  y4 Z
  Win32_Printer
% l1 r8 q3 m: Y8 I2 Y$ r   Win32_PrinterConfiguration; c2 F' C6 X& n7 m3 f) {* y# _
  Win32_PrintJob: O( `- b0 K+ q, T' ?- r; m$ \
  Win32_Processor7 h! m* O1 }9 j6 @- Y; b$ @) z! p
  Win32_Refrigeration
8 O$ l- |- p( a5 B9 M* f! \   Win32_SerialPort& d- a4 _3 A  ?( h- N
  Win32_SerialPortConfiguration
- D- U: A, _; J7 Z8 r  v7 M   Win32_SMBIOSMemory
0 ?+ m. M; x6 z7 p3 I9 H6 l   Win32_SoundDevice
# U; G6 l6 E9 Q' G& @3 k: @6 t   Win32_SystemEnclosure2 C4 m* [, C; D: x9 Z
  Win32_SystemMemoryResource
- w/ b' T( {7 m   Win32_SystemSlot& C% {8 [/ R2 D9 T6 n
  Win32_TapeDrive- r8 I" W) d6 s, W2 q
  Win32_TemperatureProbe: e+ z$ ]; I8 e& B8 D, g. ~/ g& D3 ~
  Win32_UninterruptiblePowerSupply
# F* K; H9 p: U1 Z' l8 M   Win32_USBController
4 D3 u% T# Y, }" w& ^   Win32_VideoConfiguration8 @. N1 {( M& h2 S9 J) y8 [
  Win32_VideoController
. |1 u+ r7 e  s$ w# G4 Z( L2 n) z$ [   Win32_VoltageProbe
' E1 ^% s4 T  ~+ C0 T0 M& m2 Z
5 J  c8 p# V0 [$ O! L4 b# `( u4 m以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-5 06:13 , Processed in 0.019304 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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