使用WMI获取正在运行的进程

  1. #define _WIN32_DCOM 
  2. #include "stdafx.h" 
  3. #include <iostream> 
  4. #include <comdef.h> 
  5. #include <Wbemidl.h> 
  6. #include <Wbemcli.h>
  7. #include <conio.h> 
  8. #pragma comment(lib, "wbemuuid.lib") 
  9. using namespace std; 
  10. const char * processName = "System";
  11. BOOL ManageWMI(); 
  12. int _tmain(int argc, _TCHAR* argv[]) 
  13.     if(ManageWMI()) printf("%WMI Error!"); 
  14.     _getch(); 
  15.     return 0; 
  16. }
  17. int FindProcess(IWbemServices *pSvc, const char * processName)
  18. {
  19.     HRESULT hres;
  20.     // Step 6: -------------------------------------------------- 
  21.     // Use the IWbemServices pointer to make requests of WMI ---- 
  22.     // For example, get the name of the operating system 
  23.     IEnumWbemClassObject* pEnumerator = NULL; 
  24.     hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_Process WHERE Description = 'System'"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 
  25.     if (FAILED(hres)) 
  26.     { 
  27.         cout << "Query for Process Configuration failed." 
  28.             << " Error code = 0x" 
  29.             << hex << hres << endl; 
  30.         pSvc->Release(); 
  31.         return 1; // Program has failed. 
  32.     } 
  33.     // Step 7: ------------------------------------------------- 
  34.     // Get the data from the query in step 6 ------------------- 
  35.     IWbemClassObject *pclsObj; 
  36.     ULONG uReturn = 0; 
  37.     int processId = 0;
  38.     while (pEnumerator) 
  39.     { 
  40.         HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 
  41.         if(0 == uReturn) 
  42.         { 
  43.             break
  44.         } 
  45.         VARIANT vtProp; 
  46.         VariantInit(&vtProp); 
  47.         hr = pclsObj->Get(L"ProcessId", 0, &vtProp, 0, 0); 
  48.         processId = vtProp.intVal;
  49.         VariantClear(&vtProp); 
  50.     } 
  51.     pEnumerator->Release(); 
  52.     pclsObj->Release(); 
  53.     return processId;
  54. }
  55. BOOL ManageWMI() 
  56.     HRESULT hres; 
  57.     // Step 1: -------------------------------------------------- 
  58.     // Initialize COM. ------------------------------------------ 
  59.     hres = CoInitializeEx(0, COINIT_MULTITHREADED); 
  60.     if (FAILED(hres)) 
  61.     { 
  62.         cout << "Failed to initialize COM library. Error code = 0x" 
  63.             << hex << hres << endl; 
  64.         return 1; // Program has failed. 
  65.     } 
  66.     // Step 2: -------------------------------------------------- 
  67.     // Set general COM security levels -------------------------- 
  68.     // Note: If you are using Windows 2000, you need to specify - 
  69.     // the default authentication credentials for a user by using 
  70.     // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ---- 
  71.     // parameter of CoInitializeSecurity ------------------------ 
  72.     hres = CoInitializeSecurity( 
  73.         NULL, 
  74.         -1, // COM authentication 
  75.         NULL, // Authentication services 
  76.         NULL, // Reserved 
  77.         RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication 
  78.         RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation 
  79.         NULL, // Authentication info 
  80.         EOAC_NONE, // Additional capabilities 
  81.         NULL // Reserved 
  82.         ); 
  83.     if (FAILED(hres)) 
  84.     { 
  85.         cout << "Failed to initialize security. Error code = 0x" 
  86.             << hex << hres << endl; 
  87.         CoUninitialize(); 
  88.         return 1; // Program has failed. 
  89.     } 
  90.     // Step 3: --------------------------------------------------- 
  91.     // Obtain the initial locator to WMI ------------------------- 
  92.     IWbemLocator *pLoc = NULL; 
  93.     hres = CoCreateInstance( 
  94.         CLSID_WbemLocator, 
  95.         0, 
  96.         CLSCTX_INPROC_SERVER, 
  97.         IID_IWbemLocator, (LPVOID *) &pLoc); 
  98.     if (FAILED(hres)) 
  99.     { 
  100.         cout << "Failed to create IWbemLocator object." 
  101.             << " Err code = 0x" 
  102.             << hex << hres << endl; 
  103.         CoUninitialize(); 
  104.         return 1; // Program has failed. 
  105.     } 
  106.     // Step 4: ----------------------------------------------------- 
  107.     // Connect to WMI through the IWbemLocator::ConnectServer method 
  108.     IWbemServices *pSvc = NULL; 
  109.     // Connect to the root/cimv2 namespace with 
  110.     // the current user and obtain pointer pSvc 
  111.     // to make IWbemServices calls. 
  112.     hres = pLoc->ConnectServer( 
  113.         _bstr_t(L"ROOT//CIMV2"), // Object path of WMI namespace 
  114.         NULL, // User name. NULL = current user 
  115.         NULL, // User password. NULL = current 
  116.         0, // Locale. NULL indicates current 
  117.         NULL, // Security flags. 
  118.         0, // Authority (e.g. Kerberos) 
  119.         0, // Context object 
  120.         &pSvc // pointer to IWbemServices proxy 
  121.         ); 
  122.     if (FAILED(hres)) 
  123.     { 
  124.         cout << "Could not connect. Error code = 0x" 
  125.             << hex << hres << endl; 
  126.         pLoc->Release(); 
  127.         CoUninitialize(); 
  128.         return 1; // Program has failed. 
  129.     } 
  130.     cout << "Connected to ROOT//CIMV2 WMI namespace" << endl; 
  131.     // Step 5: -------------------------------------------------- 
  132.     // Set security levels on the proxy ------------------------- 
  133.     hres = CoSetProxyBlanket( 
  134.         pSvc, // Indicates the proxy to set 
  135.         RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx 
  136.         RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx 
  137.         NULL, // Server principal name 
  138.         RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx 
  139.         RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 
  140.         NULL, // client identity 
  141.         EOAC_NONE // proxy capabilities 
  142.         ); 
  143.     if (FAILED(hres)) 
  144.     { 
  145.         cout << "Could not set proxy blanket. Error code = 0x" 
  146.             << hex << hres << endl; 
  147.         pSvc->Release(); 
  148.         pLoc->Release(); 
  149.         CoUninitialize(); 
  150.         return 1; // Program has failed. 
  151.     } 
  152.     int processId = FindProcess(pSvc, processName);
  153.     if(0 == processId)
  154.     {
  155.         pLoc->Release(); 
  156.         CoUninitialize(); 
  157.         cout << "process not found" << endl;
  158.         return 1;
  159.     }
  160.     // Cleanup 
  161.     // ======== 
  162.     pSvc->Release(); 
  163.     pLoc->Release(); 
  164.     CoUninitialize(); 
  165.     return 0; // Program successfully completed. 
  166. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值