main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include <chrono>
  6. #include <iomanip>
  7. #include "WebServer.h"
  8. #include "Logger.h"
  9. #include "Authorization.h"
  10. // 服务相关
  11. SERVICE_STATUS g_ServiceStatus = {0};
  12. SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
  13. HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;
  14. std::shared_ptr<WebServer> g_WebServer;
  15. // 服务控制处理函数
  16. VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode)
  17. {
  18. switch (CtrlCode)
  19. {
  20. case SERVICE_CONTROL_STOP:
  21. if (g_ServiceStatus.dwCurrentState != SERVICE_STOP_PENDING)
  22. {
  23. g_ServiceStatus.dwControlsAccepted = 0;
  24. g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  25. g_ServiceStatus.dwWin32ExitCode = 0;
  26. g_ServiceStatus.dwCheckPoint = 4;
  27. if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  28. {
  29. LOG_ERROR("SetServiceStatus returned error");
  30. }
  31. // 停止服务
  32. SetEvent(g_ServiceStopEvent);
  33. }
  34. break;
  35. default:
  36. break;
  37. }
  38. }
  39. // 服务主函数
  40. VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
  41. {
  42. DWORD Status = E_FAIL;
  43. // 注册服务控制处理函数
  44. g_StatusHandle = RegisterServiceCtrlHandler(L"CommModifyService", ServiceCtrlHandler);
  45. if (g_StatusHandle == NULL)
  46. {
  47. return;
  48. }
  49. // 设置服务状态
  50. ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus));
  51. g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  52. g_ServiceStatus.dwControlsAccepted = 0;
  53. g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
  54. g_ServiceStatus.dwWin32ExitCode = 0;
  55. g_ServiceStatus.dwServiceSpecificExitCode = 0;
  56. g_ServiceStatus.dwCheckPoint = 0;
  57. if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  58. {
  59. LOG_ERROR("SetServiceStatus returned error");
  60. }
  61. // 创建停止事件
  62. g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  63. if (g_ServiceStopEvent == NULL)
  64. {
  65. g_ServiceStatus.dwControlsAccepted = 0;
  66. g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  67. g_ServiceStatus.dwWin32ExitCode = GetLastError();
  68. g_ServiceStatus.dwCheckPoint = 1;
  69. if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  70. {
  71. LOG_ERROR("SetServiceStatus returned error");
  72. }
  73. return;
  74. }
  75. // 启动Web服务器 - 改为shared_ptr
  76. g_WebServer = std::make_shared<WebServer>(8080);
  77. if (!g_WebServer->Start())
  78. {
  79. LOG_ERROR("Failed to start web server");
  80. g_ServiceStatus.dwControlsAccepted = 0;
  81. g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  82. g_ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_NOT_ACTIVE;
  83. g_ServiceStatus.dwCheckPoint = 3;
  84. SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
  85. CoUninitialize();
  86. return;
  87. }
  88. // 服务启动成功
  89. g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
  90. g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  91. g_ServiceStatus.dwWin32ExitCode = 0;
  92. g_ServiceStatus.dwCheckPoint = 0;
  93. if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  94. {
  95. LOG_ERROR("SetServiceStatus returned error");
  96. }
  97. LOG_INFO("CommModifyService started successfully");
  98. // 等待停止信号
  99. WaitForSingleObject(g_ServiceStopEvent, INFINITE);
  100. // 停止Web服务器
  101. if (g_WebServer)
  102. {
  103. g_WebServer->Stop();
  104. g_WebServer.reset();
  105. }
  106. // 关闭停止事件
  107. CloseHandle(g_ServiceStopEvent);
  108. // 设置服务状态为已停止
  109. g_ServiceStatus.dwControlsAccepted = 0;
  110. g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
  111. g_ServiceStatus.dwWin32ExitCode = 0;
  112. g_ServiceStatus.dwCheckPoint = 3;
  113. if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
  114. {
  115. LOG_ERROR("SetServiceStatus returned error");
  116. }
  117. LOG_INFO("CommModifyService stopped");
  118. }
  119. // 控制台模式运行
  120. void RunConsoleMode()
  121. {
  122. std::cout << "Starting CommModifyService in console mode..." << std::endl;
  123. // 启动Web服务器 - 改为shared_ptr
  124. auto webServer = std::make_shared<WebServer>(8080);
  125. if (!webServer->Start())
  126. {
  127. std::cerr << "Failed to start web server" << std::endl;
  128. CoUninitialize();
  129. return;
  130. }
  131. std::cout << "Web server started on port 8080" << std::endl;
  132. std::cout << "Open http://localhost:8080 in your browser" << std::endl;
  133. std::cout << "Press 'q' and Enter to quit..." << std::endl;
  134. // 等待用户输入
  135. char input;
  136. while (std::cin >> input && input != 'q')
  137. {
  138. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  139. }
  140. // 停止服务器
  141. webServer->Stop();
  142. std::cout << "Service stopped." << std::endl;
  143. }
  144. int main(int argc, char *argv[])
  145. {
  146. // 初始化日志
  147. Logger::GetInstance("CommModifyService.log");
  148. // 检查授权
  149. if (!Authorization::IsAuthorized()) {
  150. std::cerr << "Software is not authorized. Please use the authorization tool to generate a license file." << std::endl;
  151. std::cerr << "Authorization code file 'auth_code.dat' has been generated." << std::endl;
  152. return 1;
  153. }
  154. // 解析命令行参数
  155. bool console_mode = false;
  156. bool use_dll = true; // 默认使用DLL
  157. for (int i = 1; i < argc; i++)
  158. {
  159. if (std::string(argv[i]) == "-console" || std::string(argv[i]) == "/console")
  160. {
  161. console_mode = true;
  162. }
  163. else if (std::string(argv[i]) == "--use-dll")
  164. {
  165. use_dll = true; // 使用DLL包装器
  166. }
  167. }
  168. if (console_mode)
  169. {
  170. RunConsoleMode();
  171. return 0;
  172. }
  173. // 作为Windows服务运行
  174. SERVICE_TABLE_ENTRY ServiceTable[] = {
  175. {(LPWSTR)L"CommModifyService", (LPSERVICE_MAIN_FUNCTION)ServiceMain},
  176. {NULL, NULL}};
  177. if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
  178. {
  179. DWORD error = GetLastError();
  180. if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
  181. {
  182. // 不是作为服务运行,切换到控制台模式
  183. std::cout << "Not running as service, switching to console mode..." << std::endl;
  184. RunConsoleMode();
  185. }
  186. else
  187. {
  188. LOG_ERROR("StartServiceCtrlDispatcher returned error: " + std::to_string(error));
  189. }
  190. }
  191. return 0;
  192. }