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

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

[复制链接]
发表于 2005-5-22 17:04:00 | 显示全部楼层 |阅读模式
  Victor Chen, (C++ 爱好者)
/ m0 H# s8 o" J& z0 X3 |' Q$ h. M% T7 [) k! K4 u8 r# e) @( I: b! l

( i7 u4 |/ c: D--------------------------------------------------------------------------------
+ C. T) m* f  G) B6 VWMI: Windows Management Instrumentation (Windows 管理工具)
5 n' y% X9 |: ]7 Z. u4 @   通过 WMI 可以获取主板、BIOS、磁盘、显卡、网络等几乎所有的系统信息。
0 Z$ t# _4 w1 T: M   利用这个工具可以管理本地或客户端系统中几乎所有的信息。. \  w; s2 `8 n  e, v
  很多网络管理工具都是基于WMI开发的。在 Windows NT/2000/XP/2003 都有这个工具, 在 Windows 98 里面可以选择安装这个工具。
1 X' Y% Z+ O+ d6 n5 o4 p( j; y( T0 x3 n8 ~( ~! V4 X
--------------------------------------------------------------------------------
8 n  c" }; W" m) s0 _BCB 的 WMI 库文件 wbemuuid.lib 由本站提供, 包含在本页下面的程序源码下载里面1 j& \# @) l3 s6 @) S. w

! u$ ?8 @$ n" t4 ^. J1 |* q--------------------------------------------------------------------------------7 O' ^1 T; j' ^0 I- H  O
① 初始化 COM 接口:
7 E$ x# ~2 `+ x. Y- g; s( P   访问 WMI, 必须先初始化 COM 接口, 在程序的一开始调用 CoInitialize(NULL); 初始化, 在结束时调用 CoUninitialize(); 释放资源。
6 G- A1 ~8 S2 H) z) I  U' e   这两个函数在 #include <comdef.h> 里面定义。
% I1 X) Y* w  D1 w' t7 ~5 `7 E3 a. e" J2 e) e
② 获取访问 WMI 权限:
8 f9 E, a( N  S% w* Y- n3 T   CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0);+ C0 W2 _; F/ Q6 g+ [/ q! `4 h, L
  如果这个函数返回 S_OK 获取权限成功, 否则为失败。# C8 B  `% m) C$ P5 j

$ o2 U# d" `9 P" L9 N$ p③ 通过 IWbemLocator 和 IWbemServices 这两个 COM 接口访问 WMI, 获取系统信息:
" W; i- X, W2 z! o4 V( k   这个函数的参数: lpList 返回信息, wsClass 为要查找的系统信息类, 这些 COM 接口在 #include <wbemidl.h> 里定义。8 T! t( Q. o% g) x

1 L7 g5 A0 [3 G8 f5 m+ Y$ j( g9 Mvoid GetWmiInfo(TStrings *lpList, WideString wsClass)
: @/ a5 k9 ]; v- x* U{
/ a: G" E; U! c! q8 ^  IWbemLocator *pWbemLocator = NULL;
2 c  G& ?7 Q$ V! ]  if(CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pWbemLocator) == S_OK)  C. d% n- j, X. C
  {: R2 d* q: G, ]& d5 w
    IWbemServices *pWbemServices = NULL;
