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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
% P. B) s. ]/ T+ E- a1 S7 _- X! \+ j% l
: C  M0 l( G* B$ I0 e3 f# a9 A$ H
--------------------------------------------------------------------------------
7 ~" w5 o2 M  j1 D, NWMI: Windows Management Instrumentation (Windows 管理工具)
! }# X$ c1 |' c4 x' E1 `0 Q   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 7 m: t5 |* l! w- n3 e, N: n/ Y
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
. k% x( P1 m* l. }* [   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 ; f1 f% a' v, g1 r2 O2 Z) u' F
$ I2 ^' Q% s% E7 r0 Y+ d& [7 k
--------------------------------------------------------------------------------
7 ], T6 s3 f' l7 p3 T6 ZBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
- W2 `' o& f0 T7 O  S! O2 d
9 `8 o! I1 ~/ V: i% g  x1 q/ r. g- N--------------------------------------------------------------------------------. D/ Y3 H+ Y5 m6 M& ]; S
① 初始化 COM 接口:
/ B1 ^; T# O% |& |8 T5 e   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
( D/ w  T5 I# Y7 H   这两个函数在 #include <comdef.h> 里面定义。$ u( K! T0 T* O# M8 M/ \: {2 J

& n# r5 N" b1 O1 N: h② 获取访问 WMI 权限:8 E6 G2 q" r: N& r+ I( }% _
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);* d0 C, H  T4 h" y2 u
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。0 C6 C& b5 @( G9 d5 ]: }0 k

2 s9 J( P& m" H' m- }8 |; r9 B③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
" u) }3 a; C8 `2 T- T6 {* X, u6 X   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。& M, d7 \& u* f/ _
8 `3 A4 f2 m/ N% Y, T- o0 j! A5 i
void GetWmiInfo(TStrings *lpList, WideString wsClass)7 b% `# @; i; j% ~; n/ Q# F2 o
{
6 Y4 q& S- L0 r8 c5 @/ e; V  IWbemLocator *pWbemLocator = NULL;- o# f! D1 e! L' R
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)
# ~3 @/ |# H3 ~2 J1 @# {5 G( k   {: }" [. {$ a! a; z
    IWbemServices *pWbemServices = NULL;1 x* a: W. f. d% J- x
    WideString wsNamespace = (L"root\\cimv2");
- _. A" L: v' _7 o     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
3 \% i+ R# s+ D( Y      {1 F9 Y7 ]. i, ~' G: R0 p& {
       IEnumWbemClassObject *pEnumClassObject = NULL;3 ~0 [- L* K% N* C+ P
       WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;1 j8 Q7 t# \* z3 I% e& O" L6 |
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)
; |+ K+ a3 |9 B6 G2 `         {
1 h3 ^8 q0 z( j' ~           IWbemClassObject *pClassObject = NULL;
! n1 n' M' j) s* U3 M9 X7 j/ O           ULONG uCount = 1, uReturned;
. ^1 n! b2 B# _           if(pEnumClassObject->Reset() == S_OK)& m$ `# U& c& _- k1 u
           {8 n5 r, w4 Q0 l$ G" ^, D2 b
             int iEnumIdx = 0;
1 T6 C& u/ J" W: J              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
6 T7 }3 A& u- t* ~4 x               {4 S! [5 d8 ^5 }' p  D; n
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");0 Z2 C* u% u* I
1 n/ p3 z8 j! `# E9 o, n/ x
                SAFEARRAY *pvNames = NULL;# h  o6 I6 B2 ^# f4 x( _& R
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
: j+ B- @4 z: v9 O$ z. c0 y                  {6 d. ]0 L9 r" C% N
                   long vbl, vbu;
( J5 p4 c0 K* k! @2 R6 r                    SafeArrayGetLBound(pvNames, 1, &vbl);
: E9 P2 Z: S2 w0 N                    SafeArrayGetUBound(pvNames, 1, &vbu);! S& A1 }0 j* A2 |0 m$ n) w
                   for(long idx=vbl; idx<=vbu; idx++)
1 u8 u" W, P! L6 f. A: O  v5 n4 |- Q0 j, v: Q                     {
4 i* j8 H: H! H                       long aidx = idx;; j5 o; X* B' |
                      wchar_t *wsName = 0;' R) ]0 M4 \) G4 A* t
                      VARIANT vValue;
3 d6 ~# w3 N: D4 |; g7 S                       VariantInit(&vValue);
- q3 y1 R3 z7 V  T8 G" b                       SafeArrayGetElement(pvNames, &aidx, &wsName);' k% a0 x( a* l; i3 x3 x2 O! G) H
, S, o$ E6 A4 j3 L; H& K0 w; O6 B( i( e
                      BSTR bs = SysAllocString(wsName);. S1 h9 x3 `) Y! W
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
* D2 I+ |: N5 R6 E4 H+ b                       SysFreeString(bs);4 T2 N( U" K4 }% S7 J! c

9 u, d# m6 c- M0 s6 p0 u                       if(hRes == S_OK)9 O( z; z! k' M3 P4 S7 }/ M
                       {! C& C) U& i2 i) @+ p
                         AnsiString s;. t$ ~6 x2 ?2 U- c/ g% R, s
                         Variant v = *(Variant*)&vValue;# _- Y) L- K1 q. z9 `
                         if(v.IsArray())- w  e/ s. z, U* v
                          {1 q9 L4 Z4 m4 E7 c
                            for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
" H7 [5 w0 L/ f                              {
" B& p$ y* l* S2 N' b+ P                                Variant a = v.GetElement(i);
  E" l" Q: N7 o9 }; C                                if(!s.IsEmpty())
' D: n( X& o9 \- a6 ~$ ]                                  s+=", ";6 z* C: P$ [" u$ s4 k# u
                               s+=VarToStr(a);
, t' S& J4 z2 g7 u                              }% j$ j4 w# O! A4 i, h% e
                          }
