Chinaunix首页 | 论坛 | 博客
  • 博客访问: 142092
  • 博文数量: 10
  • 博客积分: 2431
  • 博客等级: 大尉
  • 技术积分: 266
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 00:56
文章分类
文章存档

2016年(1)

2013年(2)

2011年(3)

2010年(4)

分类: C/C++

2011-05-25 09:35:09

  1. #include "stdafx.h"
  2. using namespace std;
  3. #include <comdef.h>
  4. #include <comutil.h>
  5. #include <Wbemidl.h>
  6. #pragma comment ( lib, "wbemuuid.lib" )
  7. IWbemServices *pSvc = NULL;
  8. IWbemLocator *pLoc = NULL;
  9. IWbemClassObject* pclsObj = NULL;
  10. IEnumWbemClassObject* pEnum = NULL;
  11. HRESULT hres;
  12. int DiskNumber( const char* strSQL, const char* strAttr )
  13. {
  14.     hres = pSvc->ExecQuery( bstr_t( "WQL" ), bstr_t( strSQL ), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum );
  15.     if( FAILED( hres ) )
  16.     {
  17.         pSvc->Release();
  18.         pLoc->Release();
  19.         CoUninitialize();
  20.         return 1;
  21.     }
  22.     ULONG uReturn = 0;
  23.     while( pEnum )
  24.     {
  25.         HRESULT hr = pEnum->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn );
  26.         if( 0 == uReturn )
  27.         {
  28.             break;
  29.         }
  30.         VARIANT vtProp;
  31.         VariantInit( &vtProp );
  32.         vtProp.vt = VT_BSTR;
  33.         vtProp.bstrVal = SysAllocString( L"" );
  34.         hr = pclsObj->Get( bstr_t( strAttr ), 0, &vtProp, 0, 0 );

  35.         if( FAILED( hr ) )
  36.         {
  37.             printf( "Error, HRESULT = %d, severity = %d, facility = %d, code = %d\n",
  38.                 hr, HRESULT_SEVERITY( hr ), HRESULT_FACILITY(hr), HRESULT_CODE(hr) );
  39.             CComQIPtr < ISupportErrorInfo > spSEI = pclsObj;
  40.             if( spSEI )
  41.             {
  42.                 hr = spSEI->InterfaceSupportsErrorInfo( IID_ISupportErrorInfo );
  43.                 if( SUCCEEDED( hr ) )
  44.                 {
  45.                     CComQIPtr < IErrorInfo > spErrInfo;
  46.                     hr = GetErrorInfo( 0, &spErrInfo );
  47.                     if( SUCCEEDED( hr ) )
  48.                     {
  49.                         CComBSTR bstrDes;
  50.                         spErrInfo->GetDescription( &bstrDes );
  51.                         printf( "Com error = %s\n", _com_util::ConvertBSTRToString( (BSTR)bstrDes ) );
  52.                     }
  53.                 }
  54.             }
  55.             continue;
  56.         }
  57.         char* strNumber = _com_util::ConvertBSTRToString( vtProp.bstrVal );
  58.         printf( "Disk information : %s, length = %d \n", strNumber, strlen( strNumber ) );
  59.         delete [] strNumber;
  60.         VariantClear( &vtProp );
  61.     }
  62.     return 0;
  63. }
  64. int main( int argc, char* argv[] )
  65. {
  66.     hres = CoInitializeEx( 0, COINIT_MULTITHREADED );
  67.     if( FAILED( hres ) )
  68.     {
  69.         return 1;
  70.     }
  71.     hres = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
  72.     if( FAILED( hres ) )
  73.     {
  74.         CoUninitialize();
  75.         return 1;
  76.     }
  77.     hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, ( LPVOID* )&pLoc );
  78.     if( FAILED( hres ) )
  79.     {
  80.         CoUninitialize();
  81.         return 1;
  82.     }
  83.     hres = pLoc->ConnectServer( _bstr_t( "ROOT\\CIMV2" ), NULL, NULL, 0, NULL, 0, 0, &pSvc );
  84.     if( FAILED( hres ) )
  85.     {
  86.         pLoc->Release();
  87.         CoUninitialize();
  88.         return 1;
  89.     }
  90.     int ret = 0;
  91.     ret |= DiskNumber( "SELECT * FROM Win32_DiskDrive", "PNPDeviceID" );
  92.     ret |= DiskNumber( "SELECT * FROM Win32_PhysicalMedia", "SerialNumber" );
  93.     if( ret )
  94.     {
  95.         printf( "Get Hard disk information failed\n" );
  96.         return 1;
  97.     }
  98.     if( pSvc )
  99.     {
  100.         pSvc->Release();
  101.         pSvc = NULL;
  102.     }
  103.     if( pLoc )
  104.     {
  105.         pLoc->Release();
  106.         pLoc = NULL;
  107.     }
  108.     if( pEnum )
  109.     {
  110.         pEnum->Release();
  111.         pEnum = NULL;
  112.     }
  113.     if( pclsObj )
  114.     {
  115.         pclsObj->Release();
  116.         pclsObj = NULL;
  117.     }
  118.     CoUninitialize();
  119.     return 0;
  120. }
阅读(13354) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~