[Joe]" < XXXX@XXXXX.COM > wrote in message
news:057a01c3b740$dcdbfb20$ XXXX@XXXXX.COM ...
I found a sample via Google:
http://groups.google.com/groups?q=IWbemServices+%22C%2B%2B%22&hl=en&lr=lang_en&ie=UTF-8&oe=UTF-8&selm=e0veO%23%24BCHA.2696%40cpmsftngxa08&rnum=3
I used it to develop the following example for IRQs (you can read the docs
to do I/O. Note that this is just an extract of the button handler that
populates a listbox:
IWbemServices* ConnectToWMINamespace(BSTR bstrNamespace)
{
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED); // Initialize COM.
if (FAILED(hres))
{
AfxMessageBox(_T("Failed to initialize COM library."));
return NULL; // Program has failed.
}
hres = CoInitializeSecurity( NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_CONNECT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0);
if (FAILED(hres))
{
AfxMessageBox(_T("Failed to initialize security."));
CoUninitialize();
return NULL; // Program has failed.
}
IWbemLocator *pLoc = 0;
hres = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
AfxMessageBox(_T("Failed to create IWbemLocator object."));
CoUninitialize();
return NULL; // Program has failed.
}
IWbemServices *pSvc = 0;
// Connect to the root\default namespace with the current user.
hres = pLoc->ConnectServer( bstrNamespace,
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc);
if (FAILED(hres))
{
AfxMessageBox(_T("Could not connect."));
CoUninitialize();
return NULL; // Program has failed.
}
// Set the proxy so that impersonation of the client occurs.
// if you allow impersonation in the CoInitializeSecurity call
// then you could omit this step.
hres = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE);
if (FAILED(hres))
{
AfxMessageBox(_T("Could not set proxy blanket."));
pSvc->Release();
pLoc->Release();
CoUninitialize();
return NULL; // Program has failed.
}
// Cleanup
// ========
pLoc->Release();
return pSvc; // Program successfully completed.
}
void CWMIIRQDlg::OnBnClickedGo()
{
HRESULT hr;
IEnumWbemClassObject *pEnum = NULL;
IWbemClassObject *pObj = NULL;
CListBox *irqList = (CListBox *)GetDlgItem(IDC_IRQS);
_bstr_t bstrNamespace(L"\\\\.\\root\\cimv2");
_bstr_t bstrWQL(L"WQL");
_bstr_t bstrQuery;
CString line;
ULONG count;
/* Lets connect to the cimv2 namespace in WMI */
IWbemServices* pWMIServices = ConnectToWMINamespace(bstrNamespace);
if (pWMIServices == NULL)
{
AfxMessageBox(_T("Failed to connect to WMI"));
return;
}
bstrQuery = L"SELECT * FROM Win32_IRQResource";
hr = pWMIServices->ExecQuery(bstrWQL, bstrQuery,
WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
if (FAILED(hr))
{
AfxMessageBox(_T("Failed to execute WMI query"));
pWMIServices->Release();
return;
}
// Enumerate the r