modCheck.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "modCheck.h"
  2. #include <set>
  3. #include <string>
  4. #include <list>
  5. #include <winpr/synch.h>
  6. #include <winpr/crt.h>
  7. #include <winpr/thread.h>
  8. using namespace std;
  9. class modCheckLock
  10. {
  11. public:
  12. modCheckLock() {
  13. static bool isInit = false;
  14. if (!isInit)
  15. {
  16. InitializeCriticalSection(&cs_);
  17. isInit = true;
  18. }
  19. EnterCriticalSection(&cs_);
  20. }
  21. ~modCheckLock() {
  22. LeaveCriticalSection(&cs_);
  23. }
  24. private:
  25. modCheckLock(const modCheckLock&);
  26. modCheckLock& operator=(const modCheckLock&);
  27. private:
  28. static CRITICAL_SECTION cs_;
  29. };
  30. CRITICAL_SECTION modCheckLock::cs_;
  31. void InitResource(toolkitResource &m_resource)
  32. {
  33. ZeroMemory(&m_resource, sizeof(toolkitResource));
  34. m_resource.g_mgr = 0;
  35. m_resource.g_initialized = 0;
  36. m_resource.hprintf_index = 0;
  37. ZeroMemory(m_resource.hprintf_buffer, sizeof(m_resource.hprintf_buffer));
  38. ZeroMemory(m_resource.szCrashID, sizeof(m_resource.szCrashID));
  39. }
  40. class modInfo
  41. {
  42. public:
  43. modInfo(string name)
  44. :m_name(name)
  45. {
  46. InitResource(m_resource);
  47. }
  48. bool operator == (const string name){
  49. return name == this->m_name;
  50. }
  51. void AddModThread(DWORD threadId){
  52. m_threadArr.insert(threadId);
  53. }
  54. bool findModThread(DWORD threadId){
  55. return m_threadArr.end() != m_threadArr.find(threadId);
  56. }
  57. toolkitResource *getResource()
  58. {
  59. return &m_resource;
  60. }
  61. string m_name;
  62. friend BOOL toolkit_setAssign(void *assign);
  63. friend BOOL toolkit_setThreadGroupByAssign(void *assign);
  64. set<DWORD> m_threadArr;
  65. private:
  66. toolkitResource m_resource;
  67. void *m_assign;
  68. };
  69. list<modInfo*> g_modInfoArr;
  70. modInfo* findModInfo(string name)
  71. {
  72. for (list<modInfo*>::iterator i = g_modInfoArr.begin(); i != g_modInfoArr.end(); i++)
  73. {
  74. if (**i == name)
  75. return *i;
  76. }
  77. return NULL;
  78. }
  79. modInfo *findModInfoByThreadId()
  80. {
  81. DWORD threadId = GetCurrentThreadId();
  82. for (list<modInfo*>::iterator i = g_modInfoArr.begin(); i != g_modInfoArr.end(); i++)
  83. {
  84. if ((*i)->findModThread(threadId))
  85. return *i;
  86. }
  87. return NULL;
  88. }
  89. TOOLKIT_API BOOL toolkit_checkThreadInName(DWORD threadId, const char *name)
  90. {
  91. if (NULL == name)
  92. return false;
  93. modCheckLock curLock;
  94. for (list<modInfo*>::iterator i = g_modInfoArr.begin(); i != g_modInfoArr.end(); i++)
  95. {
  96. //TODO: should separate it or it would iterate all mod.
  97. if (string(name) == (*i)->m_name && (*i)->findModThread(threadId))
  98. return true;
  99. }
  100. return false;
  101. }
  102. TOOLKIT_API BOOL toolkit_CreateModuleInfo(const char *name)
  103. {
  104. if (NULL == name)
  105. return false;
  106. if (NULL != findModInfo(name))
  107. toolkit_DestoryModuleInfo(name);
  108. modCheckLock curLock;
  109. modInfo *curModInfo = new modInfo(name);
  110. g_modInfoArr.push_back(curModInfo);
  111. return true;
  112. }
  113. TOOLKIT_API BOOL toolkit_DestoryModuleInfo(const char *name)
  114. {
  115. if (NULL == name)
  116. return false;
  117. modCheckLock curLock;
  118. for (list<modInfo*>::iterator i = g_modInfoArr.begin(); i != g_modInfoArr.end(); i++)
  119. {
  120. if (**i == name)
  121. {
  122. delete *i;
  123. g_modInfoArr.erase(i);
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. TOOLKIT_API BOOL toolkit_SetthreadGroup(DWORD threadId, const char *name)
  130. {
  131. if (NULL == name)
  132. return false;
  133. modCheckLock curLock;
  134. modInfo *curModInfo = findModInfo(name);
  135. if (NULL == curModInfo)
  136. return false;
  137. curModInfo->AddModThread(threadId);
  138. return true;
  139. }
  140. TOOLKIT_API BOOL toolkit_TerminateThreadExcept(DWORD exceptThreadId, const char *name)
  141. {
  142. if (NULL == name)
  143. return false;
  144. modCheckLock curLock;
  145. modInfo *curModInfo = findModInfo(name);
  146. if (NULL == curModInfo)
  147. return false;
  148. for (set<DWORD>::iterator i = curModInfo->m_threadArr.begin(); i != curModInfo->m_threadArr.end(); i++)
  149. {
  150. if (*i != exceptThreadId)
  151. {
  152. HANDLE currThread = OpenThread(PROCESS_ALL_ACCESS, FALSE, *i);
  153. if (currThread != NULL) //
  154. {
  155. TerminateThread(currThread, -1);
  156. }
  157. }
  158. }
  159. return true;
  160. }
  161. toolkitResource* toolkit_getResource()
  162. {
  163. static toolkitResource *spShellResource = NULL;
  164. modCheckLock curLock;
  165. modInfo *curMod = findModInfoByThreadId();
  166. if (NULL != curMod)
  167. return curMod->getResource();
  168. else if ((NULL != spShellResource))
  169. return spShellResource;
  170. else
  171. {
  172. spShellResource = new toolkitResource();
  173. InitResource(*spShellResource);
  174. return spShellResource;
  175. }
  176. return NULL;
  177. }
  178. BOOL toolkit_setAssign(void *assign)
  179. {
  180. if (NULL == assign)
  181. return FALSE;
  182. modCheckLock curLock;
  183. modInfo *curMod = findModInfoByThreadId();
  184. if (NULL != curMod)
  185. {
  186. curMod->m_assign = assign;
  187. return TRUE;
  188. }
  189. return FALSE;
  190. }
  191. BOOL toolkit_setThreadGroupByAssign(void *assign)
  192. {
  193. if (NULL == assign)
  194. return FALSE;
  195. modCheckLock curLock;
  196. for (list<modInfo*>::iterator i = g_modInfoArr.begin(); i != g_modInfoArr.end(); i++)
  197. {
  198. if ((*i)->m_assign == assign)
  199. {
  200. (*i)->AddModThread(GetCurrentThreadId());
  201. return TRUE;
  202. }
  203. }
  204. return FALSE;
  205. }