8 y  i, ^9 D* ]2 D/ B# o) u     WideString wsNamespace = (L"root\\cimv2");
3 x% R6 O) F- x- Q1 b     if(pWbemLocator->ConnectServer(wsNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) == S_OK)
' c4 O8 @4 U* N' v6 I      {7 M4 d( U+ h* b% ?0 C4 [# v
       IEnumWbemClassObject *pEnumClassObject = NULL;
- V! `$ p" P# a0 _3 ^+ l        WideString wsWQL=L"WQL", wsQuery=WideString(L"Select * from ")+wsClass;2 v6 L1 M" l) r/ k: a( k" B3 j9 x: W
       if(pWbemServices->ExecQuery(wsWQL, wsQuery, WBEM_FLAG_RETURN_IMMEDIATELY,NULL, &pEnumClassObject) == S_OK)" E: [: }8 B  o
        {
; M/ N( \2 P& d. R  h3 j5 y           IWbemClassObject *pClassObject = NULL;
% L$ m, i: V; g  P: y5 @6 Z           ULONG uCount = 1, uReturned;
7 f8 @1 z1 s. C8 @1 N+ o           if(pEnumClassObject->Reset() == S_OK)$ t, L) j3 a0 H: ?+ g3 y- ~* Z
           {& j9 D" t8 i: j% Q/ v7 Z3 R% Q
             int iEnumIdx = 0;" P5 O' B7 X8 s% D
             while(pEnumClassObject->Next(WBEM_INFINITE, uCount, &pClassObject, &uReturned) == S_OK)" j3 S0 n5 I* a" q. @
              {8 a4 z8 E1 c5 K0 g
                lpList->Add("---------------- ["+IntToStr(iEnumIdx)+"] -----------------");
# @% C/ M9 o7 S- L, Y5 L1 g5 w: ~# m5 u9 u! J* Y
                SAFEARRAY *pvNames = NULL;
% s1 w* w4 }  T! C  z, |                 if(pClassObject->GetNames(NULL, WBEM_FLAG_ALWAYS | WBEM_MASK_CONDITION_ORIGIN, NULL, &pvNames) == S_OK)
0 z& f9 K2 U3 X9 \& [0 ^) b: H                  {
- @  t. T0 D! H                    long vbl, vbu;% a, L; @% z: P6 u( d8 r4 ]( j# F
                   SafeArrayGetLBound(pvNames, 1, &vbl);
4 ^& j3 L$ |: J% T# j                    SafeArrayGetUBound(pvNames, 1, &vbu);
9 I: L" o0 U; c! Z                    for(long idx=vbl; idx<=vbu; idx++)$ o& l% Y% f, A; y0 k
                    {
, N& w8 S6 Q2 x* {                       long aidx = idx;
3 D/ V3 j" \4 p, L! E                       wchar_t *wsName = 0;
' M% [7 u- o$ L: i7 N                       VARIANT vValue;
8 u# f# W) V3 J! E                       VariantInit(&vValue);+ N6 v3 d' ?1 J
                      SafeArrayGetElement(pvNames, &aidx, &wsName);: I+ I" ^+ [6 S7 F

. i: e0 ~: C$ F6 F7 B                       BSTR bs = SysAllocString(wsName);% i# m1 X) {9 D$ |
                      HRESULT hRes = pClassObject->Get(bs, 0, &vValue, NULL, 0);
- A, M5 ~+ p' g! J  }( B& v                       SysFreeString(bs);
$ `. R. D* x$ ~+ V- d+ L  ~6 y) }6 @7 v. \: A+ `7 @0 r+ @
                      if(hRes == S_OK)
8 u. ?1 y4 y, n6 l) s1 d& k                        {) E( J( k& O' b5 |" A
                         AnsiString s;
- U! ]! x6 s2 x: @                          Variant v = *(Variant*)&vValue;
+ |: v7 Q! Z2 |- V# t3 ^" w                          if(v.IsArray())4 }* B  ]( e# X6 U
                          {
2 U# L' O( D: ]$ I6 V# }                             for(int i=v.ArrayLowBound(); i<=v.ArrayHighBound(); i++)
6 ?& L4 E& w; m                              {  N, v5 [8 F$ T+ }& C3 \4 q
                               Variant a = v.GetElement(i);0 \. Y7 O6 |2 e' \) x2 `5 n
                               if(!s.IsEmpty())
. Y2 S$ ]  y0 i* c, Y; K+ l                                  s+=", ";
2 E6 c" I3 F9 _1 M* @0 P. }                                s+=VarToStr(a);
6 b0 @1 X1 d- p3 L                              }
1 }5 u; m3 U1 v) {  H5 n                           }
/ H: v$ y8 W# S3 ^5 `2 Q% b& W                          else
. ?* C6 ~8 U" o: L                           {; d* `: c) L. ~
                            s = VarToStr(v);
