Добрый день, коллеги!
Помогите плиз с ошибкой.
Место ошибки помочено.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51.
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp1;
VARIANT vtProp2;
VARIANT vtProp3;
VARIANT vtProp4;
VARIANT vtProp5;
char ProcessId;
char ProcessStr;
BSTR MethodName = SysAllocString(L"Create");
BSTR ClassName = SysAllocString(L"Win32_Process");
hr = pclsObj->Get(L"Name", 0, &vtProp1, 0, 0);
wcout << " Name " << vtProp1.bstrVal << endl;
VariantClear(&vtProp1);
hr = pclsObj->Get(L"ProcessId", 0, &vtProp2, 0, 0);
wcout << " ProcessId " << vtProp2.uintVal << endl;
//ProcessId.Format( "%u", vtProp2.bstrVal);
VariantClear(&vtProp2);
hr = pclsObj->Get(L"Caption", 0, &vtProp3, 0, 0);
wcout << " Caption " << vtProp3.bstrVal << endl;
VariantClear(&vtProp3);
hr = pclsObj->Get(L"WorkingSetSize", 0, &vtProp4, 0, 0);
wcout << " WorkingSetSize " << vtProp4.uintVal << endl;
IWbemClassObject* pClass;
IWbemClassObject* pwcrGetOwnerIn = NULL;
IWbemClassObject* pwcrGetOwnerOut = NULL;
IWbemClassObject* pOutParams=0;
hr = pSvc->GetObject(_bstr_t("Win32_Process"), 0, NULL, &pClass, NULL); // - Get Service Object
hr = pClass->GetMethod(_bstr_t("GetOwner"), 0, &pwcrGetOwnerIn, &pwcrGetOwnerOut); // - Get Service Method
WCHAR path[] = L"Win32_Process.Handle=1234";
//!!!!!!!!!Здесь ошибка!!!!!!
hr = pClass->ExecMethod(_bstr_t(path), _bstr_t(L"GetOwner"), 0, NULL, NULL, &pOutParams, NULL);
pclsObj->Release();
pclsObj = NULL;
}
Текст ошибки:
1. 2. 3. 4. 5. 6. 7.
1>------ Построение начато: проект: 2, Конфигурация: Debug Win32 ------
1> 3.cpp
1>c:\documents and settings\admin\мои документы\visual studio 2010\projects\2\2\3.cpp(2): warning C4005: UNICODE: изменение макроопределения
1> аргументы командной строки: см. предыдущее определение "UNICODE"
1>c:\documents and settings\admin\мои документы\visual studio 2010\projects\2\2\3.cpp(235): error C2039: ExecMethod: не является членом "IWbemClassObject"
1> c:\program files\microsoft sdks\windows\v7.0a\include\wbemcli.h(818): см. объявление "IWbemClassObject"
========== Построение: успешно: 0, с ошибками: 1, без изменений: 0, пропущено: 0 ==========
Описание класса
http://msdn.microsoft.com/en-us/library/aa394372(v=vs.85).aspx
Официальный пример другого метода этого класса:
http://technet.microsoft.com/ru-ru/sysinternals/aa390421
Примеры:
https://github.com/CS-svnmirror/farmanager/blob/master/plugins/proclist/wmi.cpp
http://www.unknowncheats.me/forum/c-and-c/66752-wmi-get-active-processes.html
Правильно сделать знаний не хватает.
Полный текст программы
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256.
#define _WIN32_DCOM
#define UNICODE
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
# pragma comment(lib, "credui.lib")
# pragma comment(lib, "comsuppw.lib")
#include <wincred.h>
#include <strsafe.h>
#include <math.h>
#include <cmath>
HRESULT hres;
IWbemLocator *pLoc;
IWbemServices *pSvc;
int __cdecl main(int argc, char **argv)
{
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IDENTIFY, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"\\\\178.207.59.214\\root\\cimv2"),
NULL, // User name
NULL, // User password
NULL, // Locale
NULL, // Security flags
NULL, // Authority
NULL, // Context object
&pSvc // IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x" << hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Set security levels on a WMI connection ------------------
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
COLE_DEFAULT_PRINCIPAL, // Server principal name
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT Name, ProcessId, Caption, VirtualSize from Win32_Process"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for operating system name failed." << " Error code = 0x" << hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 8: -------------------------------------------------
// Secure the enumerator proxy
hres = CoSetProxyBlanket(
pEnumerator, // Indicates the proxy to set
RPC_C_AUTHN_DEFAULT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_DEFAULT, // RPC_C_AUTHZ_xxx
COLE_DEFAULT_PRINCIPAL, // Server principal name
RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket on enumerator. Error code = 0x" << hex << hres << endl;
pEnumerator->Release();
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 9: -------------------------------------------------
// Get the data from the query in step 7 -------------------
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp1;
VARIANT vtProp2;
VARIANT vtProp3;
VARIANT vtProp4;
VARIANT vtProp5;
char ProcessId;
char ProcessStr;
BSTR MethodName = SysAllocString(L"Create");
BSTR ClassName = SysAllocString(L"Win32_Process");
hr = pclsObj->Get(L"Name", 0, &vtProp1, 0, 0);
wcout << " Name " << vtProp1.bstrVal << endl;
VariantClear(&vtProp1);
hr = pclsObj->Get(L"ProcessId", 0, &vtProp2, 0, 0);
wcout << " ProcessId " << vtProp2.uintVal << endl;
//ProcessId.Format( "%u", vtProp2.bstrVal);
VariantClear(&vtProp2);
hr = pclsObj->Get(L"Caption", 0, &vtProp3, 0, 0);
wcout << " Caption " << vtProp3.bstrVal << endl;
VariantClear(&vtProp3);
hr = pclsObj->Get(L"WorkingSetSize", 0, &vtProp4, 0, 0);
wcout << " WorkingSetSize " << vtProp4.uintVal << endl;
//long memUsage = _wtol(V_BSTR(&vtProp4));
//wcout << ", MemUsage: " << (memUsage/1024) << " K" << endl;
//wcout << " WorkingSetSize " << (_wtoi64(vtProp4.bstrVal)/1024)/1024 << endl;
/*VariantClear(&vtProp4);
BSTR MethodName = SysAllocString(L"GetOwner");
BSTR ClassName = SysAllocString(L"Win32_Process");
IWbemClassObject* pInParamsDefinition = NULL;
IWbemClassObject* pOutParamsDefinition = NULL;
hr = pclsObj->GetMethod(MethodName, 0, &pInParamsDefinition, &pOutParamsDefinition);
IWbemClassObject* pClassInstance = NULL;
//hr = pInParamsDefinition->SpawnInstance(0, &pClassInstance);
// Execute Method
IWbemClassObject* pOutParams = NULL;
hr = pSvc->ExecMethod(ClassName, MethodName, 0, NULL, NULL, &pOutParams, NULL);
wcout << " VirtualSize " << pOutParams << endl;*/
//VariantClear(&vtProp5);
IWbemClassObject* pClass;
IWbemClassObject* pwcrGetOwnerIn = NULL;
IWbemClassObject* pwcrGetOwnerOut = NULL;
IWbemClassObject* pOutParams=0;
hr = pSvc->GetObject(_bstr_t("Win32_Process"), 0, NULL, &pClass, NULL); // - Get Service Object
hr = pClass->GetMethod(_bstr_t("GetOwner"), 0, &pwcrGetOwnerIn, &pwcrGetOwnerOut); // - Get Service Method
//ProcessStr.Format( "Win32_Process.Handle=%s", ProcessId); // - Prepare Method Parameters
//WMIHandle = service->ExecMethod(_bstr_t(ProcessStr), _bstr_t(L"GetOwner"), 0, NULL, pwcrGetOwnerIn, &pOutParams, NULL);
WCHAR path[] = L"Win32_Process.Handle=1234";
hr = pClass->ExecMethod(_bstr_t(path), _bstr_t(L"GetOwner"), 0, NULL, NULL, &pOutParams, NULL);
//_variant_t user;
//hr = pOutParams->Get( L"User", 0, &user, NULL, 0 );
//_variant_t domain;
//hr = pOutParams->Get( L"Domain", 0, &domain, NULL, 0 );
pclsObj->Release();
pclsObj = NULL;
}
// Cleanup
pEnumerator->Release();
if( pclsObj )
{
pclsObj->Release();
}
Sleep(100000);
return 0; // Program successfully completed.
}
|