#include "stdafx.h" #include "UnZipManager.h" #include "HJHomework.h" #include "../jsonLib/json/json.h" #include "../Util/Util.h" CUnZipManager::CUnZipManager() : m_hMainHwnd(nullptr), m_hPrintWnd(nullptr), m_hShareMemory(nullptr), m_bMonitor(TRUE) { wstring strModuleDir; GetModuleDir(strModuleDir); m_strUnZipPath = strModuleDir + L"HJUnzip.exe"; m_strConfigPath = strModuleDir + L"config.ini"; m_hShareMemory = CreateFileMapping( INVALID_HANDLE_VALUE, // 物理文件句柄 NULL, // 默认安全级别 PAGE_READWRITE, // 可读可写 0, // 高位文件大小 4096, // 低位文件大小 SHARE_MEMORY_NAME // 共享内存名称 ); LPVOID lpBase = MapViewOfFile(m_hShareMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0); share_memory_block* _data_block = (share_memory_block*)lpBase; _data_block->unzip_tickcount = 0; strcpy(_data_block->msg_info, ""); strcpy(_data_block->unzip_handle, ""); UnmapViewOfFile(lpBase); } CUnZipManager::~CUnZipManager() { CloseHandle(m_hShareMemory); } bool CUnZipManager::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) { if (pCopyDataStruct && pCopyDataStruct->lpData && pCopyDataStruct->cbData > 0) { std::string strJson((char*)pCopyDataStruct->lpData, pCopyDataStruct->cbData); Json::Features features; Json::Reader reader(features); Json::Value root; if (!reader.parse(strJson, root)) { return false; } if (root.isMember("command")) { if (pWnd) { m_hPrintWnd = pWnd->m_hWnd; } switch (root["command"].asInt()) { default: break; case COMMAND_UNZIP_MAIN_HEART: { m_pPrintLog(L"收到来自解压程序心跳包"); } break; case COMMAND_UNZIP_MESSAGEINFO: { if (root.isMember("info")) { //m_pPrintLog(CStrA2W(root["info"].asString().c_str()).c_str()); } } break; } return true; } else { return false; } } return false; } void CUnZipManager::SendCopyData(const char* szParam) { COPYDATASTRUCT cpd; cpd.dwData = strlen(szParam) + sizeof(COPYDATASTRUCT); cpd.cbData = strlen(szParam); cpd.lpData = (void*)szParam; ::SendMessage(m_hPrintWnd, WM_COPYDATA, (WPARAM)m_hMainHwnd, (LPARAM)&cpd); } void CUnZipManager::ShowPrintDialog() { } void CUnZipManager::HidePrintDialog() { } void CUnZipManager::OnClose() { KillProgress(L"HJUnzip.exe"); m_bMonitor = FALSE; } DWORD WINAPI ThreadMonitor(LPVOID lpThreadParameter) { CUnZipManager* _service = (CUnZipManager*)lpThreadParameter; _service->DoMonitor(); return 0; } void CUnZipManager::OnTaskStart() { LPVOID lpBase = MapViewOfFile(m_hShareMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0); share_memory_block* _data_block = (share_memory_block*)lpBase; _data_block->unzip_tickcount = std::time(0); strcpy(_data_block->msg_info, ""); UnmapViewOfFile(lpBase); m_bMonitor = TRUE; RunJHUzip(); m_hThreadMonitor = CreateThread(NULL, 0, ThreadMonitor, this, 0, 0); return; } void CUnZipManager::DoMonitor() { while (m_bMonitor) { LPVOID lpBase = MapViewOfFile(m_hShareMemory, FILE_MAP_ALL_ACCESS, 0, 0, 0); share_memory_block* _data_block = (share_memory_block*)lpBase; if (strlen(_data_block->msg_info) > 0) { m_pPrintLog(GB2312ToUnicode(_data_block->msg_info).c_str()); strcpy(_data_block->msg_info, ""); } if (_data_block->unzip_tickcount > 0) { int _unzip_count = std::time(0) - _data_block->unzip_tickcount; if (_unzip_count > 30) { m_pPrintLog(L"重连解压程序"); RunJHUzip(); _data_block->unzip_tickcount = std::time(0); } } HWND hWndUnzip = (HWND)strtol(_data_block->unzip_handle, 0, 16); if (IsWindow(hWndUnzip) && ::IsWindowVisible(hWndUnzip)) { ShowWindow(hWndUnzip, SW_HIDE); } UnmapViewOfFile(lpBase); Sleep(1000); } } void CUnZipManager::SetMainHwnd(HWND hwnd, PirntLogPtr pPrintLog) { m_hMainHwnd = hwnd; m_pPrintLog = pPrintLog; } void CUnZipManager::RunJHUzip() { KillProgress(L"HJUnzip.exe"); TCHAR szParam[1024] = { 0 }; TCHAR szWnd[MAX_PATH] = { 0 }; _stprintf(szWnd, _T("window=%p "), reinterpret_cast(m_hMainHwnd)); _tcscat(szParam, szWnd); PROCESS_INFORMATION _ProcStruct; STARTUPINFO StartInfo; memset(&StartInfo, 0, sizeof(STARTUPINFO)); StartInfo.cb = sizeof(STARTUPINFO); if (::CreateProcess( (LPCTSTR)m_strUnZipPath.c_str(), szParam, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &StartInfo, &_ProcStruct)) { CloseHandle(_ProcStruct.hProcess); CloseHandle(_ProcStruct.hThread); } }