5 D! I! n3 m0 s; A. U                           }# `$ w- B9 W0 |& ~3 \
                         lpList->Add(AnsiString(wsName)+"="+s);
: D6 S$ L( b5 U3 R- L2 Y                        }: K6 q  o& C% \9 v
: m" ^( Z( ~4 V" g0 R
                      VariantClear(&vValue);
7 H) _! ^& p2 L. ~1 e, G9 B. o                       SysFreeString(wsName);& l3 u" P# h/ ^2 r( S
                    }
' F# v, S, V* t+ i; K                  }0 v7 `: b4 R2 J+ [& Y
                if(pvNames)SafeArrayDestroy(pvNames);
( S0 r/ o# L, x, H1 Q) }- ~. ?6 q                 iEnumIdx++;. A  U: G: t0 C6 [
              }
3 D( ~( H& J7 W  Z1 `! C            }' s* {, X  c; f7 j8 n& x2 K
          if(pClassObject)pClassObject->Release();
* A& v) A' e' x& s- Q- y         }
$ }# g" g* M' W9 Y+ P        if(pEnumClassObject)pEnumClassObject->Release();
/ m7 a  u" {  I: e. L- W. C      }
1 \- w" _0 \4 U2 k     if(pWbemServices)pWbemServices->Release();# }. _7 W( \4 T7 N8 \
  }9 A: V: Q/ U  T6 ?- \6 \. o
  if(pWbemLocator)pWbemLocator->Release();4 ]" o% M; d" V/ O. D
}7 J$ S' L/ C/ r; i
//---------------------------------------------------------------------------
$ W# ~# Q8 \7 u1 C5 w: a% {8 k6 L6 X
// 通过 WIN32_bios 获取 BIOS 信息:; m6 h! o3 x5 `* Z9 q
void __fastcall TForm1::Button1Click(TObject *Sender)
, ?+ k0 E( [6 J$ P6 ]7 r4 Q{
! }  q1 \! F, E5 _    Memo1->Lines->Add("================== [WIN32_bios] =================");
; Z) ]% S4 m, q" ]    GetWmiInfo(Memo1->Lines, "WIN32_bios");
4 }4 @8 n* B, T3 I    Memo1->Lines->Add("");
. l6 I9 q$ H3 P3 i0 r}6 ], M5 W  \% Q3 Z% w& {$ O
, D9 i$ d4 D6 M& M0 n: ?" D
--------------------------------------------------------------------------------
, B5 N$ e8 P) b( j- b$ ^- M
+ O+ K6 A. N7 C9 {: RWMI 可以访问的信息类型有:
, z' j1 {+ ^( N; ?3 v   Win32_1394Controller4 r* o7 [4 e8 d% d! t- T
  Win32_BaseBoard# J" G0 Z" N; [5 K
  Win32_Battery
/ Y8 k1 F. R' C* v   Win32_BIOS
7 ~" L/ G/ A$ G+ T   Win32_Bus- q# V" L: n% v# h" ^- |
  Win32_CacheMemory
2 q$ J! m5 E$ q% h* Y   Win32_CDROMDrive  `3 r  Z- [: ^! P8 F
  Win32_CurrentProbe7 @3 [3 ~5 v$ {& y' U
  Win32_DesktopMonitor/ ~& p- ]7 L( ]7 O2 A6 }
  Win32_DeviceMemoryAddress. [5 [/ o" V& V4 O' |
  Win32_DiskDrive
! f$ A# |. _$ N( C5 {1 l( K   Win32_DisplayConfiguration6 k& a3 t, W8 q6 ?
  Win32_DisplayControllerConfiguration, U  i5 A! D! i( i( l- x
  Win32_DMAChannel: K- f+ Q1 }7 s  h
  Win32_Fan
3 a& y$ l) J! J8 e   Win32_FloppyController
% e3 k7 o6 L4 r, D* [, q( v4 ?   Win32_FloppyDrive
0 j# h7 z' C; X# R   Win32_HeatPipe
' r4 v  i. K! ?% U) Z- ^   Win32_IDEController# L+ a' y! i6 E3 E# F) n5 ^
  Win32_InfraredDevice
9 S: Q# u, b& J$ W& t   Win32_IRQResource+ _1 K7 \0 Z/ \# X  r  {# h
  Win32_Keyboard
. n, n5 \' Z; `# u: P   Win32_MemoryArray+ y; ]7 _) S2 w: Q/ g  s
  Win32_MemoryDevice
) a" F+ _8 f  P; ]7 g/ D9 m   Win32_MotherboardDevice# u* P' ?; _9 {& t; `
  Win32_NetworkAdapter
) F  S/ \7 [) H* I   Win32_NetworkAdapterConfiguration
1 b6 j3 \$ V' w1 j7 Y* v& i) F   Win32_OnBoardDevice
$ j# p! \& a5 D  y/ H9 b3 ^! h  [   Win32_ParallelPort' _. Q$ z6 m% P2 R( X9 L
  Win32_PCMCIAController
