#include #include #include #pragma comment(lib, "wbemuuid.lib") void Logging_LogSystem(char *, ...); void Support_HandleNonSystemError(char *, ...); void Support_HandleError(char *, ...); int SNMPQuery(char *, char *); int main() { SNMPQuery("10.1.1.1", "snmpTestString1"); return 0; } int SNMPQuery(char *szTargetIP, char *szCommunityString) { HRESULT hResult; IWbemLocator *pLocator = NULL; IWbemContext *pContext = NULL; IWbemServices *pService = NULL; IEnumWbemClassObject *pEnumerator = NULL; IWbemClassObject *pObject = NULL; BSTR bstrServer = NULL, bstrWQL = NULL, bstrQuery = NULL; VARIANT vValue; ULONG uResult = 0; wchar_t wszString[MAX_PATH]; int iRetVal = TRUE; hResult = CoInitializeEx(0, COINITBASE_MULTITHREADED); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "CoInitializeEx()", "hResult: %X", hResult); Logging_LogSystem("Unable to initialize COM subsystem.\r\n"); goto Cleanup; } hResult = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "CoInitializeSecurity()", "hResult: %X", hResult); Logging_LogSystem("Unable to initialize COM subsystem security.\r\n"); goto Cleanup; } hResult = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLocator); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "CoCreateInstance()", "hResult: %X", hResult); Logging_LogSystem("Unable to create locator instance.\r\n"); goto Cleanup; } hResult = CoCreateInstance(CLSID_WbemContext, 0, CLSCTX_INPROC_SERVER, IID_IWbemContext, (LPVOID *) &pContext); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "CoCreateInstance()", "hResult: %X", hResult); Logging_LogSystem("Unable to create context instance.\r\n"); goto Cleanup; } if (MultiByteToWideChar(CP_THREAD_ACP, 0, szTargetIP, -1, wszString, sizeof(wszString)) == 0) { iRetVal = FALSE; Support_HandleError(__FUNCTION__, "MultiByteToWideChar()", GetLastError()); Logging_LogSystem("Unable to convert Target IP to unicode.\r\n"); goto Cleanup; } VariantInit(&vValue); vValue.vt = VT_BSTR; vValue.bstrVal = SysAllocString(wszString); pContext->SetValue(L"AgentAddress", 0, &vValue); SysFreeString(vValue.bstrVal); VariantClear(&vValue); if (MultiByteToWideChar(CP_THREAD_ACP, 0, szCommunityString, -1, wszString, sizeof(wszString)) == 0) { iRetVal = FALSE; Support_HandleError(__FUNCTION__, "MultiByteToWideChar()", GetLastError()); Logging_LogSystem("Unable to convert Community String to unicode.\r\n"); goto Cleanup; } VariantInit(&vValue); vValue.vt = VT_BSTR; vValue.bstrVal = SysAllocString(wszString); pContext->SetValue(L"AgentReadCommunityName", 0, &vValue); SysFreeString(vValue.bstrVal); VariantClear(&vValue); VariantInit(&vValue); vValue.vt = VT_BSTR; vValue.bstrVal = SysAllocString(L"2C"); // Version 2C pContext->SetValue(L"AgentSNMPVersion", 0, &vValue); SysFreeString(vValue.bstrVal); VariantClear(&vValue); bstrServer = SysAllocString(L"root\\snmp\\localhost"); hResult = pLocator->ConnectServer(bstrServer, NULL, NULL, 0, NULL, 0, 0, &pService); SysFreeString(bstrServer); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "ConnectServer()", "hResult: %X", hResult); Logging_LogSystem("Unable to connect to local SNMP service.\r\n"); goto Cleanup; } hResult = CoSetProxyBlanket(pService, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "CoSetProxyBlanket()", "hResult: %X", hResult); Logging_LogSystem("Unable to configure proxy.\r\n"); goto Cleanup; } bstrWQL = SysAllocString(L"WQL"); bstrQuery = SysAllocString(L"SELECT * FROM SNMP_RFC1213_MIB_system"); hResult = pService->ExecQuery(bstrWQL, bstrQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, pContext, &pEnumerator); SysFreeString(bstrWQL); SysFreeString(bstrQuery); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "ExecQuery()", "hResult: %X", hResult); Logging_LogSystem("Unable to execute query.\r\n"); goto Cleanup; } while (pEnumerator) { hResult = pEnumerator->Next(WBEM_INFINITE, 1, &pObject, &uResult); if (uResult == 0) break; hResult = pObject->Get(L"sysDescr", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() sysDescr", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("sysDescr: %S\r\n", vValue.bstrVal); } hResult = pObject->Get(L"sysUptime", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() sysUptime", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("sysUptime: %d\r\n", vValue.iVal); } hResult = pObject->Get(L"sysName", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() sysName", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("sysName: %S\r\n", vValue.bstrVal); } pObject->Release(); } pEnumerator->Release(); pEnumerator = NULL; bstrWQL = SysAllocString(L"WQL"); bstrQuery = SysAllocString(L"SELECT * FROM SNMP_RFC1213_MIB_ipAddrTable"); hResult = pService->ExecQuery(bstrWQL, bstrQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, pContext, &pEnumerator); SysFreeString(bstrWQL); SysFreeString(bstrQuery); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "ExecQuery()", "hResult: %X", hResult); Logging_LogSystem("Unable to execute query.\r\n"); goto Cleanup; } while (pEnumerator) { hResult = pEnumerator->Next(WBEM_INFINITE, 1, &pObject, &uResult); if (uResult == 0) break; hResult = pObject->Get(L"ipAdEntIfIndex", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() ipAdEntIfIndex", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("ipAdEntIfIndex: %d\r\n", vValue.iVal); } hResult = pObject->Get(L"ipAdEntAddr", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() ipAdEntAddr", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("ipAdEntAddr: %d\r\n", vValue.bstrVal); } hResult = pObject->Get(L"ipAdEntNetMask", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() ipAdEntNetMask", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("ipAdEntNetMask: %S\r\n", vValue.bstrVal); } pObject->Release(); } pEnumerator->Release(); pEnumerator = NULL; bstrWQL = SysAllocString(L"WQL"); bstrQuery = SysAllocString(L"SELECT * FROM SNMP_RFC1213_MIB_ifTable"); hResult = pService->ExecQuery(bstrWQL, bstrQuery, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, pContext, &pEnumerator); SysFreeString(bstrWQL); SysFreeString(bstrQuery); if (FAILED(hResult)) { iRetVal = FALSE; Support_HandleNonSystemError(__FUNCTION__, "ExecQuery()", "hResult: %X", hResult); Logging_LogSystem("Unable to execute query.\r\n"); goto Cleanup; } while (pEnumerator) { hResult = pEnumerator->Next(WBEM_INFINITE, 1, &pObject, &uResult); if (uResult == 0) break; hResult = pObject->Get(L"ifIndex", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() ifIndex", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("ifIndex: %d\r\n", vValue.iVal); } hResult = pObject->Get(L"ifType", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() ifType", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("ifType: %S\r\n", vValue.ppdispVal); } hResult = pObject->Get(L"ifDescr", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() ifDescr", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("ifDescr: %S\r\n", vValue.bstrVal); } hResult = pObject->Get(L"ifPhysAddress", 0, &vValue, 0, 0); if (FAILED(hResult)) { Support_HandleNonSystemError(__FUNCTION__, "Get() ifPhysAddress", "hResult: %X", hResult); Logging_LogSystem("Unable to retrieve query result.\r\n"); } else { Logging_LogSystem("ifPhysAddress: %S\r\n", vValue.bstrVal); } pObject->Release(); } pEnumerator->Release(); pEnumerator = NULL; Cleanup: if (pEnumerator) pEnumerator->Release(); if (pService) pService->Release(); if (pContext) pContext->Release(); if (pLocator) pLocator->Release(); CoUninitialize(); return iRetVal; }