You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. #ifndef TALK_BASE_WIN32TOOLHELP_H_
  3. #define TALK_BASE_WIN32TOOLHELP_H_
  4. #ifndef WIN32
  5. #error WIN32 Only
  6. #endif
  7. #include "talk/base/win32.h"
  8. // Should be included first, but that causes redefinitions.
  9. #include <tlhelp32.h>
  10. #include "talk/base/constructormagic.h"
  11. namespace talk_base {
  12. // The toolhelp api used to enumerate processes and their modules
  13. // on Windows is very repetetive and clunky to use. This little
  14. // template wraps it to make it a little more programmer friendly.
  15. //
  16. // Traits: Traits type that adapts the enumerator to the corresponding
  17. // win32 toolhelp api. Each traits class need to:
  18. // - define the type of the enumerated data as a public symbol Type
  19. //
  20. // - implement bool First(HANDLE, T*) normally calls a
  21. // Xxxx32First method in the toolhelp API. Ex Process32First(...)
  22. //
  23. // - implement bool Next(HANDLE, T*) normally calls a
  24. // Xxxx32Next method in the toolhelp API. Ex Process32Next(...)
  25. //
  26. // - implement bool CloseHandle(HANDLE)
  27. //
  28. template<typename Traits>
  29. class ToolhelpEnumeratorBase {
  30. public:
  31. ToolhelpEnumeratorBase(HANDLE snapshot)
  32. : snapshot_(snapshot), broken_(false), first_(true) {
  33. // Clear out the Traits::Type structure instance.
  34. Zero(&current_);
  35. }
  36. virtual ~ToolhelpEnumeratorBase() {
  37. Close();
  38. }
  39. // Moves forward to the next object using the First and Next
  40. // pointers. If either First or Next ever indicates an failure
  41. // all subsequent calls to this method will fail; the enumerator
  42. // object is considered broken.
  43. bool Next() {
  44. if (!Valid()) {
  45. return false;
  46. }
  47. // Move the iteration forward.
  48. current_.dwSize = sizeof(typename Traits::Type);
  49. bool incr_ok = false;
  50. if (first_) {
  51. incr_ok = Traits::First(snapshot_, &current_);
  52. first_ = false;
  53. } else {
  54. incr_ok = Traits::Next(snapshot_, &current_);
  55. }
  56. if (!incr_ok) {
  57. Zero(&current_);
  58. broken_ = true;
  59. }
  60. return incr_ok;
  61. }
  62. const typename Traits::Type& current() const {
  63. return current_;
  64. }
  65. void Close() {
  66. if (snapshot_ != INVALID_HANDLE_VALUE) {
  67. Traits::CloseHandle(snapshot_);
  68. snapshot_ = INVALID_HANDLE_VALUE;
  69. }
  70. }
  71. private:
  72. // Checks the state of the snapshot handle.
  73. bool Valid() {
  74. return snapshot_ != INVALID_HANDLE_VALUE && !broken_;
  75. }
  76. static void Zero(typename Traits::Type* buff) {
  77. ZeroMemory(buff, sizeof(typename Traits::Type));
  78. }
  79. HANDLE snapshot_;
  80. typename Traits::Type current_;
  81. bool broken_;
  82. bool first_;
  83. };
  84. class ToolhelpTraits {
  85. public:
  86. static HANDLE CreateSnapshot(uint32 flags, uint32 process_id) {
  87. return CreateToolhelp32Snapshot(flags, process_id);
  88. }
  89. static bool CloseHandle(HANDLE handle) {
  90. return ::CloseHandle(handle) == TRUE;
  91. }
  92. };
  93. class ToolhelpProcessTraits : public ToolhelpTraits {
  94. public:
  95. typedef PROCESSENTRY32 Type;
  96. static bool First(HANDLE handle, Type* t) {
  97. return ::Process32First(handle, t) == TRUE;
  98. }
  99. static bool Next(HANDLE handle, Type* t) {
  100. return ::Process32Next(handle, t) == TRUE;
  101. }
  102. };
  103. class ProcessEnumerator : public ToolhelpEnumeratorBase<ToolhelpProcessTraits> {
  104. public:
  105. ProcessEnumerator()
  106. : ToolhelpEnumeratorBase(
  107. ToolhelpProcessTraits::CreateSnapshot(TH32CS_SNAPPROCESS, 0)) {
  108. }
  109. private:
  110. DISALLOW_EVIL_CONSTRUCTORS(ProcessEnumerator);
  111. };
  112. class ToolhelpModuleTraits : public ToolhelpTraits {
  113. public:
  114. typedef MODULEENTRY32 Type;
  115. static bool First(HANDLE handle, Type* t) {
  116. return ::Module32First(handle, t) == TRUE;
  117. }
  118. static bool Next(HANDLE handle, Type* t) {
  119. return ::Module32Next(handle, t) == TRUE;
  120. }
  121. };
  122. class ModuleEnumerator : public ToolhelpEnumeratorBase<ToolhelpModuleTraits> {
  123. public:
  124. explicit ModuleEnumerator(uint32 process_id)
  125. : ToolhelpEnumeratorBase(
  126. ToolhelpModuleTraits::CreateSnapshot(TH32CS_SNAPMODULE,
  127. process_id)) {
  128. }
  129. private:
  130. DISALLOW_EVIL_CONSTRUCTORS(ModuleEnumerator);
  131. };
  132. } // namespace talk_base
  133. #endif // TALK_BASE_WIN32TOOLHELP_H_