我正在从 Windows 7 机器连接到 Windows server 2012 和 2008 R2。
我的代码在某些计算机上工作,但在某些计算机上,当我运行我的应用程序查询时,总是返回 NULL。
当我对 IEnumWbemClassObject 对象执行 Next 方法时,它返回错误代码 x80041003。
我正在尝试查询 Win32_Process 类
我的代码:
hres = CoInitializeEx( 0, COINIT_MULTITHREADED );
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_CONNECT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
0 // Reserved
);
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
hres = pLoc->ConnectServer( ServerName,Username, Password, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pSvc);
COAUTHIDENTITY AuthId;
memset(&AuthId, 0, sizeof(COAUTHIDENTITY));
//Initialized AuthId
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_DEFAULT, // 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
&AuthId, // client identity
EOAC_NONE
);
Query.Format( _T( "SELECT * FROM Win32_Process WHERE Name like '%s.exe'" ), App.Name );
IEnumWbemClassObject* pEnumerator = NULL;
hr = App.Server.pSvc->ExecQuery(
L"WQL",
Query.AllocSysString(),
WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);