CWxObject.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. // 注:必须在主窗口销毁前分离;
  72. if (!m_bAttached)
  73. DetachWxWnd();
  74. }
  75. void CWxObject::setInjectionObj(DWORD dwProcId, LPCTSTR lpDynamicLibraryPath)
  76. {
  77. ASSERT(dwProcId != 0);
  78. ASSERT(lpDynamicLibraryPath != NULL);
  79. m_dwWxProcId = dwProcId;
  80. memset(m_szDllPath, 0, sizeof(m_szDllPath));
  81. memset(m_wszDllPath, 0, sizeof(m_wszDllPath));
  82. #ifdef UNICODE
  83. _tcscpy_s(m_szDllPath, lpDynamicLibraryPath);
  84. #else
  85. _tcscpy_s(m_szDllPath, lpDynamicLibraryPath);
  86. ASCII2UNICODE(lpDynamicLibraryPath, m_wszDllPath, MAX_PATH);
  87. #endif
  88. //m_hWxProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, m_dwWxProcId);
  89. m_hWxProcess = OpenProcess(PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, m_dwWxProcId);
  90. if (m_hWxProcess == NULL)
  91. {
  92. WriteTextLog(_T("打开WeChat.exe进程失败"));
  93. }
  94. }
  95. BOOL CWxObject::InjectDynamicLibrary()
  96. {
  97. ASSERT(m_hWxProcess != NULL);
  98. m_dwPathLen = wcslen(m_wszDllPath) * sizeof(WCHAR) + 1;
  99. m_lpInjectData = VirtualAllocEx(m_hWxProcess, NULL, m_dwPathLen, MEM_COMMIT, PAGE_READWRITE);
  100. if (NULL == m_lpInjectData)
  101. {
  102. WriteTextLog(_T("创建WeChat.exe进程虚拟内存失败"));
  103. return FALSE;
  104. }
  105. if (WriteProcessMemory(m_hWxProcess, m_lpInjectData, m_wszDllPath, m_dwPathLen, NULL) == 0)
  106. {
  107. // 注意:MEM_RELEASE释放时第三参数一定要为0,请查看MSDN;
  108. VirtualFreeEx(m_hWxProcess, m_lpInjectData, 0, MEM_RELEASE);
  109. return FALSE;
  110. }
  111. HMODULE hk32 = GetModuleHandle(_T("kernel32.dll"));
  112. // 注意:微信使用的是W版本;
  113. LPVOID lpAddr = GetProcAddress(hk32, "LoadLibraryW");
  114. m_hInjectThread = CreateRemoteThread(m_hWxProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpAddr, m_lpInjectData, 0, NULL);
  115. if (NULL == m_hInjectThread)
  116. {
  117. // 注意:MEM_RELEASE释放时第三参数一定要为0,请查看MSDN;
  118. VirtualFreeEx(m_hWxProcess, m_lpInjectData, 0, MEM_RELEASE);
  119. return FALSE;
  120. }
  121. WaitForSingleObject(m_hInjectThread, INFINITE);
  122. if (m_hInjectThread)
  123. CloseHandle(m_hInjectThread);
  124. m_hInjectThread = NULL;
  125. /* 注入成功后,不能释放内存否则微信会挂;
  126. if (m_lpInjectData != NULL)
  127. VirtualFreeEx(m_hWxProcess, m_lpInjectData, 0, MEM_RELEASE);
  128. */
  129. return TRUE;
  130. }
  131. BOOL CWxObject::EjectDynamicLibrary()
  132. {
  133. if (m_hWxProcess == NULL)
  134. return TRUE;
  135. // 获取模块句柄;
  136. HANDLE hModule = FindModuleEx(m_szDllPath, m_dwWxProcId);
  137. if (hModule == NULL)
  138. {
  139. WriteTextLog(_T("获取WeChat.exe进程模块hook.dll失败"));
  140. return FALSE;
  141. }
  142. LPVOID lpAddr = GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "FreeLibraryAndExitThread");//FreeLibraryAndExitThread//FreeLibrary
  143. if (lpAddr == NULL)
  144. {
  145. WriteTextLog(_T("获取kernel32.dll中的FreeLibraryAndExitThread失败"));
  146. return FALSE;
  147. }
  148. m_hEjectThread = CreateRemoteThread(m_hWxProcess, NULL, 0, (LPTHREAD_START_ROUTINE)lpAddr, hModule, 0, NULL);
  149. if (m_hEjectThread == NULL)
  150. {
  151. WriteTextLog(_T("创建WeChat.exe远程线程(FreeLibraryAndExitThread)失败"));
  152. return FALSE;
  153. }
  154. WaitForSingleObject(m_hEjectThread, INFINITE);
  155. if (m_hEjectThread)
  156. CloseHandle(m_hEjectThread);
  157. m_hEjectThread = NULL;
  158. return TRUE;
  159. }
  160. BOOL CWxObject::FindWxMainWnd()
  161. {
  162. WNDINFO wnd;
  163. wnd.hWxWnd = NULL;
  164. wnd.dwWxProcId = m_dwWxProcId;
  165. _stprintf_s(wnd.szWndName, WX_MAIN_WND_NAME);
  166. _stprintf_s(wnd.szClassName, WX_MAIN_WND_CLASS_NAME);
  167. if (::EnumWindows(&EnumWindowsProc, (LPARAM)&wnd) == FALSE)
  168. {
  169. m_hWxMainWnd = wnd.hWxWnd;
  170. m_rcWxWnd = wnd.rcWnd;
  171. return TRUE;
  172. }
  173. return FALSE;
  174. }
  175. BOOL CWxObject::FindWxLoginWnd()
  176. {
  177. WNDINFO wnd;
  178. wnd.hWxWnd = NULL;
  179. wnd.dwWxProcId = m_dwWxProcId;
  180. _stprintf_s(wnd.szWndName, WX_LOGIN_WND_NAME);
  181. _stprintf_s(wnd.szClassName, WX_LOGIN_WND_CLASS_NAME);
  182. if (::EnumWindows(&EnumWindowsProc, (LPARAM)&wnd) == FALSE)
  183. {
  184. m_hWxLoginWnd = wnd.hWxWnd;
  185. m_rcWxWnd = wnd.rcWnd;
  186. return TRUE;
  187. }
  188. return FALSE;
  189. }
  190. BOOL CWxObject::Attach2MainWnd(CWnd *pMainWnd, BOOL bLoginWnd )
  191. {
  192. HWND hWxWnd = bLoginWnd ? m_hWxLoginWnd : m_hWxMainWnd;
  193. if (hWxWnd != NULL)
  194. {
  195. // 获取微信窗口的样式;
  196. DWORD dwStyle = ::GetWindowLong(hWxWnd, GWL_STYLE);
  197. // WS_CLIPSIBLINGS告诉父窗口不要绘制子窗口出现的区域;
  198. //dwStyle |= WS_CLIPSIBLINGS;
  199. // 如果窗口隐藏的,显示出来;
  200. dwStyle |= WS_VISIBLE;
  201. // 重新设置窗口样式 ;
  202. ::SetWindowLong(hWxWnd, GWL_STYLE, dwStyle);
  203. CRect rect;
  204. pMainWnd->GetWindowRect(&rect);
  205. // 设置父窗口;
  206. ::SetParent(hWxWnd, pMainWnd->m_hWnd);//set parent of ms paint to our dialog.
  207. // 擦除背景;
  208. //SetWindowLong(hWxWnd, GWL_STYLE, WS_VISIBLE);//eraze title of ms paint window.
  209. //Positioning ms paint.
  210. // 将屏幕坐标转在窗口坐标;
  211. pMainWnd->ScreenToClient(&rect);
  212. // 居中显示;
  213. CRect rcDisplay = rect;
  214. if (0)
  215. {// 居中显示;
  216. if (m_rcWxWnd.Width() >= rect.Width())
  217. {
  218. rcDisplay.left = 0;
  219. rcDisplay.right = rect.right;
  220. }
  221. else
  222. {
  223. rcDisplay.left = (rect.Width() - m_rcWxWnd.Width()) / 2;
  224. rcDisplay.right = rcDisplay.left + m_rcWxWnd.Width();
  225. }
  226. if (m_rcWxWnd.Height() >= rect.Height())
  227. {
  228. rcDisplay.top = 0;
  229. rcDisplay.bottom = rect.bottom;
  230. }
  231. else
  232. {
  233. rcDisplay.top = (rect.Height() - m_rcWxWnd.Height()) / 2;
  234. rcDisplay.bottom = rcDisplay.top + m_rcWxWnd.Height();
  235. }
  236. // 注意:MoveWindow/SetWindowPos使用的是父窗口的坐标,如果父窗口为NULL,则使用的是屏幕坐标;
  237. //::MoveWindow(hWxWnd, rcDisplay.left, rcDisplay.top, rcDisplay.Width(), rcDisplay.Height(), true);
  238. ::SetWindowPos(hWxWnd, NULL, rcDisplay.left, rcDisplay.top, rcDisplay.Width(), rcDisplay.Height(), WM_WINDOWPOSCHANGING|SWP_SHOWWINDOW | SWP_HIDEWINDOW);
  239. }
  240. else
  241. {//最大化显示并固定;
  242. // 注意:MoveWindow/SetWindowPos使用的是父窗口的坐标,如果父窗口为NULL,则使用的是屏幕坐标;
  243. ::MoveWindow(hWxWnd, rcDisplay.left, rcDisplay.top, rcDisplay.Width(), rcDisplay.Height(), true);
  244. //::SetWindowPos(hWxWnd, NULL, rcDisplay.left, rcDisplay.top, rcDisplay.Width(), rcDisplay.Height(), WM_MOVE| WM_SIZE| WM_WINDOWPOSCHANGING| WM_NCCALCSIZE | SWP_SHOWWINDOW);
  245. }
  246. #ifdef _DEBUG
  247. pMainWnd->ClientToScreen(&rect);
  248. WriteTextLog(_T("rect=[%d,%d,%d,%d]"), rect.left, rect.top, rect.right, rect.bottom);
  249. //pMainWnd->ScreenToClient(&m_rcWxWnd);
  250. WriteTextLog(_T("m_rcWxWnd=[%d,%d,%d,%d]"), m_rcWxWnd.left, m_rcWxWnd.top, m_rcWxWnd.right, m_rcWxWnd.bottom);
  251. WriteTextLog(_T("rcCenter=[%d,%d,%d,%d]"), rcDisplay.left, rcDisplay.top, rcDisplay.right, rcDisplay.bottom);
  252. #endif
  253. //窗口重绘,(因创建exe时,设置为SW_HIDE,导致exe窗口会被父窗口覆盖一部分)
  254. pMainWnd->Invalidate();
  255. ::UpdateWindow(hWxWnd);
  256. ::ShowWindow(hWxWnd, SW_SHOW);
  257. m_bAttached = TRUE;
  258. }
  259. return 0;
  260. }
  261. BOOL CWxObject::DetachWxWnd()
  262. {
  263. if (m_hWxMainWnd)
  264. ::SetParent(m_hWxMainWnd, NULL);
  265. if (m_hWxLoginWnd)
  266. ::SetParent(m_hWxLoginWnd, NULL);
  267. m_bAttached = FALSE;
  268. return 0;
  269. }
  270. BOOL CWxObject::EnumWindowsProc(HWND hwnd, LPARAM lParam)
  271. {
  272. DWORD dwProcId = 0, dwThreadId;
  273. TCHAR szWndName[MAX_PATH] = { 0 };
  274. TCHAR szClassName[MAX_PATH] = { 0 };
  275. WNDINFO* pWndInfo = (WNDINFO*)lParam;
  276. dwThreadId = GetWindowThreadProcessId(hwnd, &dwProcId);
  277. if(dwProcId == pWndInfo->dwWxProcId)
  278. {
  279. pWndInfo->hWxWnd = hwnd;
  280. pWndInfo->dwThreadId = dwThreadId;
  281. ::GetWindowText(hwnd, szWndName, MAX_PATH);
  282. ::GetClassName(hwnd, szClassName, MAX_PATH);
  283. #ifdef _DEBUG
  284. WriteTextLog(_T("窗口名称:%s, 窗口类名:%s, 线程ID:%d,句柄:%p"), szWndName, szClassName, dwThreadId, hwnd);
  285. #endif
  286. if (_tcscmp(szWndName, pWndInfo->szWndName) == 0 && _tcscmp(szClassName, pWndInfo->szClassName) == 0)
  287. {
  288. ::GetWindowRect(hwnd, &pWndInfo->rcWnd);
  289. return FALSE;
  290. }
  291. }
  292. return TRUE;
  293. }