c++ - Error 0x8004100e connecting to WMI namespace -
i'm using windows 7 , want develop program wmi.
i've read msdn , found example code.
#include "stdafx.h" #define _win32_dcom #include <iostream> using namespace std; #include <comdef.h> #include <wbemidl.h> #include <conio.h> # pragma comment(lib, "wbemuuid.lib") bool managewmi(); int main(int argc, char* argv[]) { if(managewmi()) printf("%wmi error!"); _getch(); return 0; } bool managewmi() { hresult hres; // step 1: -------------------------------------------------- // initialize com. ------------------------------------------ hres = coinitializeex(0, coinit_multithreaded); if (failed(hres)) { cout << "failed initialize com library. error code = 0x" << hex << hres << endl; return 1; // program has failed. } cout << "step 1" << endl; // step 2: -------------------------------------------------- // set general com security levels -------------------------- // note: if using windows 2000, need specify - // default authentication credentials user using // sole_authentication_list structure in pauthlist ---- // parameter of coinitializesecurity ------------------------ hres = coinitializesecurity( null, -1, // com authentication null, // authentication services null, // reserved rpc_c_authn_level_default, // default authentication rpc_c_imp_level_impersonate, // default impersonation null, // authentication info eoac_none, // additional capabilities null // reserved ); if (failed(hres)) { cout << "failed initialize security. error code = 0x" << hex << hres << endl; couninitialize(); return 1; // program has failed. } cout << "step 2 " << endl; // step 3: --------------------------------------------------- // obtain initial locator wmi ------------------------- iwbemlocator *ploc = null; hres = cocreateinstance( clsid_wbemlocator, 0, clsctx_inproc_server, iid_iwbemlocator, (lpvoid *) &ploc); if (failed(hres)) { cout << "failed create iwbemlocator object." << " err code = 0x" << hex << hres << endl; couninitialize(); return 1; // program has failed. } cout << "step 3" << endl; // step 4: ----------------------------------------------------- // connect wmi through iwbemlocator::connectserver method iwbemservices *psvc = null; // connect root/cimv2 namespace // current user , obtain pointer psvc // make iwbemservices calls. hres = ploc->connectserver( _bstr_t(l"root//cimv2"), // object path of wmi namespace null, // user name. null = current user null, // user password. null = current 0, // locale. null indicates current null, // security flags. 0, // authority (e.g. kerberos) 0, // context object &psvc // pointer iwbemservices proxy ); if (failed(hres)) { cout << "could not connect. error code = 0x" << hex << hres << endl; ploc->release(); cout << "released!!" << endl; couninitialize(); cout << "counitialized !!" << endl; return 1; // program has failed. } cout<< "step 4" << endl; cout << "connected root//cimv2 wmi namespace" << endl; // step 5: -------------------------------------------------- // set security levels on proxy ------------------------- hres = cosetproxyblanket( psvc, // indicates proxy set rpc_c_authn_winnt, // rpc_c_authn_xxx rpc_c_authz_none, // rpc_c_authz_xxx null, // server principal name rpc_c_authn_level_call, // rpc_c_authn_level_xxx rpc_c_imp_level_impersonate, // rpc_c_imp_level_xxx null, // client identity eoac_none // proxy capabilities ); cout << "step 5" << endl; if (failed(hres)) { cout << "could not set proxy blanket. error code = 0x" << hex << hres << endl; psvc->release(); ploc->release(); couninitialize(); return 1; // program has failed. } // step 6: -------------------------------------------------- // use iwbemservices pointer make requests of wmi ---- // example, name of operating system ienumwbemclassobject* penumerator = null; hres = psvc->execquery( bstr_t("wql"), bstr_t("select * win32_networkadapterconfiguration ipenabled = ''true''"), wbem_flag_forward_only | wbem_flag_return_immediately, null, &penumerator); if (failed(hres)) { cout << "query network adapter configuration failed." << " error code = 0x" << hex << hres << endl; psvc->release(); ploc->release(); couninitialize(); return 1; // program has failed. } // step 7: ------------------------------------------------- // data query in step 6 ------------------- iwbemclassobject *pclsobj; ulong ureturn = 0; while (penumerator) { hresult hr = penumerator->next(wbem_infinite, 1, &pclsobj, &ureturn); if(0 == ureturn) { break; } variant vtprop; variantinit(&vtprop); //hr = pclsobj->get(l"ipsubnet", 0, &vtprop, 0, 0); // value of enable property hr = pclsobj->get(l"ipenabled", 0, &vtprop, 0, 0); // value of macaddress property if(vtprop.boolval) { hr = pclsobj->get(l"macaddress", 0, &vtprop, 0, 0); wcout << " macaddress : " << vtprop.bstrval << endl; } hr = pclsobj->get(l"ipenabled", 0, &vtprop, 0, 0); if(vtprop.boolval) { long lstart, lend; long idx = -1; bstr* pbstr; safearray *sa; hr = pclsobj->get(l"description", 0, &vtprop, 0, 0); if(!failed(hr)) { wcout << "description: " << vtprop.bstrval << endl; } hr = pclsobj->get(l"dnshostname", 0, &vtprop, 0, 0); if(!failed(hr)) { cout << "dns:" << vtprop.bstrval << endl; } hr = pclsobj->get(l"ipaddress", 0, &vtprop, 0, 0); if(!failed(hr)) { safearray *psa = vtprop.parray; cout << "ip address::" << psa << endl; } } variantclear(&vtprop); } // cleanup // ======== psvc->release(); ploc->release(); penumerator->release(); pclsobj->release(); couninitialize(); return 0; // program completed. } however, these code doesn't work on computer.
it failed in "step4" error code 0x8004100e. googled , restart wmi service doesn't work either.
does had same experience , solved that? hope give me help. thanks...
you using wrong syntax root//cimv2 namespace try changing root\\cimv2
hres = ploc->connectserver( _bstr_t(l"root\\cimv2"), // object path of wmi namespace null, // user name. null = current user null, // user password. null = current 0, // locale. null indicates current null, // security flags. 0, // authority (e.g. kerberos) 0, // context object &psvc // pointer iwbemservices proxy ); btw 0x8004100e value code wbem_e_invalid_namespace (namespace specified cannot found.) error.
Comments
Post a Comment