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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)+ f! a2 O. [2 {& K9 w

# {+ a: `. i- I; a, {7 C  x1 J" I0 V7 V+ n/ J. |( D
--------------------------------------------------------------------------------
+ e7 g! Q" {6 y( e2 rWMI: Windows Management Instrumentation (Windows 管理工具)! t+ v& {% s1 j8 T& Y
  通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
: b9 l4 ^" Y. p5 R2 V0 F: ]) W# n   利用这个工具可以管理本地或客户端系统中几乎所有的信息。: G. w& b( x1 A; e" ~. u5 I0 }! G
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。 8 \, l$ g2 \/ R0 D7 b" Y
/ a" I- p, a* [8 ^) P( Q
--------------------------------------------------------------------------------
- S8 U! q; A6 m$ O0 YBCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面- o- A( I) R6 k) C; a+ e9 T# d
/ Z& E: U6 h/ i6 \+ S* t2 F: }
--------------------------------------------------------------------------------- d2 n/ S% F8 ^4 p7 f: B  s
① 初始化 COM 接口:- I0 g9 M, _, Y/ u
  访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。) N5 t+ }( T5 s# U2 Y5 ^
  这两个函数在 #include <comdef.h> 里面定义。
9 h- i; z1 O9 B  q! u* z. [9 y# B! o# Z* k- e
② 获取访问 WMI 权限:
. b5 a7 K3 z9 g# I) G3 t- U0 Y   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);9 i# C8 B6 M" _) F4 _8 m& l
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。2 g. _7 e6 l4 z! o

. H$ |% C4 b, ]4 S" W  ~③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:; Y% R, s2 y0 H
  这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。
3 y/ q, `& h1 D$ r
( g4 }3 Z! v  F" n5 D" Uvoid GetWmiInfo(TStrings *lpList, WideString wsClass)6 t" U# C- {6 N2 H3 p/ k2 ]6 _* w
{
- P9 B2 h6 Z; N4 c0 y  IWbemLocator *pWbemLocator = NULL;2 Q% u) n6 P! _+ V
  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)" v2 K9 S/ W5 m7 ?& R
  {
( v' T; b2 O& o' B, k     IWbemServices *pWbemServices = NULL;( n' ~1 j9 D% A7 w  k, C& i
    WideString wsNamespace = (L"root\\cimv2");
& F; `0 p( {3 B0 c; x/ W; p     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
: J+ k3 b( j% J8 A, ]$ _      {. c8 D* W: F+ c
       IEnumWbemClassObject *pEnumClassObject = NULL;
) {( `8 c: w7 v+ Y! {        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;) l# v! @+ s: d$ E* L0 U* p) M9 R
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)1 ~+ R+ Z% \: i
        {
. z$ {9 a% U  u6 A: o, A           IWbemClassObject *pClassObject = NULL;7 w% |% F  i2 O. Z8 ^/ `
          ULONG uCount = 1, uReturned;. S+ E! \% c2 q
          if(pEnumClassObject->Reset() == S_OK)
! B2 F! |$ X; q. _1 p/ ^4 @            {: w$ S" s: ~0 i3 {# P  W! Z0 O
             int iEnumIdx = 0;
0 B3 L. b7 G( G              while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)2 H9 B3 m  [  |' h
              {' Q/ Q+ {- |5 ?
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");9 T4 N) s5 h- n% l: ^, e9 x  v

' R0 n; l( q3 p- f9 {                 SAFEARRAY *pvNames = NULL;
! i8 T. ?+ l# c0 V! G$ g: A0 d2 ^                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)% R3 E, m! T! p, H
                 {
3 _! {8 S% n6 h% a                    long vbl, vbu;
8 [4 n" V8 K: Z  x! n9 K                    SafeArrayGetLBound(pvNames, 1, &vbl);
7 [; k$ |4 o# k" i  P: |                    SafeArrayGetUBound(pvNames, 1, &vbu);
6 M9 l1 M2 r0 V9 `; `                    for(long idx=vbl; idx<=vbu; idx++)& \1 i: r  Y$ T$ c/ p7 q
                    {
! ~4 ^9 x5 r+ S" M4 N5 H                       long aidx = idx;) S$ k" s8 T  k8 F  ^4 `' p
                      wchar_t *wsName = 0;
! g% q) i" m: b- Y* o                       VARIANT vValue;
, q, m; q" E/ ^# c" w& x9 n' Z                       VariantInit(&vValue);2 Z% h! w5 E( D- X
                      SafeArrayGetElement(pvNames, &aidx, &wsName);
) t0 j' _2 S7 y4 U8 l: g/ ]! h
3 f8 V% g" }0 `) `& w( _) o+ K3 S                       BSTR bs = SysAllocString(wsName);7 z# y% Z/ N* U& [
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
) ^. P4 C% t* i2 \( c                       SysFreeString(bs);
7 }; s, M* f/ e: A
! d' n# f( V! b4 ^3 U7 U% U6 t3 a& ^/ d                       if(hRes == S_OK)6 z8 }! B4 e4 Q0 u. v1 d, N
                       {
* ]1 K" J, q- p. p5 i2 \- E                          AnsiString s;. m, y6 k( K. W( C
                         Variant v = *(Variant*)&vValue;( M: `, h. v, R% Y8 M9 O8 K
                         if(v.IsArray())$ X6 ~9 [! [' Z+ q+ N
                          {
6 i) Q, a( F% L, M9 ^5 H+ F                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)* }) D- S3 ]- B! m) O3 m+ F" s
                             {
# |& Q& \# I2 ^7 S; L5 L# |                                Variant a = v.GetElement(i);
  e2 G& \2 i- g5 M                                if(!s.IsEmpty())
; s; v. ]+ n/ i' w: q                                  s+=", ";
, K6 p, F3 i" K, S% B                                s+=VarToStr(a);
* f$ y: n: `# e; @+ @                              }# U1 L/ K  g" P7 Y" G2 L* ~5 c
                          }9 B' U; c% g% l" x+ n- [
                         else
8 h- r! g" W7 [  Z- k  V                           {
: u) x# n; L2 v' C! |                             s = VarToStr(v);
/ t& l# M+ `) w$ \& a4 l: d                           }
: V3 G2 ^( ]: u" `                          lpList->Add(AnsiString(wsName)+"="+s);
$ A7 b6 s' b" u7 g/ `% {                        }
( C8 f/ Q% b. Z) j( f% B: v
7 I7 O6 `# s- N4 q4 M                       VariantClear(&vValue);) ^  Y5 V" Y- y: ?9 M# h& ^$ y7 i: O
                      SysFreeString(wsName);& K4 W! J2 k. k8 [  p% ~$ c
                    }
; K+ W. c+ y$ s: z  i' Y                  }% ]0 B7 F( R- V$ Q2 e+ B9 J4 a
                if(pvNames)SafeArrayDestroy(pvNames);
6 q5 F/ z! Y0 ~  u- \                 iEnumIdx++;3 D1 B$ I! S; n* @: K4 |! t
              }
. F" k; {& ~- j            }' ?( f. R& A0 K, u+ t- B/ t
          if(pClassObject)pClassObject->Release();
) L/ R0 W! f+ [& d' t. G1 x7 x         }+ ?% b: }5 s1 Q+ M/ o
       if(pEnumClassObject)pEnumClassObject->Release();! S+ f" q' q- F4 Z1 `
     }
: f! W( K9 ^/ t: p4 p     if(pWbemServices)pWbemServices->Release();8 I  a  N8 }* c9 I7 s; D+ w' U7 G
  }( d  S# B+ `6 ]* M; e+ E( D
  if(pWbemLocator)pWbemLocator->Release();0 f' A6 C1 k/ @  k
}% K$ v" }5 N; ]2 J* q
//---------------------------------------------------------------------------  e# G3 x# D7 g5 ]3 f

* L( Z" [! P% f8 s1 p// 通过 WIN32_bios 获取 BIOS 信息:
% E" A; O; \& zvoid __fastcall TForm1::Button1Click(TObject *Sender)
) i* v  ~6 V' q* W* X- {{$ u: A/ \$ {9 B/ [
   Memo1->Lines->Add("================== [WIN32_bios] =================");$ j6 R( g. A: c" u% ~9 I" P
   GetWmiInfo(Memo1->Lines, "WIN32_bios");
. p8 h3 L3 y7 ?    Memo1->Lines->Add("");* g% S3 L# B, t' u7 n1 V1 U' T
}3 V" D# z" y, t/ b8 F
+ I! x# K. L3 M0 d0 H
--------------------------------------------------------------------------------* e& {' E1 p' P1 k

! H( b4 t: ]# u. P# H) C+ EWMI 可以访问的信息类型有:  V4 `; ^9 E% n0 v
  Win32_1394Controller
  S/ p8 u+ b! V& s# }7 |   Win32_BaseBoard" M0 S: g" N- M, z
  Win32_Battery1 ]  v  a5 [: F/ S% I
  Win32_BIOS
( R+ D5 z  _1 j) p# U, U   Win32_Bus
; I$ A; a3 L! C+ Q- N/ B5 c+ D   Win32_CacheMemory7 [; z: G( d  M  B5 t
  Win32_CDROMDrive3 T: H0 p5 }3 S
  Win32_CurrentProbe
" x0 [8 g2 Q+ m" j- ]6 x   Win32_DesktopMonitor* B4 J! Y0 S; l" O
  Win32_DeviceMemoryAddress& U% C0 h' X) T* b; O9 f% q
  Win32_DiskDrive
7 a* W: o( s( ~5 \0 l$ n   Win32_DisplayConfiguration" ?, l& r) g- [
  Win32_DisplayControllerConfiguration
9 Q- [# {+ a2 w2 `   Win32_DMAChannel
. m" \. K$ G: t+ f  h% O2 ?" }   Win32_Fan
! Z5 p# w1 q; c2 t$ i   Win32_FloppyController
8 B' _  n; C' h% D" @: Z# v   Win32_FloppyDrive
2 a% r8 K6 V6 G$ a: ^   Win32_HeatPipe
1 X. u  s4 _  [* K   Win32_IDEController; O# ]6 X: D5 S5 V
  Win32_InfraredDevice. {$ |( I7 _; X/ j2 A
  Win32_IRQResource2 h+ M! m, `# |4 w# u% e2 D1 \
  Win32_Keyboard
7 s! K, v5 T5 C4 ~" F9 t- A2 r   Win32_MemoryArray
) {! E* @% n3 S, H% E; X9 ^. e6 d   Win32_MemoryDevice: w% v' S6 m0 W4 x5 b  g6 F7 m# d8 h: g
  Win32_MotherboardDevice! ^* \- N  l7 \% I
  Win32_NetworkAdapter! W4 N& r: L9 ~9 y6 ~
  Win32_NetworkAdapterConfiguration" \" e- i, N$ j/ K8 A. i, M# [/ n
  Win32_OnBoardDevice6 y, C2 O  B8 F- G' e
  Win32_ParallelPort' [! u& R3 Y. m$ s3 i9 p
  Win32_PCMCIAController1 |  o; R4 N  g) s7 }2 B$ t
  Win32_PhysicalMemory
) i' T/ s9 i; o' ^, A, P4 I   Win32_PhysicalMemoryArray
, Z( `# d* k0 d, S; y   Win32_PnPEntity+ W% `4 [9 s; C8 r- t! W
  Win32_PointingDevice3 ?) y; P, h5 m7 h6 u
  Win32_PortableBattery6 m7 g- k: S5 E3 s/ M% v& |
  Win32_PortConnector1 X! M" j) `% _2 B- K1 {5 m
  Win32_PortResource
  ^) F& K" |7 Q. \: K  u6 c6 t   Win32_POTSModem
, j; ]9 ^9 f) T1 y6 }   Win32_PowerManagementEvent  d# E2 D# s2 W7 l
  Win32_Printer
) T$ R# v: I$ e' k   Win32_PrinterConfiguration
8 h$ K9 P  [8 E   Win32_PrintJob* l7 P' ]' d4 Q6 d' l" S
  Win32_Processor
6 e% }; w' ?. \( K7 @2 Q   Win32_Refrigeration8 ^- O% z# ?; i& v1 Y
  Win32_SerialPort7 F+ O4 H; r$ M6 S6 _- \
  Win32_SerialPortConfiguration
; a: e- a, i% M4 Z$ s) ?9 c" w5 ~   Win32_SMBIOSMemory
( q4 x) X0 `' {! X   Win32_SoundDevice4 Z# d  @6 K" M, \0 k5 k
  Win32_SystemEnclosure
9 w% C: y/ |5 V2 c   Win32_SystemMemoryResource; g; |( O! P$ Q- S1 ^
  Win32_SystemSlot- m9 U6 _7 ]. G- A
  Win32_TapeDrive7 w. |" X$ a7 x0 m
  Win32_TemperatureProbe
( T( [; W8 g+ ]. l) Q1 B! g" p   Win32_UninterruptiblePowerSupply
, t% C- ^6 l: }! O+ {5 S   Win32_USBController
) B" F/ U7 A2 X   Win32_VideoConfiguration1 c( S; R3 M& i+ T
  Win32_VideoController
6 J9 A& e. `2 P0 u) Z& Z   Win32_VoltageProbe* A: w  u7 X1 x% i

7 Q0 w6 `! u5 u, z4 R+ J以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-1-11 09:46 , Processed in 0.021017 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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