MainDlg.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // MainDlg.cpp : implementation of the CMainDlg class
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "resource.h"
  6. #include <vector>
  7. #include <string>
  8. #include <json/json.h>
  9. #include <json/writer.h>
  10. #include <fstream>
  11. #include "7z/LzmaDec.h"
  12. #include "7z/7ZItf.h"
  13. #include <curl/curl.h>
  14. #include "../common/comm.h"
  15. #include <TlHelp32.h>
  16. #include "MainDlg.h"
  17. LRESULT CMainDlg::OnInitDialog(HWND, LPARAM )
  18. {
  19. // center the dialog on the screen
  20. CenterWindow();
  21. Count=1;
  22. // set icons
  23. HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON));
  24. SetIcon(hIcon, TRUE);
  25. HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON));
  26. SetIcon(hIconSmall, FALSE);
  27. _thread = std::thread(std::bind(&CMainDlg::UpdateThread, this));
  28. SetTimer(IDC_TIMER, 500);
  29. return TRUE;
  30. }
  31. void CMainDlg::OnTimer(UINT id)
  32. {
  33. std::string text;
  34. for (int i = 0; i < Count; i++)
  35. {
  36. text += ".";
  37. }
  38. Count++;
  39. if (Count == 4)
  40. Count = 0;
  41. SetDlgItemText(IDC_PREFIX, text.c_str());
  42. }
  43. void CMainDlg::UpdateThread()
  44. {
  45. LoadConfig();
  46. char szurl[256];
  47. sprintf(szurl, "%s/iego.json", _url.c_str());
  48. char szPath[MAX_PATH],szfile[MAX_PATH];
  49. GetCurrentDirectory(MAX_PATH, szPath);
  50. DownloadFile(szurl,"iego.json");
  51. Json::Value root;
  52. Json::Reader jsonReader;
  53. std::ifstream ifile("iego.json");
  54. int32_t version = -1;
  55. if (jsonReader.parse(ifile, root))
  56. {
  57. version = root["host_ver"].asInt();
  58. }
  59. ifile.close();
  60. DeleteFile("iego.json");
  61. if (_version < version)
  62. {
  63. std::string file = root["host_file"].asString();
  64. char szurl[256];
  65. sprintf(szurl, "%s/%s", _url.c_str(), file.c_str());
  66. DownloadFile(szurl, file.c_str());
  67. Decompress(file.c_str());
  68. DeleteFile(file.c_str());
  69. WriteConfig(version);
  70. }
  71. sprintf_s(szfile, "%s\\EgoWindow.exe", szPath);
  72. //LPPROCESS_INFORMATION info = NULL;
  73. STARTUPINFO si = { sizeof(si) };
  74. ShellExecute(NULL,"open",szfile, NULL, szPath,SW_SHOW);
  75. PostMessage(WM_COMPLETE);
  76. }
  77. void CMainDlg::OnKillProcess()
  78. {
  79. int * nCount = 0;
  80. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  81. //PROCESSENTRY32进程快照的结构体
  82. PROCESSENTRY32 pe;
  83. pe.dwSize = sizeof(PROCESSENTRY32);
  84. if (Process32First(hSnapShot, &pe))
  85. {
  86. while (Process32Next(hSnapShot, &pe))
  87. {
  88. if (stricmp(pe.szExeFile, "EgoWindow.exe") == 0)
  89. {
  90. DWORD dwProcessID = pe.th32ProcessID;
  91. HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
  92. ::TerminateProcess(hProcess, 0); //杀死进程
  93. CloseHandle(hProcess);
  94. }
  95. }
  96. CloseHandle(hSnapShot);
  97. }
  98. // do{
  99. // nCount = 0;
  100. //
  101. // EnumWindows(lpEnumFunc, (LPARAM)&nCount);
  102. // if (nCount > 0)
  103. // Sleep(100);
  104. // } while (nCount > 0);
  105. }
  106. void CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND)
  107. {
  108. EndDialog(wID);
  109. }
  110. void CMainDlg::OnComplete()
  111. {
  112. _thread.join();
  113. EndDialog(0);
  114. }
  115. CURLcode CMainDlg::DownloadFile(const char * url,const char * file)
  116. {
  117. FILE* fp = fopen(file, "wb+");
  118. CURL* curl = curl_easy_init();
  119. if (curl == NULL)
  120. {
  121. //抛出错误异常
  122. throw std::runtime_error("create easy_handle fail");
  123. }
  124. curl_easy_setopt(curl, CURLOPT_URL, url);
  125. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CMainDlg::DownloadCallback);
  126. curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  127. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 5);
  128. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  129. curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
  130. //开始进行请求
  131. CURLcode retCode = curl_easy_perform(curl);
  132. //清理curl_easy
  133. curl_easy_cleanup(curl);
  134. fclose(fp);
  135. return retCode;
  136. }
  137. bool CMainDlg::LoadConfig()
  138. {
  139. Json::Value root;
  140. Json::Reader jsonReader;
  141. std::ifstream ifile("./Config.json");
  142. if (jsonReader.parse(ifile, root))
  143. {
  144. _url = root["url"].asString();
  145. _version = root["version"].asInt();
  146. return true;
  147. }
  148. return false;
  149. }
  150. void CMainDlg::WriteConfig(int32_t version)
  151. {
  152. Json::Value root;
  153. Json::Reader jsonReader;
  154. std::ifstream ifile("Config.json");
  155. if (jsonReader.parse(ifile, root))
  156. {
  157. ifile.close();
  158. root["version"] = version;
  159. Json::StreamWriterBuilder b;
  160. std::unique_ptr<Json::StreamWriter> sw(b.newStreamWriter());
  161. //sw.Write(root);
  162. std::ofstream ofile;
  163. ofile.open("Config.json", std::ios::out);
  164. sw->write(root, &ofile);
  165. ofile.close();
  166. }
  167. }
  168. size_t CMainDlg::DownloadCallback(void* pBuffer, size_t nSize, size_t nMemBytes, FILE* fp)
  169. {
  170. size_t nWrite = fwrite(pBuffer, nSize, nMemBytes, fp);
  171. return nWrite;
  172. }