CWxObject.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "stdafx.h"
  2. #include "CWxObject.h"
  3. #include <math.h>
  4. #define WX_MAIN_WND_NAME _T("微信")
  5. #define WX_MAIN_WND_CLASS_NAME _T("WeChatMainWndForPC")
  6. #define WX_LOGIN_WND_NAME _T("登录")
  7. #define WX_LOGIN_WND_CLASS_NAME _T("WeChatLoginWndForPC")
  8. BOOL ASCII2UNICODE(IN LPCCH lpASCIIStr, OUT PWCH pUNICODEStr, IN CONST INT& nUNICODEStrLen)
  9. {
  10. if (lpASCIIStr == NULL)
  11. return FALSE;
  12. // 获取宽字符字节数;
  13. int cchWideChar = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, NULL, 0);
  14. if (cchWideChar == 0 || cchWideChar >= nUNICODEStrLen)
  15. return FALSE;
  16. // 转换成宽字符串;
  17. memset(pUNICODEStr, 0, sizeof(WCHAR)*nUNICODEStrLen);
  18. int nWriteNum = MultiByteToWideChar(CP_ACP, 0, lpASCIIStr, -1, pUNICODEStr, cchWideChar);
  19. if (nWriteNum != cchWideChar)
  20. return FALSE;
  21. return TRUE;
  22. }
  23. CWxObject::CWxObject()
  24. :m_dwWxProcId(0)
  25. , m_hWxProcess(NULL)
  26. , m_hWxMainWnd(NULL)
  27. , m_hWxLoginWnd(NULL)
  28. , m_lpInjectData(NULL)
  29. , m_lpEjectData(NULL)
  30. , m_hInjectThread(NULL)
  31. , m_hEjectThread(NULL)
  32. , m_dwPathLen(0)
  33. , m_bAttached(FALSE)
  34. {
  35. }
  36. CWxObject::CWxObject(DWORD dwProcId, LPCTSTR lpDynamicLibraryPath)
  37. :m_dwWxProcId(dwProcId)
  38. , m_hWxProcess(NULL)
  39. , m_hWxMainWnd(NULL)
  40. , m_hWxLoginWnd(NULL)
  41. , m_lpInjectData(NULL)
  42. , m_lpEjectData(NULL)
  43. , m_hInjectThread(NULL)
  44. , m_hEjectThread(NULL)
  45. , m_dwPathLen(0)
  46. , m_bAttached(FALSE)
  47. {
  48. setInjectionObj(dwProcId, lpDynamicLibraryPath);
  49. }
  50. CWxObject::~CWxObject()
  51. {
  52. // 卸载dll;
  53. EjectDynamicLibrary();
  54. // 释放所有资源;
  55. if (m_hInjectThread)
  56. CloseHandle(m_hInjectThread);
  57. m_hInjectThread = NULL;
  58. if (m_hEjectThread)
  59. CloseHandle(m_hEjectThread);
  60. m_hEjectThread = NULL;
  61. if (m_lpInjectData)
  62. VirtualFreeEx(m_hWxProcess, m_lpInjectData, m_dwPathLen, MEM_RELEASE);
  63. m_lpInjectData = NULL;
  64. if (m_lpEjectData)
  65. VirtualFreeEx(m_hWxProcess, m_lpEjectData, m_dwPathLen, MEM_RELEASE);
  66. m_lpEjectData = NULL;
  67. if (m_hWxProcess)
  68. CloseHandle(m_hWxProcess);
  69. m_hWxProcess = NULL;
  70. }
  71. void CWxObject::setInjectionObj(DWORD dwProcId, LPCTSTR lpDynamicLibraryPath)
  72. {
  73. ASSERT(dwProcId != 0);
  74. ASSERT(lpDynamicLibraryPath != NULL);
  75. m_dwWxProcId = dwProcId;
  76. memset(m_szDllPath, 0, sizeof(m_szDllPath));
  77. memset(m_wszDllPath, 0, sizeof(m_wszDllPath));
  78. #ifdef UNICODE
  79. _tcscpy_s(m_szDllPath, lpDynamicLibraryPath);
  80. #else
  81. _tcscpy_s(m_szDllPath, lpDynamicLibraryPath);
  82. ASCII2UNICODE(lpDynamicLibraryPath, m_wszDllPath, MAX_PATH);
  83. #endif
  84. //m_hWxProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, m_dwWxProcId);
  85. m_hWxProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, m_dwWxProcId);
  86. if (m_hWxProcess == NULL)
  87. {
  88. WriteTextLog(_T("打开WeChat.exe进程失败"));
  89. }
  90. }
  91. BOOL CWxObject::InjectDynamicLibrary()
  92. {
  93. ASSERT(m_hWxProcess != NULL);
  94. m_dwPathLen = wcslen(m_wszDllPath) * sizeof(WCHAR) + 1;
  95. m_lpInjectData = VirtualAllocEx(m_hWxProcess, NULL, m_dwPathLen, MEM_COMMIT, PAGE_READWRITE);
  96. if (NULL == m_lpInjectData)
  97. {
  98. WriteTextLog(_T("创建WeChat.exe进程虚拟内存失败"));
  99. return FALSE;
  100. }
  101. if (WriteProcessMemory(m_hWxProcess, m_lpInjectData, m_wszDllPath, m_dwPathLen, NULL) == 0)
  102. {
  103. // 注意:MEM_RELEASE释放时第三参数一定要为0,请查看MSDN;
  104. VirtualFreeEx(m_hWxProcess, m_lpInjectData, 0, MEM_RELEASE);
  105. return FALSE;
  106. }
  107. HMODULE hk32 = GetModuleHandle(_T("kernel32.dll"));
  108. // 注意:微信使用的是W版本;
  109. LPVOID lpAddr = GetProcAddress(hk32, "LoadLibraryW");
  110. m_hInjectThread = CreateRemoteThread(m_hWxProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpAddr, m_lpInjectData, 0, NULL);
  111. if (NULL == m_hInjectThread)
  112. {
  113. // 注意:MEM_RELEASE释放时第三参数一定要为0,请查看MSDN;
  114. VirtualFreeEx(m_hWxProcess, m_lpInjectData, 0, MEM_RELEASE);
  115. return FALSE;
  116. }
  117. WaitForSingleObject(m_hInjectThread, INFINITE);
  118. if (m_hInjectThread)
  119. CloseHandle(m_hInjectThread);
  120. m_hInjectThread = NULL;
  121. /* 注入成功后,不能释放内存否则微信会挂;
  122. if (m_lpInjectData != NULL)
  123. VirtualFreeEx(m_hWxProcess, m_lpInjectData, 0, MEM_RELEASE);
  124. */
  125. return TRUE;
  126. }
  127. BOOL CWxObject::EjectDynamicLibrary()
  128. {
  129. if (m_hWxProcess == NULL)
  130. return TRUE;
  131. // 获取模块句柄;
  132. HANDLE hModule = FindModuleEx(m_szDllPath, m_dwWxProcId);
  133. if (hModule == NULL)
  134. {
  135. WriteTextLog(_T("获取WeChat.exe进程模块hook.dll失败"));
  136. return FALSE;
  137. }
  138. LPVOID lpAddr = GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "FreeLibraryAndExitThread");//FreeLibraryAndExitThread//FreeLibrary
  139. if (lpAddr == NULL)
  140. {
  141. WriteTextLog(_T("获取kernel32.dll中的FreeLibraryAndExitThread失败"));
  142. return FALSE;
  143. }
  144. m_hEjectThread = CreateRemoteThread(m_hWxProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpAddr, hModule, 0, NULL);
  145. if (m_hEjectThread == NULL)
  146. {
  147. WriteTextLog(_T("创建WeChat.exe远程线程(FreeLibraryAndExitThread)失败"));
  148. return FALSE;
  149. }
  150. WaitForSingleObject(m_hEjectThread, INFINITE);
  151. if (m_hEjectThread)
  152. CloseHandle(m_hEjectThread);
  153. m_hEjectThread = NULL;
  154. return TRUE;
  155. }
  156. BOOL CWxObject::FindWxMainWnd()
  157. {
  158. WNDINFO wnd;
  159. wnd.hWxWnd = NULL;
  160. wnd.dwWxProcId = m_dwWxProcId;
  161. _stprintf_s(wnd.szWndName, WX_MAIN_WND_NAME);
  162. _stprintf_s(wnd.szClassName, WX_MAIN_WND_CLASS_NAME);
  163. if (::EnumWindows(&EnumWindowsProc, (LPARAM)&wnd) == FALSE)
  164. {
  165. m_hWxMainWnd = wnd.hWxWnd;
  166. m_rcWxWnd = wnd.rcWnd;
  167. return TRUE;
  168. }
  169. return FALSE;
  170. }
  171. BOOL CWxObject::FindWxLoginWnd()
  172. {
  173. WNDINFO wnd;
  174. wnd.hWxWnd = NULL;
  175. wnd.dwWxProcId = m_dwWxProcId;
  176. _stprintf_s(wnd.szWndName, WX_LOGIN_WND_NAME);
  177. _stprintf_s(wnd.szClassName, WX_LOGIN_WND_CLASS_NAME);
  178. if (::EnumWindows(&EnumWindowsProc, (LPARAM)&wnd) == FALSE)
  179. {
  180. m_hWxLoginWnd = wnd.hWxWnd;
  181. m_rcWxWnd = wnd.rcWnd;
  182. return TRUE;
  183. }
  184. return FALSE;
  185. }
  186. BOOL CWxObject::Attach2MainWnd(CWnd *pMainWnd, BOOL bLoginWnd )
  187. {
  188. HWND hWxWnd = bLoginWnd ? m_hWxLoginWnd : m_hWxMainWnd;
  189. if (hWxWnd != NULL)
  190. {
  191. // 获取微信窗口的样式;
  192. DWORD dwStyle = ::GetWindowLong(hWxWnd, GWL_STYLE);
  193. // WS_CLIPSIBLINGS告诉父窗口不要绘制子窗口出现的区域;
  194. dwStyle |= WS_CLIPSIBLINGS;
  195. // 如果窗口隐藏的,显示出来;
  196. dwStyle |= WS_VISIBLE;
  197. // 重新设置窗口样式 ;
  198. ::SetWindowLong(hWxWnd, GWL_STYLE, dwStyle);
  199. CRect rect;
  200. pMainWnd->GetWindowRect(&rect);
  201. // 设置父窗口;
  202. ::SetParent(hWxWnd, pMainWnd->m_hWnd);//set parent of ms paint to our dialog.
  203. // 擦除背景;
  204. //SetWindowLong(hWxWnd, GWL_STYLE, WS_VISIBLE);//eraze title of ms paint window.
  205. //Positioning ms paint.
  206. // 将屏幕坐标转在窗口坐标;
  207. pMainWnd->ScreenToClient(&rect);
  208. // 居中显示;
  209. CRect rcCenter = rect;
  210. if (m_rcWxWnd.Width() >= rect.Width())
  211. {
  212. rcCenter.left = 0;
  213. rcCenter.right = rect.right;
  214. }
  215. else
  216. {
  217. rcCenter.left = (rect.Width() - m_rcWxWnd.Width()) / 2;
  218. rcCenter.right = rcCenter.left + m_rcWxWnd.Width();
  219. }
  220. if (m_rcWxWnd.Height() >= rect.Height())
  221. {
  222. rcCenter.top = 0;
  223. rcCenter.bottom = rect.bottom;
  224. }
  225. else
  226. {
  227. rcCenter.top = (rect.Height() - m_rcWxWnd.Height()) / 2;
  228. rcCenter.bottom = rcCenter.bottom + m_rcWxWnd.Height();
  229. }
  230. // 注意:MoveWindow/SetWindowPos使用的是父窗口的坐标,如果父窗口为NULL,则使用的是屏幕坐标;
  231. //::MoveWindow(hWxWnd, rcCenter.left, rcCenter.top, rcCenter.right, rcCenter.bottom, true);
  232. ::SetWindowPos(hWxWnd, NULL, rcCenter.left, rcCenter.top, rcCenter.right, rcCenter.bottom, SWP_SHOWWINDOW | SWP_HIDEWINDOW);
  233. //窗口重绘,(因创建exe时,设置为SW_HIDE,导致exe窗口会被父窗口覆盖一部分)
  234. //Invalidate();
  235. ::UpdateWindow(hWxWnd);
  236. ::ShowWindow(hWxWnd, SW_SHOW);
  237. m_bAttached = TRUE;
  238. }
  239. return 0;
  240. }
  241. BOOL CWxObject::DetachWxWnd()
  242. {
  243. if (m_hWxMainWnd)
  244. ::SetParent(m_hWxMainWnd, NULL);
  245. if (m_hWxLoginWnd)
  246. ::SetParent(m_hWxLoginWnd, NULL);
  247. m_bAttached = FALSE;
  248. return 0;
  249. }
  250. BOOL CWxObject::EnumWindowsProc(HWND hwnd, LPARAM lParam)
  251. {
  252. DWORD dwProcId = 0, dwThreadId;
  253. TCHAR szWndName[MAX_PATH] = { 0 };
  254. TCHAR szClassName[MAX_PATH] = { 0 };
  255. WNDINFO* pWndInfo = (WNDINFO*)lParam;
  256. dwThreadId = GetWindowThreadProcessId(hwnd, &dwProcId);
  257. if(dwProcId == pWndInfo->dwWxProcId)
  258. {
  259. pWndInfo->hWxWnd = hwnd;
  260. pWndInfo->dwThreadId = dwThreadId;
  261. ::GetWindowText(hwnd, szWndName, MAX_PATH);
  262. ::GetClassName(hwnd, szClassName, MAX_PATH);
  263. if (_tcscmp(szWndName, pWndInfo->szWndName) == 0 && _tcscmp(szClassName, pWndInfo->szClassName) == 0)
  264. {
  265. ::GetWindowRect(hwnd, &pWndInfo->rcWnd);
  266. return FALSE;
  267. }
  268. }
  269. return TRUE;
  270. }