#include #include #include #include #include #include #include "WebServer.h" #include "Logger.h" #include "Authorization.h" // 服务相关 SERVICE_STATUS g_ServiceStatus = {0}; SERVICE_STATUS_HANDLE g_StatusHandle = NULL; HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE; std::shared_ptr g_WebServer; // 服务控制处理函数 VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode) { switch (CtrlCode) { case SERVICE_CONTROL_STOP: if (g_ServiceStatus.dwCurrentState != SERVICE_STOP_PENDING) { g_ServiceStatus.dwControlsAccepted = 0; g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING; g_ServiceStatus.dwWin32ExitCode = 0; g_ServiceStatus.dwCheckPoint = 4; if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE) { LOG_ERROR("SetServiceStatus returned error"); } // 停止服务 SetEvent(g_ServiceStopEvent); } break; default: break; } } // 服务主函数 VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv) { DWORD Status = E_FAIL; // 注册服务控制处理函数 g_StatusHandle = RegisterServiceCtrlHandler(L"CommModifyService", ServiceCtrlHandler); if (g_StatusHandle == NULL) { return; } // 设置服务状态 ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus)); g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; g_ServiceStatus.dwControlsAccepted = 0; g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING; g_ServiceStatus.dwWin32ExitCode = 0; g_ServiceStatus.dwServiceSpecificExitCode = 0; g_ServiceStatus.dwCheckPoint = 0; if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE) { LOG_ERROR("SetServiceStatus returned error"); } // 创建停止事件 g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (g_ServiceStopEvent == NULL) { g_ServiceStatus.dwControlsAccepted = 0; g_ServiceStatus.dwCurrentState = SERVICE_STOPPED; g_ServiceStatus.dwWin32ExitCode = GetLastError(); g_ServiceStatus.dwCheckPoint = 1; if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE) { LOG_ERROR("SetServiceStatus returned error"); } return; } // 启动Web服务器 - 改为shared_ptr g_WebServer = std::make_shared(8080); if (!g_WebServer->Start()) { LOG_ERROR("Failed to start web server"); g_ServiceStatus.dwControlsAccepted = 0; g_ServiceStatus.dwCurrentState = SERVICE_STOPPED; g_ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_NOT_ACTIVE; g_ServiceStatus.dwCheckPoint = 3; SetServiceStatus(g_StatusHandle, &g_ServiceStatus); CoUninitialize(); return; } // 服务启动成功 g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP; g_ServiceStatus.dwCurrentState = SERVICE_RUNNING; g_ServiceStatus.dwWin32ExitCode = 0; g_ServiceStatus.dwCheckPoint = 0; if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE) { LOG_ERROR("SetServiceStatus returned error"); } LOG_INFO("CommModifyService started successfully"); // 等待停止信号 WaitForSingleObject(g_ServiceStopEvent, INFINITE); // 停止Web服务器 if (g_WebServer) { g_WebServer->Stop(); g_WebServer.reset(); } // 关闭停止事件 CloseHandle(g_ServiceStopEvent); // 设置服务状态为已停止 g_ServiceStatus.dwControlsAccepted = 0; g_ServiceStatus.dwCurrentState = SERVICE_STOPPED; g_ServiceStatus.dwWin32ExitCode = 0; g_ServiceStatus.dwCheckPoint = 3; if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE) { LOG_ERROR("SetServiceStatus returned error"); } LOG_INFO("CommModifyService stopped"); } // 控制台模式运行 void RunConsoleMode() { std::cout << "Starting CommModifyService in console mode..." << std::endl; // 启动Web服务器 - 改为shared_ptr auto webServer = std::make_shared(8080); if (!webServer->Start()) { std::cerr << "Failed to start web server" << std::endl; CoUninitialize(); return; } std::cout << "Web server started on port 8080" << std::endl; std::cout << "Open http://localhost:8080 in your browser" << std::endl; std::cout << "Press 'q' and Enter to quit..." << std::endl; // 等待用户输入 char input; while (std::cin >> input && input != 'q') { std::this_thread::sleep_for(std::chrono::milliseconds(100)); } // 停止服务器 webServer->Stop(); std::cout << "Service stopped." << std::endl; } int main(int argc, char *argv[]) { // 初始化日志 Logger::GetInstance("CommModifyService.log"); // 检查授权 if (!Authorization::IsAuthorized()) { std::cerr << "Software is not authorized. Please use the authorization tool to generate a license file." << std::endl; std::cerr << "Authorization code file 'auth_code.dat' has been generated." << std::endl; return 1; } // 解析命令行参数 bool console_mode = false; bool use_dll = true; // 默认使用DLL for (int i = 1; i < argc; i++) { if (std::string(argv[i]) == "-console" || std::string(argv[i]) == "/console") { console_mode = true; } else if (std::string(argv[i]) == "--use-dll") { use_dll = true; // 使用DLL包装器 } } if (console_mode) { RunConsoleMode(); return 0; } // 作为Windows服务运行 SERVICE_TABLE_ENTRY ServiceTable[] = { {(LPWSTR)L"CommModifyService", (LPSERVICE_MAIN_FUNCTION)ServiceMain}, {NULL, NULL}}; if (StartServiceCtrlDispatcher(ServiceTable) == FALSE) { DWORD error = GetLastError(); if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { // 不是作为服务运行,切换到控制台模式 std::cout << "Not running as service, switching to console mode..." << std::endl; RunConsoleMode(); } else { LOG_ERROR("StartServiceCtrlDispatcher returned error: " + std::to_string(error)); } } return 0; }