Hello I tried to convert the following from VBS to C++. I keep getting error "Failed to Initialize COM. Error code = 0x80010106" on my CoInitializeEx(NULL, COINIT_MULTITHREADED) call. What do I do wrong? Thanks in advance for any help. Code: strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from SoftwareLicensingProduct") For Each objItem in colItems If NOT objItem.ProductKeyID = "null" Then Wscript.Echo "Extend PID: " & objItem.ProductKeyID Wscript.Echo "Windows Family: " & objItem.LicenseFamily Wscript.Echo "Windows Type: " & objItem.ProductKeyChannel Wscript.Echo "Activation ID: " & objItem.ID Exit For End If Next My function in C++: Code: private: std::string GetActIDFromSystem() { HRESULT hres; hres = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (FAILED(hres)) { //std::string format_error(unsigned __int32 hres); std::stringstream ss; ss << "Failed to Initialize COM. Error code = 0x" << std::hex << hres << std::endl; return ss.str(); } hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); if (FAILED(hres)) { CoUninitialize(); //std::string format_error(unsigned __int32 hres); std::stringstream ss; ss << "Failed to initialize security. Error code = 0x" << std::hex << hres << std::endl; return ss.str(); } IWbemLocator *pLoc = NULL; hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc); if (FAILED(hres)) { CoUninitialize(); //std::string format_error(unsigned __int32 hres); std::stringstream ss; ss << "Failed to create IWbemLocator object. Error code = 0x" << std::hex << hres << std::endl; return ss.str(); } IWbemServices *pSvc = NULL; // Connect to the root\cimv2 namespace with // the current user and obtain pointer pSvc // to make IWbemServices calls. hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc); if (FAILED(hres)) { pLoc->Release(); CoUninitialize(); //std::string format_error(unsigned __int32 hres); std::stringstream ss; ss << "Could not connect. Error code = 0x" << std::hex << hres << std::endl; return ss.str(); } //cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl; 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)) { pSvc->Release(); pLoc->Release(); CoUninitialize(); //std::string format_error(unsigned __int32 hres); std::stringstream ss; ss << "Could not set proxy blanket. Error code = 0x" << std::hex << hres << std::endl; return ss.str(); } // For example, get the name of the operating system IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM SoftwareLicensingProduct"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); CoUninitialize(); //std::string format_error(unsigned __int32 hres); std::stringstream ss; ss << "Query for SoftwareLicensingProduct failed. Error code = 0x" << std::hex << hres << std::endl; return ss.str(); } IWbemClassObject *pclsObj = NULL; ULONG uReturn = 0; while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if (0 == uReturn) { break; } VARIANT vtProp; // Get the value of the Name property hr = pclsObj->Get(L"ProductKeyID", 0, &vtProp, 0, 0); std::stringstream ss; ss << vtProp.bstrVal; VariantClear(&vtProp); pclsObj->Release(); } // Cleanup // ======== pSvc->Release(); pLoc->Release(); pEnumerator->Release(); CoUninitialize(); return "0"; }
Take a look here. https://docs.microsoft.com/en-us/windows/desktop/api/combaseapi/nf-combaseapi-coinitializeex You're getting RPC_E_CHANGED_MODE as a return code. This means that CoInitalizeEx() has already been initialized with "COINIT_APARTMENTTHREADED" Try this hres = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); I hope this helps.
Thank you for your answer. I tried that, but did get an error on CoInitializeSecurity. I disabled that part of code and then it worked.
@Jachra: I'm glad I was able to help. Please describe the error that you're getting, and We'll try to figure out what's needed to fix it.