UnZipManager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "stdafx.h"
  2. #include "UnZipManager.h"
  3. #include "HJHomework.h"
  4. #include "../jsonLib/json/json.h"
  5. #include "../Util/Util.h"
  6. CUnZipManager::CUnZipManager() :
  7. m_hMainHwnd(nullptr), m_hPrintWnd(nullptr), m_hShareMemory(nullptr), m_bMonitor(TRUE)
  8. {
  9. wstring strModuleDir;
  10. GetModuleDir(strModuleDir);
  11. m_strUnZipPath = strModuleDir + L"HJUnzip.exe";
  12. m_strConfigPath = strModuleDir + L"config.ini";
  13. m_hShareMemory = CreateFileMapping(
  14. INVALID_HANDLE_VALUE, // 物理文件句柄
  15. NULL, // 默认安全级别
  16. PAGE_READWRITE, // 可读可写
  17. 0, // 高位文件大小
  18. 4096, // 低位文件大小
  19. SHARE_MEMORY_NAME // 共享内存名称
  20. );
  21. LPVOID lpBase = MapViewOfFile(m_hShareMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  22. share_memory_block* _data_block = (share_memory_block*)lpBase;
  23. _data_block->unzip_tickcount = 0;
  24. strcpy(_data_block->msg_info, "");
  25. strcpy(_data_block->unzip_handle, "");
  26. UnmapViewOfFile(lpBase);
  27. }
  28. CUnZipManager::~CUnZipManager()
  29. {
  30. CloseHandle(m_hShareMemory);
  31. }
  32. bool CUnZipManager::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
  33. {
  34. if (pCopyDataStruct && pCopyDataStruct->lpData && pCopyDataStruct->cbData > 0)
  35. {
  36. std::string strJson((char*)pCopyDataStruct->lpData, pCopyDataStruct->cbData);
  37. Json::Features features;
  38. Json::Reader reader(features);
  39. Json::Value root;
  40. if (!reader.parse(strJson, root))
  41. {
  42. return false;
  43. }
  44. if (root.isMember("command"))
  45. {
  46. if (pWnd)
  47. {
  48. m_hPrintWnd = pWnd->m_hWnd;
  49. }
  50. switch (root["command"].asInt())
  51. {
  52. default:
  53. break;
  54. case COMMAND_UNZIP_MAIN_HEART:
  55. {
  56. m_pPrintLog(L"收到来自解压程序心跳包");
  57. }
  58. break;
  59. case COMMAND_UNZIP_MESSAGEINFO:
  60. {
  61. if (root.isMember("info"))
  62. {
  63. //m_pPrintLog(CStrA2W(root["info"].asString().c_str()).c_str());
  64. }
  65. }
  66. break;
  67. }
  68. return true;
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75. return false;
  76. }
  77. void CUnZipManager::SendCopyData(const char* szParam)
  78. {
  79. COPYDATASTRUCT cpd;
  80. cpd.dwData = strlen(szParam) + sizeof(COPYDATASTRUCT);
  81. cpd.cbData = strlen(szParam);
  82. cpd.lpData = (void*)szParam;
  83. ::SendMessage(m_hPrintWnd, WM_COPYDATA, (WPARAM)m_hMainHwnd, (LPARAM)&cpd);
  84. }
  85. void CUnZipManager::ShowPrintDialog()
  86. {
  87. }
  88. void CUnZipManager::HidePrintDialog()
  89. {
  90. }
  91. void CUnZipManager::OnClose()
  92. {
  93. KillProgress(L"HJUnzip.exe");
  94. m_bMonitor = FALSE;
  95. }
  96. DWORD WINAPI ThreadMonitor(LPVOID lpThreadParameter)
  97. {
  98. CUnZipManager* _service = (CUnZipManager*)lpThreadParameter;
  99. _service->DoMonitor();
  100. return 0;
  101. }
  102. void CUnZipManager::OnTaskStart()
  103. {
  104. LPVOID lpBase = MapViewOfFile(m_hShareMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  105. share_memory_block* _data_block = (share_memory_block*)lpBase;
  106. _data_block->unzip_tickcount = std::time(0);
  107. strcpy(_data_block->msg_info, "");
  108. UnmapViewOfFile(lpBase);
  109. m_bMonitor = TRUE;
  110. RunJHUzip();
  111. m_hThreadMonitor = CreateThread(NULL, 0, ThreadMonitor, this, 0, 0);
  112. return;
  113. }
  114. void CUnZipManager::DoMonitor()
  115. {
  116. while (m_bMonitor)
  117. {
  118. LPVOID lpBase = MapViewOfFile(m_hShareMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0);
  119. share_memory_block* _data_block = (share_memory_block*)lpBase;
  120. if (strlen(_data_block->msg_info) > 0)
  121. {
  122. m_pPrintLog(GB2312ToUnicode(_data_block->msg_info).c_str());
  123. strcpy(_data_block->msg_info, "");
  124. }
  125. if (_data_block->unzip_tickcount > 0)
  126. {
  127. int _unzip_count = std::time(0) - _data_block->unzip_tickcount;
  128. if (_unzip_count > 30)
  129. {
  130. m_pPrintLog(L"重连解压程序");
  131. RunJHUzip();
  132. _data_block->unzip_tickcount = std::time(0);
  133. }
  134. }
  135. HWND hWndUnzip = (HWND)strtol(_data_block->unzip_handle, 0, 16);
  136. if (IsWindow(hWndUnzip) && ::IsWindowVisible(hWndUnzip))
  137. {
  138. ShowWindow(hWndUnzip, SW_HIDE);
  139. }
  140. UnmapViewOfFile(lpBase);
  141. Sleep(1000);
  142. }
  143. }
  144. void CUnZipManager::SetMainHwnd(HWND hwnd, PirntLogPtr pPrintLog)
  145. {
  146. m_hMainHwnd = hwnd;
  147. m_pPrintLog = pPrintLog;
  148. }
  149. void CUnZipManager::RunJHUzip()
  150. {
  151. KillProgress(L"HJUnzip.exe");
  152. TCHAR szParam[1024] = { 0 };
  153. TCHAR szWnd[MAX_PATH] = { 0 };
  154. _stprintf(szWnd, _T("window=%p "), reinterpret_cast<long>(m_hMainHwnd));
  155. _tcscat(szParam, szWnd);
  156. PROCESS_INFORMATION _ProcStruct;
  157. STARTUPINFO StartInfo;
  158. memset(&StartInfo, 0, sizeof(STARTUPINFO));
  159. StartInfo.cb = sizeof(STARTUPINFO);
  160. if (::CreateProcess(
  161. (LPCTSTR)m_strUnZipPath.c_str(),
  162. szParam,
  163. NULL,
  164. NULL,
  165. FALSE,
  166. CREATE_NO_WINDOW,
  167. NULL,
  168. NULL,
  169. &StartInfo,
  170. &_ProcStruct))
  171. {
  172. CloseHandle(_ProcStruct.hProcess);
  173. CloseHandle(_ProcStruct.hThread);
  174. }
  175. }