void ListClasses(IWbemServices *pNamespace) { HRESULT hr; IEnumWbemClassObject *pEnum = NULL; hr = pNamespace->CreateClassEnum(NULL, 0, NULL, &pEnum); if(SUCCEEDED(hr)) { // Note that even though security was set on the namespace pointer, it must // also be set on this pointer since COM will revert back to the default // settings for any new pointers! hr = SetProxySecurity(pEnum); if(SUCCEEDED(hr)) { IWbemClassObject* Array[1]; Array[0] = NULL; ULONG uRet = 0; while (SUCCEEDED(hr = pEnum->Next(10000, 1, Array, &uRet)) && Array[0]) { // Note that IWbemClassObjects are inproc and thus have no proxy. // Therefore, they never need CoSetProxyBlanket. BSTR strText; IWbemClassObject* pObj = Array[0]; hr = pObj->GetObjectText(0, &strText); if(SUCCEEDED(hr) && strText) { printf("\nGot class %S", strText); SysFreeString(strText); } pObj->Release(); Array[0] = NULL; } } else printf("\nFAILED TO SET SECURITY FOR ENUMERATOR!!!!! hr = 0x%x", hr); pEnum->Release(); } }
//*************************************************************************** // Function: PrintError // Purpose: Formats and prints the error message //*************************************************************************** void PrintError(char *pszFailureReason, SCODE psc, DWORD dwMode) { VARIANT varString; SCODE sc; IWbemClassObject *pErrorObject = NULL; IErrorInfo* pEI = NULL; fprintf(stdout, "%s\n", pszFailureReason); fprintf(stdout, "FunctionReturn: %S(0x%08lx)\n", WbemErrorString(psc), psc); if (GetErrorInfo(0, &pEI) == S_OK) { pEI->QueryInterface(IID_IWbemClassObject, (void**)&pErrorObject); pEI->Release(); } if (pErrorObject != NULL) { VariantInit(&varString); if (dwMode == ERROR_MODE_PRINTFIELDS) { if (pErrorObject->InheritsFrom(L"__NotifyStatus") != WBEM_NO_ERROR) { fprintf(stdout, "Unrecognized Error Object type\n"); } else if (pErrorObject->InheritsFrom(L"__ExtendedStatus") == WBEM_NO_ERROR) { sc = pErrorObject->Get(L"Description", 0L, &varString, NULL, NULL); if (sc != S_OK) { fprintf(stdout, "Can't get Description: %d\n", sc); } else if (V_VT(&varString) == VT_BSTR) { FWPRINTF(stdout, L"Description: %wS\n", V_BSTR(&varString)); } VariantClear(&varString); pErrorObject->Get(L"Operation", 0L, &varString, NULL, NULL); if (sc != S_OK) { fprintf(stdout, "Can't get Operation: %d\n", sc); } else if (V_VT(&varString) == VT_BSTR) { FWPRINTF(stdout, L"Operation: %wS\n", V_BSTR(&varString)); } VariantClear(&varString); pErrorObject->Get(L"ParameterInfo", 0L, &varString, NULL, NULL); if (sc != S_OK) { fprintf(stdout, "Can't get ParameterInfo: %d\n", sc); } else if (V_VT(&varString) == VT_BSTR) { FWPRINTF(stdout, L"ParameterInfo: %wS\n", V_BSTR(&varString)); } VariantClear(&varString); pErrorObject->Get(L"ProviderName", 0L, &varString, NULL, NULL); if (sc != S_OK) { fprintf(stdout, "Can't get ProviderName: %d\n", sc); } else if (V_VT(&varString) == VT_BSTR) { FWPRINTF(stdout, L"ProviderName: %wS\n", V_BSTR(&varString)); } VariantClear(&varString); } } else { BSTR bstrObjectText = NULL; if (SUCCEEDED(pErrorObject->GetObjectText(0, &bstrObjectText))) { fprintf(stdout, "%wS", bstrObjectText); SysFreeString(bstrObjectText); } } RELEASE(pErrorObject); } }
int main(int iArgCnt, char ** argv) { IWbemLocator *pLocator = NULL; IWbemServices *pNamespace = 0; IWbemClassObject * pClass = NULL; IWbemClassObject * pOutInst = NULL; IWbemClassObject * pInClass = NULL; IWbemClassObject * pInInst = NULL; BSTR Text = NULL; HRESULT hr = S_OK; BSTR path = SysAllocString(L"root\\default"); BSTR ClassPath = SysAllocString(L"MethProvSamp"); BSTR MethodName = SysAllocString(L"Echo"); BSTR ArgName = SysAllocString(L"sInArg"); if (!path || ! ClassPath || !MethodName || ! ArgName) { printf("SysAllocString failed. Out of memory.\n"); goto cleanup; } // Initialize COM and connect up to CIMOM hr = CoInitialize(0); if (FAILED(hr)) { printf("CoInitialize returned 0x%x:", hr); goto cleanup; } // NOTE: // When using asynchronous WMI API's remotely in an environment where the "Local System" account // has no network identity (such as non-Kerberos domains), the authentication level of // RPC_C_AUTHN_LEVEL_NONE is needed. However, lowering the authentication level to // RPC_C_AUTHN_LEVEL_NONE makes your application less secure. It is wise to // use semi-synchronous API's for accessing WMI data and events instead of the asynchronous ones. hr = CoInitializeSecurity ( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_SECURE_REFS, //change to EOAC_NONE if you change dwAuthnLevel to RPC_C_AUTHN_LEVEL_NONE NULL ); if (FAILED(hr)) { printf("CoInitializeSecurity returned 0x%x:", hr); goto cleanup; } hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLocator); if (FAILED(hr)) { printf("CoCreateInstance returned 0x%x:", hr); goto cleanup; } hr = pLocator->ConnectServer(path, NULL, NULL, NULL, 0, NULL, NULL, &pNamespace); printf("\n\nConnectServer returned 0x%x:", hr); if(hr != WBEM_S_NO_ERROR) goto cleanup; // Get the class object hr = pNamespace->GetObject(ClassPath, 0, NULL, &pClass, NULL); printf("\nGetObject returned 0x%x:", hr); if(hr != WBEM_S_NO_ERROR) goto cleanup; // Get the input argument and set the property hr = pClass->GetMethod(MethodName, 0, &pInClass, NULL); printf("\nGetMethod returned 0x%x:", hr); if(hr != WBEM_S_NO_ERROR) goto cleanup; hr = pInClass->SpawnInstance(0, &pInInst); printf("\nSpawnInstance returned 0x%x:", hr); if(hr != WBEM_S_NO_ERROR) goto cleanup; VARIANT var; var.vt = VT_BSTR; var.bstrVal= SysAllocString(L"hello"); if (var.bstrVal == NULL) goto cleanup; hr = pInInst->Put(ArgName, 0, &var, 0); VariantClear(&var); // Call the method hr = pNamespace->ExecMethod(ClassPath, MethodName, 0, NULL, pInInst, &pOutInst, NULL); printf("\nExecMethod returned 0x%x:", hr); if(hr != WBEM_S_NO_ERROR) goto cleanup; // Display the results. hr = pOutInst->GetObjectText(0, &Text); if(hr != WBEM_S_NO_ERROR) goto cleanup; printf("\n\nThe object text of the output object is:\n%S", Text); printf("Terminating normally\n"); // Free up resources cleanup: SysFreeString(path); SysFreeString(ClassPath); SysFreeString(MethodName); SysFreeString(ArgName); SysFreeString(Text); if (pClass) pClass->Release(); if (pInInst) pInInst->Release(); if (pInClass) pInClass->Release(); if (pOutInst) pOutInst->Release(); if (pLocator) pLocator->Release(); if (pNamespace) pNamespace->Release(); CoUninitialize(); return 0; }