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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)& Y0 N4 M# l8 ]: @$ `! V" D4 V: S7 N
7 E$ K2 p* S& c

6 X0 Z5 l& {( n--------------------------------------------------------------------------------. I+ ?) u$ I0 z
WMI: Windows Management Instrumentation (Windows 管理工具)
2 c+ a9 w$ {- ~. e   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。 + F8 O5 ?& f' ?/ y5 [: B  C, Q8 i+ f
  利用这个工具可以管理本地或客户端系统中几乎所有的信息。
. f2 t' T2 ^# L# J1 ~   很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
! _* \& @! U4 T
$ y$ o" s+ Z/ m--------------------------------------------------------------------------------
0 t% }8 Q! L2 ~1 U9 N; NBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面
+ ~6 R9 ]- Q2 T% W, B0 K6 a7 D3 l* g7 p$ S" Z9 P' y- w
--------------------------------------------------------------------------------. q9 A+ o& c0 l: Y5 i
① 初始化 COM 接口:
9 E7 s  F/ t7 R# w   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
/ o# I9 W( e3 a/ M( U2 h   这两个函数在 #include <comdef.h> 里面定义。+ D# O1 ?5 Z9 M' e  `! m
! W$ h' u0 V  d
② 获取访问 WMI 权限:# [. t$ f, c1 @
  CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);
8 E0 k+ ?% H1 w  t   如果这个函数返回 S_OK 获取权限成功, 否则为失败。; r3 o$ G  z" U8 [, U