2 F( }5 l" m. i9 L8 e* J6 f                          else) [% O% N) h: n* ?+ {6 S
                          {5 W* _+ y, p: O9 m& N
                            s = VarToStr(v);
" s: N  ~) I1 p0 b' \4 K                           }) A- Z) l- T/ Y; {9 o3 J9 B; ?/ g
                         lpList->Add(AnsiString(wsName)+"="+s);5 Y1 v* r5 k  R* N% @
                       }# G2 Y9 Z) t2 H: ?: ~
2 A7 H9 e! n' w
                      VariantClear(&vValue);1 O# q$ a  F! ?. D- Q! U
                      SysFreeString(wsName);0 n( u$ t- H, @/ d! c
                    }
& F* \8 n( {: |& t8 u  E                  }4 g' W* s0 R+ |% S8 Z8 \7 N# W
                if(pvNames)SafeArrayDestroy(pvNames);
5 a! q: V/ ?+ i$ _                 iEnumIdx++;
1 j. N' C' D- l$ a) p' m               }
% C' j0 T8 O8 w' {            }6 ]+ M6 m; P8 e
          if(pClassObject)pClassObject->Release();
% O+ i8 Z; t5 t& \& T5 V+ D# c1 ^         }: I* B+ q2 F; e& i1 W
       if(pEnumClassObject)pEnumClassObject->Release();6 B1 ~- Q: V7 B+ j
     }3 z# r1 D9 C( |) ?' G, P6 u
    if(pWbemServices)pWbemServices->Release();1 \5 C+ t2 W7 _6 |" w3 I2 b
  }
4 U/ t+ G2 a3 R3 j" x, a7 e1 s0 R  if(pWbemLocator)pWbemLocator->Release();
' ]0 H" M8 n' T1 }- r; c- ]}3 p& A9 |. |, ?$ z9 v
//---------------------------------------------------------------------------; i" }/ ]/ r* K3 W* l, u
5 Z4 g6 h& {& F8 a
// 通过 WIN32_bios 获取 BIOS 信息:+ ^" }4 Z3 C- a- A6 ?
void __fastcall TForm1::Button1Click(TObject *Sender)
! q# k: H6 x2 H$ L{" o% B" Z& ~8 M5 }9 X: t, q- M, n4 K
   Memo1->Lines->Add("================== [WIN32_bios] =================");
8 ]( n9 f6 X0 G- y    GetWmiInfo(Memo1->Lines, "WIN32_bios");- ^: ?8 a: F# s5 Q
   Memo1->Lines->Add("");+ y; J1 x8 l) j
}6 R  b( C! [5 e9 B
1 T1 r3 k2 t2 f  k7 f
--------------------------------------------------------------------------------. u; ?$ L$ O7 |) `) |8 _. ^. H
; p6 I  a9 _% C' v9 W4 x
WMI 可以访问的信息类型有:
! w, E2 d$ p8 v' v   Win32_1394Controller$ @; O- ]! n% c$ }- _& W
  Win32_BaseBoard' h( y3 N; U" C9 \* e4 b
  Win32_Battery5 I; ^$ f. c( N" B( {
  Win32_BIOS
" B; Q, |8 I# D+ ^6 w% Y1 k   Win32_Bus4 V$ F% ^" `9 J) \' `: B
  Win32_CacheMemory* C$ `' `4 T& m+ a$ Q( B
  Win32_CDROMDrive
% r  p4 s) ?# M5 {+ m$ N   Win32_CurrentProbe0 V; `" [: n  @3 y0 |$ `& P! j9 g
  Win32_DesktopMonitor) i* Z) Q. J: M5 \% ]  _! I5 {7 n
  Win32_DeviceMemoryAddress
9 E, ]- U) m. W0 L5 l8 t   Win32_DiskDrive: a  K" y4 F: y; w% {& x5 n8 z
  Win32_DisplayConfiguration
