- #include "stdafx.h"
- using namespace std;
- #include <comdef.h>
- #include <comutil.h>
- #include <Wbemidl.h>
- #pragma comment ( lib, "wbemuuid.lib" )
- IWbemServices *pSvc = NULL;
- IWbemLocator *pLoc = NULL;
- IWbemClassObject* pclsObj = NULL;
- IEnumWbemClassObject* pEnum = NULL;
- HRESULT hres;
- int DiskNumber( const char* strSQL, const char* strAttr )
- {
- hres = pSvc->ExecQuery( bstr_t( "WQL" ), bstr_t( strSQL ), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum );
- if( FAILED( hres ) )
- {
- pSvc->Release();
- pLoc->Release();
- CoUninitialize();
- return 1;
- }
- ULONG uReturn = 0;
- while( pEnum )
- {
- HRESULT hr = pEnum->Next( WBEM_INFINITE, 1, &pclsObj, &uReturn );
- if( 0 == uReturn )
- {
- break;
- }
- VARIANT vtProp;
- VariantInit( &vtProp );
- vtProp.vt = VT_BSTR;
- vtProp.bstrVal = SysAllocString( L"" );
- hr = pclsObj->Get( bstr_t( strAttr ), 0, &vtProp, 0, 0 );
- if( FAILED( hr ) )
- {
- printf( "Error, HRESULT = %d, severity = %d, facility = %d, code = %d\n",
- hr, HRESULT_SEVERITY( hr ), HRESULT_FACILITY(hr), HRESULT_CODE(hr) );
- CComQIPtr < ISupportErrorInfo > spSEI = pclsObj;
- if( spSEI )
- {
- hr = spSEI->InterfaceSupportsErrorInfo( IID_ISupportErrorInfo );
- if( SUCCEEDED( hr ) )
- {
- CComQIPtr < IErrorInfo > spErrInfo;
- hr = GetErrorInfo( 0, &spErrInfo );
- if( SUCCEEDED( hr ) )
- {
- CComBSTR bstrDes;
- spErrInfo->GetDescription( &bstrDes );
- printf( "Com error = %s\n", _com_util::ConvertBSTRToString( (BSTR)bstrDes ) );
- }
- }
- }
- continue;
- }
- char* strNumber = _com_util::ConvertBSTRToString( vtProp.bstrVal );
- printf( "Disk information : %s, length = %d \n", strNumber, strlen( strNumber ) );
- delete [] strNumber;
- VariantClear( &vtProp );
- }
- return 0;
- }
- int main( int argc, char* argv[] )
- {
- hres = CoInitializeEx( 0, COINIT_MULTITHREADED );
- if( FAILED( hres ) )
- {
- return 1;
- }
- hres = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL );
- if( FAILED( hres ) )
- {
- CoUninitialize();
- return 1;
- }
- hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, ( LPVOID* )&pLoc );
- if( FAILED( hres ) )
- {
- CoUninitialize();
- return 1;
- }
- hres = pLoc->ConnectServer( _bstr_t( "ROOT\\CIMV2" ), NULL, NULL, 0, NULL, 0, 0, &pSvc );
- if( FAILED( hres ) )
- {
- pLoc->Release();
- CoUninitialize();
- return 1;
- }
- int ret = 0;
- ret |= DiskNumber( "SELECT * FROM Win32_DiskDrive", "PNPDeviceID" );
- ret |= DiskNumber( "SELECT * FROM Win32_PhysicalMedia", "SerialNumber" );
- if( ret )
- {
- printf( "Get Hard disk information failed\n" );
- return 1;
- }
- if( pSvc )
- {
- pSvc->Release();
- pSvc = NULL;
- }
- if( pLoc )
- {
- pLoc->Release();
- pLoc = NULL;
- }
- if( pEnum )
- {
- pEnum->Release();
- pEnum = NULL;
- }
- if( pclsObj )
- {
- pclsObj->Release();
- pclsObj = NULL;
- }
- CoUninitialize();
- return 0;
- }
阅读(13354) | 评论(0) | 转发(0) |