Using WMI to monitor process creation (VC version) under Ring3

Source: Internet
Author: User

[CPP]View PlainCopy
  1. #include "stdafx.h"
  2. #define _win32_dcom
  3. #include <iostream>
  4. Using namespace std;
  5. #include <comdef.h>
  6. #include <Wbemidl.h>
  7. # pragma comment (lib, "Wbemuuid.lib")
  8. int main (int argc, char **argv)
  9. {
  10. HRESULT hres;
  11. hres = CoInitializeEx (0, coinit_multithreaded);
  12. if (FAILED (hres))
  13. {
  14. cout << "Failed to initialize COM library."
  15. << "Error code = 0x"
  16. << hex << hres << Endl;
  17. return 1;
  18. }
  19. IWbemLocator *ploc = 0;
  20. HRESULT HR;
  21. hr = CoCreateInstance (clsid_wbemlocator, 0,
  22. Clsctx_inproc_server, Iid_iwbemlocator, (lpvoid *) &ploc);
  23. if (FAILED (HR))
  24. {
  25. cout << "Failed to create IWbemLocator object. ERR code = 0x "
  26. << hex << hr << Endl;
  27. return HR;  //program has failed.
  28. }
  29. IWbemServices *psvc = 0;
  30. bstr_t Strnetworkresource ("root\\cimv2");
  31. hr = Ploc->connectserver (
  32. Strnetworkresource,
  33. NULL, NULL, 0, NULL, 0, 0, &psvc);
  34. if (FAILED (HR))
  35. {
  36. cout << "Could not connect. Error code = 0x "
  37. << hex << hr << Endl;
  38. Ploc->release ();
  39. CoUninitialize ();
  40. return HR;  //program has failed.
  41. }
  42. cout << "Connected to WMI" << Endl;
  43. //Set the proxy so that impersonation of the client occurs.
  44. hr = CoSetProxyBlanket (PSVC,
  45. Rpc_c_authn_winnt,
  46. Rpc_c_authz_none,
  47. Null
  48. Rpc_c_authn_level_call,
  49. Rpc_c_imp_level_impersonate,
  50. Null
  51. Eoac_none
  52. );
  53. if (FAILED (HR))
  54. {
  55. cout << "Could not set proxy blanket. Error code = 0x "
  56. << hex << hr << Endl;
  57. Psvc->release ();
  58. Ploc->release ();
  59. CoUninitialize ();
  60. return HR;
  61. }
  62. bstr_t Strlang ("WQL");
  63. //monitor Taskmgr.exe process creation
  64. bstr_t strquery ("SELECT * from __InstanceCreationEvent within 1 WHERE targetinstance ISA ' Win32_Process ' and targetins Tance.   Name = ' Taskmgr.exe ');
  65. ienumwbemclassobject* PResult = NULL;
  66. hr = Psvc->execnotificationquery (Strlang, strquery, Wbem_flag_forward_only | wbem_flag_return_immediately, NULL, &presult);
  67. if (SUCCEEDED (HR))
  68. {
  69. do{
  70. iwbemclassobject* pobject = NULL;
  71. ULONG lcnt = 0;
  72. hr = Presult->next (wbem_infinite, 1, &pobject, &lcnt);
  73. if (SUCCEEDED (HR) && pobject)
  74. {
  75. cout<<"Taskmgr.exe process created" <<endl;
  76. Break ; //Exit
  77. }
  78. }while (true);
  79. }
  80. Psvc->release ();
  81. Ploc->release ();
  82. CoUninitialize ();
  83. CoUninitialize ();
  84. return 0;  //program successfully completed.
  85. }

http://blog.csdn.net/zwfgdlc/article/details/6613605

Using WMI to monitor process creation (VC version) under Ring3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.