( K1 ~/ e# @+ ~   Win32_DisplayControllerConfiguration2 }  K7 Z0 ]0 ]2 D" y$ {. s* |! n5 ^
  Win32_DMAChannel
# d" w( ?: V2 I   Win32_Fan
1 j0 v; `0 ~& |, E   Win32_FloppyController5 T- T( c9 m3 }3 a* {9 E
  Win32_FloppyDrive: o: X+ g: g0 w( K& `8 B1 _
  Win32_HeatPipe
  P: M0 `% K7 L1 c   Win32_IDEController. M1 E4 a2 Y% }' J
  Win32_InfraredDevice
$ p" A/ ?# r2 H+ k   Win32_IRQResource
8 p2 `- y8 _/ z3 O1 \   Win32_Keyboard
! `& t7 S6 K. Z* |( w   Win32_MemoryArray
) {. R: D" X* J) A( @) r   Win32_MemoryDevice
/ w# ^, b( X$ U7 K3 g   Win32_MotherboardDevice; i* ?7 i. Y% Y' o' ]
  Win32_NetworkAdapter6 B, r8 Y+ g( {) U
  Win32_NetworkAdapterConfiguration
% }  e' X7 p) M# ]   Win32_OnBoardDevice
- Y1 q& I; y* l& D# n- [   Win32_ParallelPort  n" {, ~7 s% P/ u9 H4 L
  Win32_PCMCIAController/ N2 n9 R( J: c' N3 p, P5 J
  Win32_PhysicalMemory
2 d/ @1 s. T! }/ G   Win32_PhysicalMemoryArray
/ M# ~* v2 A0 d   Win32_PnPEntity1 S. @4 I. a# E" o8 V
  Win32_PointingDevice
& y7 e  |, a1 \4 N  Y1 M   Win32_PortableBattery  R1 L- F5 ]: I' D4 G. H
  Win32_PortConnector
& g4 O( x4 t3 t' t" Q   Win32_PortResource
  A8 t: k6 f1 {( D8 Y   Win32_POTSModem' f; a  r, f8 p. u1 Q- {. _
  Win32_PowerManagementEvent
3 P6 T) f7 Y, @2 u/ k  I+ C8 y   Win32_Printer5 J2 m) Q% F5 I5 u% s/ S+ U6 K) A
  Win32_PrinterConfiguration# ~& g) T* C$ G. p8 V* V: _- d: k
  Win32_PrintJob
/ U$ _6 ?' W5 k; b  ~1 L: {, o- c   Win32_Processor
& E6 t$ I1 J! ?# ~6 ?9 {   Win32_Refrigeration
# t# V. _  c! }. w   Win32_SerialPort. [" C; S! T) P4 V: N7 j" \/ h
  Win32_SerialPortConfiguration
$ A# L1 Q6 F$ F: X( P" Y$ q   Win32_SMBIOSMemory$ [* f2 N: V+ r& h# W% H
  Win32_SoundDevice
# v5 [8 `( r6 e- b. q   Win32_SystemEnclosure5 \: d* e$ G* K# A5 F) n% t# W9 K0 O
  Win32_SystemMemoryResource
1 r0 d5 |9 Q+ y1 t8 K6 q# z: ^0 V   Win32_SystemSlot
6 N8 ~' a% j2 C   Win32_TapeDrive& `/ |+ _9 C. i$ S' M# ^
  Win32_TemperatureProbe
  D3 @( A9 U. X  Y0 t; N   Win32_UninterruptiblePowerSupply
; Z* i8 R8 W3 w+ v   Win32_USBController) N/ p; ~$ K& O' l4 }: q9 C; t7 H
  Win32_VideoConfiguration
* j% y# b/ \5 a4 {- [  M4 J" V. l. K   Win32_VideoController
: e* u$ s  ^+ F6 }4 Q8 m7 k1 O   Win32_VoltageProbe
7 f+ I& X/ `' v- ^, O3 Y( z+ r. J( X1 T, T' m/ p$ t8 ?) d
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-2-9 02:31 , Processed in 0.018275 second(s), 15 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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