WxMgr.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "stdafx.h"
  2. #include "WxMgr.h"
  3. #include "Global.h"
  4. #include "resource.h"
  5. TCHAR CWxMgr::m_szWeChatPath[MAX_PATH] = { 0 };
  6. CWxMgr::CWxMgr(CWnd *pMainWnd) :m_pMainWnd(pMainWnd)
  7. {
  8. GetWeChatPath();
  9. }
  10. CWxMgr::~CWxMgr()
  11. {
  12. }
  13. CRect CWxMgr::CalcRect()
  14. {
  15. CRect rc;
  16. int nCount = m_mapWx.size();
  17. // 高固定大小;
  18. rc.left = m_rcWxList.left + 2;
  19. rc.top = 100 * nCount + m_rcWxList.top + 20;
  20. rc.right = m_rcWxList.right - 2;
  21. rc.bottom = rc.top + 100;
  22. return rc;
  23. }
  24. BOOL CWxMgr::AttachUnMgrWxProc(CWnd *pWnd)
  25. {
  26. vector<DWORD> vtWxProcId = FindAllProcess(WECHAT);
  27. for (DWORD pid : vtWxProcId)
  28. {
  29. if (m_mapWx.count(pid) == 0)
  30. {
  31. AddMapItem(pid, NULL);
  32. }
  33. }
  34. return TRUE;
  35. }
  36. BOOL CWxMgr::AttachWx()
  37. {
  38. return 0;
  39. }
  40. VOID CWxMgr::AddMapItem(DWORD dwProcId, LPCTSTR lpItemName)
  41. {
  42. WXOBJINFO wxObjInfo;
  43. // 创建微信对象;
  44. wxObjInfo.pWxObject = new CWxObject(dwProcId, g_szDynamicLibraryPath);
  45. // 创建按钮;
  46. wxObjInfo.pWxButton = new CButtonST();
  47. wxObjInfo.pWxButton->Create(lpItemName, WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, CalcRect(), m_pMainWnd, dwProcId);
  48. wxObjInfo.pWxButton->OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 60);
  49. //wxObjInfo.pWxButton->DrawTransparent();
  50. wxObjInfo.pWxButton->SetIcon(ICON_CHECK);
  51. wxObjInfo.pWxButton->ShowWindow(SW_SHOW);
  52. // 创建聊天窗口;
  53. wxObjInfo.pWxChatWnd = new CStatic();
  54. wxObjInfo.pWxChatWnd->Create(lpItemName, WS_CHILD | WS_VISIBLE | WS_TABSTOP | SS_GRAYRECT, m_rcWxShow, m_pMainWnd, dwProcId + 1);
  55. wxObjInfo.pWxChatWnd->ShowWindow(SW_SHOW);
  56. // 附加到窗口;
  57. wxObjInfo.pWxObject->InjectDynamicLibrary();
  58. if (wxObjInfo.pWxObject->FindWxMainWnd())
  59. {
  60. wxObjInfo.pWxObject->Attach2MainWnd(wxObjInfo.pWxChatWnd, FALSE);
  61. }
  62. else if (wxObjInfo.pWxObject->FindWxLoginWnd())
  63. {
  64. wxObjInfo.pWxObject->Attach2MainWnd(wxObjInfo.pWxChatWnd, TRUE);
  65. }
  66. m_mapWx.insert(pair<DWORD, WXOBJINFO>(dwProcId, wxObjInfo));
  67. }
  68. VOID CWxMgr::RemoveMapItem(DWORD dwProcId)
  69. {
  70. for (pair<DWORD, WXOBJINFO> wxinfo : m_mapWx)
  71. {
  72. if (wxinfo.first == dwProcId)
  73. {
  74. wxinfo.second.pWxObject->DetachWxWnd();
  75. delete wxinfo.second.pWxObject;
  76. delete wxinfo.second.pWxButton;
  77. delete wxinfo.second.pWxChatWnd;
  78. // 下方的WxButton向上移动;
  79. // ...
  80. break;
  81. }
  82. }
  83. // 移除key;
  84. m_mapWx.erase(dwProcId);
  85. }
  86. VOID CWxMgr::DetachAllWxProc()
  87. {
  88. for (pair<DWORD, WXOBJINFO> wxinfo : m_mapWx)
  89. {
  90. wxinfo.second.pWxObject->DetachWxWnd();
  91. delete wxinfo.second.pWxObject;
  92. delete wxinfo.second.pWxButton;
  93. delete wxinfo.second.pWxChatWnd;
  94. }
  95. m_mapWx.clear();
  96. }
  97. BOOL CWxMgr::OpenWeChat()
  98. {
  99. // 关闭互斥句柄;
  100. PatchWeChat();
  101. STARTUPINFO si;
  102. PROCESS_INFORMATION pi;
  103. ZeroMemory(&si, sizeof(si));
  104. si.cb = sizeof(si);
  105. ZeroMemory(&pi, sizeof(pi));
  106. si.dwFlags = STARTF_USESHOWWINDOW; // 指定wShowWindow成员有效
  107. si.wShowWindow = SW_HIDE; // 设置创建进程时,窗口不显示,
  108. // 为FALSE的话则不显示
  109. BOOL bRet = ::CreateProcess(
  110. m_szWeChatPath, // 不在此指定可执行文件的文件名
  111. NULL, // 命令行参数
  112. NULL, // 默认进程安全性
  113. NULL, // 默认线程安全性
  114. FALSE, // 指定当前进程内的句柄不可以被子进程继承
  115. NULL,
  116. NULL, // 使用本进程的环境变量
  117. NULL, // 使用本进程的驱动器和目录
  118. &si,
  119. &pi);
  120. if (bRet)
  121. {
  122. /*WaitForSingleObject(pi.hProcess, INFINITE);*/
  123. //WaitForInputIdle(pi.hProcess, INFINITE);
  124. Sleep(1500); // 启动微信需要1秒左右;
  125. // 既然我们不使用两个句柄,最好是立刻将它们关闭
  126. ::CloseHandle(pi.hThread);
  127. ::CloseHandle(pi.hProcess);
  128. // 当进程挂起时,是无法修改关闭微信句柄;
  129. AddMapItem(pi.dwProcessId, NULL);
  130. }
  131. return 0;
  132. }
  133. BOOL CWxMgr::GetWeChatPath()
  134. {
  135. static BOOL bGotPath = FALSE;
  136. if (!bGotPath)
  137. {
  138. // 通过注册表获取微信安装目录;
  139. HKEY hKey = NULL;
  140. if (ERROR_SUCCESS != RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Tencent\\WeChat"), &hKey))
  141. {
  142. return FALSE;
  143. }
  144. DWORD Type = REG_SZ;
  145. DWORD cbData = MAX_PATH * sizeof(WCHAR);
  146. if (ERROR_SUCCESS != RegQueryValueEx(hKey, _T("InstallPath"), 0, &Type, (LPBYTE)m_szWeChatPath, &cbData))
  147. {
  148. RegCloseKey(hKey);
  149. return FALSE;
  150. }
  151. PathAppend(m_szWeChatPath, _T("WeChat.exe"));
  152. bGotPath = TRUE;
  153. }
  154. return bGotPath;
  155. }