CoInitialize(NULL); if(CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) != S_OK) { qDebug() << "Error: CoInitialize"; return 1;} IWbemLocator * pIWbemLocator = NULL; IWbemServices * pWbemServices = NULL; IEnumWbemClassObject * pEnumObject = NULL; BSTR bstrNamespace = (L"root\\cimv2"); if(CoCreateInstance (CLSID_WbemAdministrativeLocator, NULL , CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , IID_IUnknown ,( void ** ) & pIWbemLocator ) != S_OK) {return 1;} if(pIWbemLocator->ConnectServer(bstrNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices) != S_OK) { qDebug() << "Error: ConnectServer"; return 1;} HRESULT hRes; BSTR strQuery = (L"Select * from win32_diskdrive"); BSTR strQL = (L"WQL"); hRes = pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); IWbemClassObject * pClassObject = NULL; VARIANT v; if(hRes != S_OK) { qDebug() <<"Could not execute Query"; return 1; } else { ULONG uCount = 1, uReturned; hRes = pEnumObject->Reset(); if(hRes != S_OK) { qDebug() <<"Could not Enumerate"; return 1; } while (pEnumObject) { hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); if(uReturned==0) { qDebug() << "End"; return 1 ; } BSTR strClassProp = SysAllocString(L"Size"); hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0); if(hRes != S_OK) { qDebug() << "Could not Get Value"; return 1; } SysFreeString(strClassProp); _bstr_t bstrPath = &v; //Just to convert BSTR to ANSI char* strPath=(char*)bstrPath; if (SUCCEEDED(hRes)) { qDebug() << strPath << endl; } else qDebug() << "Error in getting object"; } } VariantClear( &v ); pIWbemLocator->Release(); pWbemServices->Release(); pEnumObject->Release(); pClassObject->Release(); CoUninitialize(); return 0;
C++ (Qt)#ifndef INFOHARD_H#define INFOHARD_H #include <QObject>#include <QString>#include <QMainWindow>#include <QMessageBox>#include <QDebug>#include <iostream> #ifdef Q_OS_WIN32 #pragma once #define _WIN32_DCOM #include <wbemidl.h> #include <comdef.h> #include <conio.h> #pragma comment(lib, "wbemuuid.lib") #endifusing namespace std; class InfoHard : public QObject{ Q_OBJECTpublic: explicit InfoHard(QObject *parent = 0); signals: public slots: int sl_GetInfo(); };
C++ (Qt)#include "infohard.h" InfoHard::InfoHard(QObject *parent) : QObject(parent){ }int InfoHard::sl_GetInfo(){ CoInitialize(NULL); if(CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) != S_OK) { qDebug() << "Error: CoInitialize"; return 1;} IWbemLocator * pIWbemLocator = NULL; IWbemServices * pWbemServices = NULL; IEnumWbemClassObject * pEnumObject = NULL; BSTR bstrNamespace = (L"root\\cimv2"); if(CoCreateInstance (CLSID_WbemAdministrativeLocator, NULL , CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER , IID_IUnknown ,( void ** ) & pIWbemLocator ) != S_OK) {return 1;} if(pIWbemLocator->ConnectServer( bstrNamespace, NULL, NULL, NULL, 0, NULL, NULL, &pWbemServices ) != S_OK) { qDebug() << "Error: ConnectServer"; return 1;} HRESULT hRes; BSTR strQuery = (L"Select * from win32_diskdrive"); BSTR strQL = (L"WQL"); hRes = pWbemServices->ExecQuery(strQL, strQuery,WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumObject); IWbemClassObject * pClassObject = NULL; VARIANT v; if(hRes != S_OK) { qDebug() <<"Could not execute Query"; return 1; } else { ULONG uCount = 1, uReturned; hRes = pEnumObject->Reset(); if(hRes != S_OK) { qDebug() <<"Could not Enumerate"; return 1; } while (pEnumObject) { hRes = pEnumObject->Next(WBEM_INFINITE,uCount, &pClassObject, &uReturned); if(uReturned==0) { qDebug() << "End"; return 1 ; } BSTR strClassProp = SysAllocString(L"Size"); hRes = pClassObject->Get(strClassProp, 0, &v, 0, 0); if(hRes != S_OK) { qDebug() << "Could not Get Value"; return 1; } SysFreeString(strClassProp); _bstr_t bstrPath = &v; //Just to convert BSTR to ANSI char* strPath=(char*)bstrPath; if (SUCCEEDED(hRes)) { qDebug() << strPath << endl; } else qDebug() << "Error in getting object"; } } VariantClear( &v ); pIWbemLocator->Release(); pWbemServices->Release(); pEnumObject->Release(); pClassObject->Release(); CoUninitialize(); return 0;}
C++ (Qt) inf = new InfoHard; connect(ui->cmdSaveSettings, SIGNAL(clicked()), inf, SLOT(sl_GetInfo()));