2 a1 n% _/ e1 P! I7 j8 ~% Y③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
# s$ u6 y% ?6 p; m   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
9 s' \$ F1 O$ X3 c
! C  n1 S& W$ [8 Q! G) Ivoid GetWmiInfo(TStrings *lpList, WideString wsClass)' i1 J/ @3 A$ h
{! [0 ]& F6 s9 Y0 D& T; ]  `8 {
  IWbemLocator *pWbemLocator = NULL;& c3 w! C' x' Z2 e; C% c( \# y, ?
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)8 O/ q: G2 w$ C' T; V
  {
1 @6 i& _) `" I: u" t8 e: R/ ^, }     IWbemServices *pWbemServices = NULL;
5 Y& r- R( @% Q+ u- {     WideString wsNamespace = (L"root\\cimv2");8 q, h5 K4 ?7 \/ E5 o- \  ?
    if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
! u/ e. S( s5 |( v' q& O0 n& d      {8 V8 \2 v2 s* _& W$ q9 E7 _
       IEnumWbemClassObject *pEnumClassObject = NULL;
: \$ g# b" p5 l* c2 j9 Q        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;
$ ^& A2 G- z6 G9 h* k5 i5 Y) ~, t        if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK): c2 X' y. H) ?0 U
        {- U& [- U; O/ w4 r% I# q
          IWbemClassObject *pClassObject = NULL;
# m( d7 m5 v0 d* Z           ULONG uCount = 1, uReturned;
4 I& P( H! q2 B% `, W0 P           if(pEnumClassObject->Reset() == S_OK)
* k( m' k; I' N9 @' H8 G# C            {
3 x5 K- U: I9 N8 j" m. X& X              int iEnumIdx = 0;" B/ S! P) U2 _% W
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)
3 [7 {# U4 C, F               {
4 z8 T. o2 t& L% i& ^3 i                 lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
8 w0 t) [, A8 t- j' q( S7 ^% \+ \; \. t8 b( H; N! g7 V3 K
                SAFEARRAY *pvNames = NULL;9 q9 t6 p( Y' x; o* [- P$ d
                if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
8 V4 `' f! Z/ @4 m9 g3 i                  {
# N# y: b& C4 v% `, z                    long vbl, vbu;2 N% _& M& n- S5 [
                   SafeArrayGetLBound(pvNames, 1, &vbl);
" S, e$ k8 }  q# J+ j% q                    SafeArrayGetUBound(pvNames, 1, &vbu);# W* F+ M( {( `" k" ]; i4 n
                   for(long idx=vbl; idx<=vbu; idx++)) C  g( m: F8 L0 J5 x" ^9 N0 ?
                    {+ M% E# K4 G! A% P
                      long aidx = idx;
, K0 ]' w1 `" \                       wchar_t *wsName = 0;
$ z  ~& n5 T& f! H. C                       VARIANT vValue;% R! }7 y' j5 j1 t% l; k/ N
                      VariantInit(&vValue);
5 E3 p$ Y7 l( R) G4 M5 j" b                       SafeArrayGetElement(pvNames, &aidx, &wsName);
5 o+ I4 k9 x" ~- |. V- |; |/ x! D. o9 I% c
                      BSTR bs = SysAllocString(wsName);6 T0 Q6 e9 r( z4 J1 k: r- ~) t2 t
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);* l* X. L3 [; A  }
                      SysFreeString(bs);( ?1 C! B1 ~- @3 G7 u

. q  A3 L8 h  D  ~$ J6 r                       if(hRes == S_OK)
: L' L. {  G" c0 S( P! h                        {; ]  K9 V* e3 t) T, t0 Z/ u7 w
                         AnsiString s;1 k3 X$ o$ e2 v& L, M
                         Variant v = *(Variant*)&vValue;
# B9 y5 c0 Q. K- z6 W                          if(v.IsArray())- R: |$ v) c8 r5 L) n) ?8 u9 L/ S
                          {
: w* Y. O; l7 F0 K                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
5 r# I4 }( D0 A( E' e6 D                              {
& ?: y) C; L- I( X) [                                Variant a = v.GetElement(i);2 ?5 m" g& H6 B. ?, S' T" y5 U/ c
                               if(!s.IsEmpty())2 {* c! K% S# H3 L) o% C
                                 s+=", ";
: k! W6 K8 b: ^# n% m                                s+=VarToStr(a);
6 M& @/ N8 Z' Z! d  c! L                              }
5 R/ z, l" i5 r1 X/ q- k                           }5 @8 `+ w, N" I; Z- T
                         else* Q2 A& o2 h: q8 F1 F0 w1 r  g
                          {
+ \# y9 O- l  C4 u& a6 s                             s = VarToStr(v);
6 S3 Q7 ~6 k' `5 W                           }
" ]5 j1 D3 v: U) I/ E                          lpList->Add(AnsiString(wsName)+"="+s);  s5 S6 p5 t/ S1 ^- m# U) a
                       }
( S5 U) `) }& P; {$ x. a
& f5 G6 ]& }. y                       VariantClear(&vValue);8 G2 L4 C6 g5 U! k/ }' S
                      SysFreeString(wsName);
! v6 u, q: C, U9 W: j) p' j                     }
% K; E" H& b. w5 z                  }
  Y: p: `, u4 T( i/ T- U                 if(pvNames)SafeArrayDestroy(pvNames);# {" D+ _9 t  z9 Z' k: g
                iEnumIdx++;
% a$ b  Q/ M3 v  Q7 t; j) [               }- ~+ \$ H5 v% c6 x
           }  B, U  w" ^9 n+ K  x: h
          if(pClassObject)pClassObject->Release();
4 f. z' Y) Q9 w3 L         }
" Y% I- H) x% _; H* \& B        if(pEnumClassObject)pEnumClassObject->Release();
# }' E" q1 I% v/ i$ {      }
3 q$ f" Y% w( g3 a# I     if(pWbemServices)pWbemServices->Release();4 e  G) _) K( c' D+ U. u3 \
  }
9 @# r- |: w8 Z6 g' @  if(pWbemLocator)pWbemLocator->Release();) q, Z) B3 d6 e2 v; c0 o: ~
}# p6 P- l4 }. B# w: H' {! a$ f0 w
//---------------------------------------------------------------------------
  u5 g4 C1 @9 t5 ]
3 K+ x; s4 n: e// 通过 WIN32_bios 获取 BIOS 信息:! \  G& }6 n( r
void __fastcall TForm1::Button1Click(TObject *Sender). {' b" L  B2 ^+ d: p
{* O9 _# W8 y- X) g& F
   Memo1->Lines->Add("================== [WIN32_bios] =================");
! C: d, }/ a$ \" G$ c$ q1 E    GetWmiInfo(Memo1->Lines, "WIN32_bios");2 ~; i* M# H* {+ D7 x3 b3 U' Q
   Memo1->Lines->Add("");6 ^5 \0 R, a( Q' O
}& v. D" W: b- `
* N  O. g- D& f0 n# `, S
--------------------------------------------------------------------------------
/ x! S0 v" ?( h# q7 C: m* k+ T/ R" [  J; o
WMI 可以访问的信息类型有:+ f" s. F- c* m7 Y7 q
  Win32_1394Controller; x$ j$ k% g" Y4 q
  Win32_BaseBoard+ M' j$ b' C. {# o  f3 A% I  M
  Win32_Battery
( |3 Z  [  y- X+ _6 t7 T) G% K   Win32_BIOS
( G; b5 A& \! `* y3 g9 S2 w5 X/ C5 W   Win32_Bus- W+ i( ~% c- w
  Win32_CacheMemory& W1 z3 K% R6 F- J6 ^( t$ I+ k; t
  Win32_CDROMDrive
7 w# ]; Q" }: f2 c: K4 ^/ \   Win32_CurrentProbe
0 I1 m3 N3 \/ Q) d3 k   Win32_DesktopMonitor
( w; Z8 Z$ i6 q, J9 x$ U0 E& ?9 i   Win32_DeviceMemoryAddress% g& o! j: Q0 D9 v
  Win32_DiskDrive
) B. X! N! ^/ |9 Z! F9 F' O   Win32_DisplayConfiguration
2 s/ }; h7 r2 y1 W- C6 n+ G& _   Win32_DisplayControllerConfiguration
" H# F4 t6 i% @' C# f* V; {   Win32_DMAChannel$ |* T* X: d1 V* q! a/ P
  Win32_Fan% g$ F- y4 w  Y& }& X, r
  Win32_FloppyController4 _! w# Y9 [8 P3 ~  I/ ]( J5 O5 g
  Win32_FloppyDrive; [, L! r" M  L  E  e
  Win32_HeatPipe
8 a$ Z' A" D  u; {% O# L+ O7 J   Win32_IDEController" W+ C, K% N' H. i9 g4 N
  Win32_InfraredDevice) ]' P. E2 w0 S2 o
  Win32_IRQResource
( o; Y, v0 b. \: K# p% T. A9 U   Win32_Keyboard
5 `8 b* h, |+ M. G   Win32_MemoryArray, R* E1 [& b! ^& e- V
  Win32_MemoryDevice
! J, D1 p7 M1 [6 [/ G2 W   Win32_MotherboardDevice) V; B* H& v* Y: A- I! H! B0 A
  Win32_NetworkAdapter, m) U$ r" A- ~# h$ @/ m
  Win32_NetworkAdapterConfiguration1 V' n4 X3 S7 M  R/ i3 i
  Win32_OnBoardDevice) b, g" T/ {$ s
  Win32_ParallelPort/ E" D' P0 x; ?
  Win32_PCMCIAController: {" |( M# |6 H% d; s8 z; S" O
  Win32_PhysicalMemory# F+ }9 |" _# p& o) m# r/ m8 J% P
  Win32_PhysicalMemoryArray  Y- m6 X- v' @
  Win32_PnPEntity5 L9 m7 m) y: M5 y# z5 _
  Win32_PointingDevice3 I* r7 j9 ~& Z# G& l+ F
  Win32_PortableBattery- [' p- t2 `( U. p
  Win32_PortConnector0 f, X  h# q. j
  Win32_PortResource* ^$ B1 q! p. x* T# q$ ^- V
  Win32_POTSModem
; b2 V4 t, C  y! J% Y& I6 W# I- Q   Win32_PowerManagementEvent
; D, F" v: ]3 u" |2 P: Y' {# K- S   Win32_Printer) m1 P0 y2 t$ c
  Win32_PrinterConfiguration$ g2 J3 o; H* a, O
  Win32_PrintJob. |  j7 _% q) c3 \
  Win32_Processor5 @5 V0 B/ J. {7 o# g
  Win32_Refrigeration
& V5 T  d' e( t4 J   Win32_SerialPort
" ]$ E0 |2 t8 E   Win32_SerialPortConfiguration! n" A3 \4 J" ^9 X3 g/ W0 J
  Win32_SMBIOSMemory& x# {0 ~" H# N8 y8 p
  Win32_SoundDevice
# {  e4 t/ H3 @, ]$ Z! ?- W9 A   Win32_SystemEnclosure9 p3 S, i; ~* V/ a  A$ z
  Win32_SystemMemoryResource
* N- K+ q, I+ f" P5 U+ J) h   Win32_SystemSlot% b. z# m  K( e9 }5 k7 c; j
  Win32_TapeDrive0 m7 N8 Q$ f" }" [1 ]4 v0 v* o6 i8 k
  Win32_TemperatureProbe
6 q' s. [1 V/ G4 Y. u, P3 S; j5 M   Win32_UninterruptiblePowerSupply
$ F2 I! E+ l1 Z. |  H7 p   Win32_USBController) q# m, L- r8 E$ r6 C! V
  Win32_VideoConfiguration% C: a( A- ]! ^9 g6 Z; D
  Win32_VideoController
) G; O! {1 {5 u+ n, x' [& j' X- h3 \   Win32_VoltageProbe6 N8 U9 M% M$ N$ G6 H
7 F" b1 m4 z5 o
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-3 23:41 , Processed in 0.018445 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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