8 L* m' |7 Y# d   Win32_PhysicalMemory
: S4 q. @4 ?  u4 c   Win32_PhysicalMemoryArray8 l# }8 K' X3 `! w9 l0 H! s& T* W
  Win32_PnPEntity
9 {1 c( D1 W/ A7 S( s& D/ j" o   Win32_PointingDevice/ A4 _5 ~) K! T7 J
  Win32_PortableBattery: Q# a% m* d' `6 Z7 |) i
  Win32_PortConnector- p) T7 y  o* V. g* T1 M" ]
  Win32_PortResource
" {5 k/ y6 E& ~! x' H, W   Win32_POTSModem
+ ^/ u4 @; l! T3 E: y5 e" I" u' N   Win32_PowerManagementEvent
1 }- k# p- g9 a" j% g   Win32_Printer3 i. u6 B9 L+ c: f/ t- b" Y8 |& [1 g9 y* k
  Win32_PrinterConfiguration& p2 x% M8 N' P8 |0 ?
  Win32_PrintJob" V( C2 y7 C; Y7 \* c8 M: u
  Win32_Processor
; c9 b9 ~2 X* [0 U8 c   Win32_Refrigeration( ^- r& I8 i( o  T  a9 U- C! _
  Win32_SerialPort
9 G5 c% g$ a" f2 Z( X8 o   Win32_SerialPortConfiguration' j$ K9 X5 R( x0 n
  Win32_SMBIOSMemory
$ r- x! r6 q" T3 B+ U) `) E   Win32_SoundDevice  e8 m# d+ I+ L, M/ i( J
  Win32_SystemEnclosure
8 N7 H) j( Y8 L   Win32_SystemMemoryResource
  g! X; K% D/ n) v   Win32_SystemSlot  W9 T! P0 n( }& y1 @5 ?7 X0 z
  Win32_TapeDrive0 ]4 G3 [+ X0 ]4 Y4 q* ]
  Win32_TemperatureProbe
7 |( v2 y* M: m   Win32_UninterruptiblePowerSupply9 m( y9 z0 ]3 O7 Q
  Win32_USBController1 ~& i7 f2 i. ~" u
  Win32_VideoConfiguration( X% k/ {5 h# K& _0 r2 e7 o; a+ j' H
  Win32_VideoController
9 C8 M: a3 l7 ]) p+ S   Win32_VoltageProbe/ Y! Z; z0 ]0 T( I
% R7 i! S4 [$ T0 P' X) d+ d3 s4 ]
以上类型(字符串值)通过前面写的函数 GetWmiInfo 可以得到相应的信息, 例如 GetWmiInfo(Memo1->Lines, "WIN32_bios");
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-11-26 19:42 , Processed in 0.099138 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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