标签:server 上下文 omd 客户 效果 addition 错误代码 imp reserve
1 #define _WIN32_DCOM 2 #include <iostream> 3 #include <comdef.h> 4 #include <Wbemidl.h> 5 6 using namespace std; 7 8 #pragma comment(lib, "wbemuuid.lib") 9 10 int main(int argc, char **argv) 11 { 12 HRESULT hres; 13 14 // 第一步:初始化COM 15 hres = CoInitializeEx(0, COINIT_MULTITHREADED); 16 if (FAILED(hres)) 17 { 18 cout << "Failed to initialize COM library. Error code = 0x" 19 << hex << hres << endl; 20 return 1; // Program has failed. 21 } 22 23 // 第二步:设置COM安全级别 24 hres = CoInitializeSecurity( 25 NULL, 26 -1, // COM 认证 27 NULL, // 服务认证 28 NULL, // 保留NULL 29 RPC_C_AUTHN_LEVEL_DEFAULT, // 默认权限 30 RPC_C_IMP_LEVEL_IMPERSONATE, // 默认模拟 31 NULL, // 认证信息 32 EOAC_NONE, // Additional capabilities 33 NULL // Reserved 34 ); 35 36 37 if (FAILED(hres)) 38 { 39 cout << "安全级别初始化失败,错误代码 = 0x" << hex << hres << endl; 40 CoUninitialize(); 41 return 1; // Program has failed. 42 } 43 44 // 第三步:获取初始化本地WMI 45 IWbemLocator *pLoc = NULL; 46 47 hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc); 48 49 if (FAILED(hres)) 50 { 51 cout << "创建IWbemLocator对象失败,错误代码 = 0x" << hex << hres << endl; 52 CoUninitialize(); 53 return 1; // Program has failed. 54 } 55 56 // 第四步:通过 IWbemLocator::ConnectServer 方法连接WMI 57 IWbemServices *pSvc = NULL; 58 59 // 使用IWbemServices 连接 root\cimv2 命名空间 60 hres = pLoc->ConnectServer( 61 _bstr_t(L"ROOT\\CIMV2"), // 对象路径 62 NULL, // 用户名为空默认当前用户 63 NULL, // 用户密码为空默认当前密码 64 0, // 本地,NULL表示当前 65 NULL, // 安全标志 66 0, // 授权人 67 0, // 上下文对象 68 &pSvc // IWbemServices代理指针 69 ); 70 71 if (FAILED(hres)) 72 { 73 cout << "无法连接,错误代码 = 0x" << hex << hres << endl; 74 pLoc->Release(); 75 CoUninitialize(); 76 return 1; // Program has failed. 77 } 78 79 cout << "已连接到 ROOT\\CIMV2 WMI 命名空间" << endl; 80 81 82 // 第五步:设置代理安全级别 83 84 hres = CoSetProxyBlanket( 85 pSvc, // 要设置的代理指针 86 RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx 87 RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx 88 NULL, // 委托服务名 89 RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx 90 RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 91 NULL, // 客户端身份 92 EOAC_NONE // 代理能力 93 ); 94 95 if (FAILED(hres)) 96 { 97 cout << "代理设置失败,错误代码 = 0x" << hex << hres << endl; 98 pSvc->Release(); 99 pLoc->Release(); 100 CoUninitialize(); 101 return 1; // Program has failed. 102 } 103 104 // 第六步:使用 IWbemServices 指针获取系统名 105 IEnumWbemClassObject* pEnumerator = NULL; 106 hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_OperatingSystem"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 107 108 if (FAILED(hres)) 109 { 110 cout << "查询系统名失败,错误代码 = 0x" << hex << hres << endl; 111 pSvc->Release(); 112 pLoc->Release(); 113 CoUninitialize(); 114 return 1; // Program has failed. 115 } 116 117 // 第七步:获取查询数据 118 IWbemClassObject *pclsObj = NULL; 119 ULONG uReturn = 0; 120 121 while (pEnumerator) 122 { 123 HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,&pclsObj, &uReturn); 124 125 if (0 == uReturn) 126 { 127 break; 128 } 129 130 VARIANT vtProp; 131 132 // 获取Name属性值 133 hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0); 134 wcout << " 系统名 : " << vtProp.bstrVal << endl; 135 VariantClear(&vtProp); 136 137 pclsObj->Release(); 138 } 139 140 // 清理工作 141 pSvc->Release(); 142 pLoc->Release(); 143 pEnumerator->Release(); 144 CoUninitialize(); 145 system("pause"); 146 return 0; // Program successfully completed. 147 148 }
说明都在源码注释里,效果图:
标签:server 上下文 omd 客户 效果 addition 错误代码 imp reserve
原文地址:https://www.cnblogs.com/biaoge140/p/10779763.html