int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { RestartService(); logTo(L"execute WindowRestartService"); return 0; UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: Place code here. // Initialize global strings LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_WINDOWRESTARTSERVICE, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // Perform application initialization: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWRESTARTSERVICE)); MSG msg; // Main message loop: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; }
DWORD ServiceExecutionThread(LPDWORD param) { #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread\n"); #endif if (IntializeSystemInfo() == FALSE) { return IC_NO_INITIALIZE; } WORD wVersionRequested = MAKEWORD(1, 1); WSADATA wsaData; WSAStartup(wVersionRequested, &wsaData); SOCKET udpSocket = INVALID_SOCKET; int result = IC_NOERROR; bool showHelp = false; do { g_killServiceEvent = CreateEvent(0, TRUE, FALSE, 0); if (!g_killServiceEvent) { OutputError(L"CreateEvent fails! (%d)", GetLastError()); result = IC_EVENT_CREATE_FAIL; break; } wstring apiKey; wstring envInfo; vector<int> intervalTimes; ConnectionInfo connection; g_debugMode = GetDebugMode(g_argc, g_argv); if (cmdOptionExists(g_argv, g_argv + g_argc, L"-h") == true || cmdOptionExists(g_argv, g_argv + g_argc, L"/h") == true) { showHelp = true; break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-unreg") == true) { DoUnregistration(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-start") == true) { DoStartService(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-stop") == true) { DoStopService(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-update") == true) { ProcessLatestUpdate(); break; } if (cmdOptionExists(g_argv, g_argv + g_argc, L"-restart") == true) { RestartService(); break; } apiKey = GetApiKey(g_argc, g_argv); envInfo = GetEnvInfo(g_argc, g_argv); intervalTimes = GetIntervalTime(g_argc, g_argv); if (apiKey.length() == 0) { OutputError(L"NO ApiKey\n"); result = IC_NO_APIKEY; showHelp = true; break; } if (envInfo.length() == 0) { OutputError(L"NO AgentID Info\n"); result = IC_NO_AGENTIDINFO; showHelp = true; break; } string address = GetHostAddress(g_argc, g_argv, connection); struct hostent *host = gethostbyname(address.c_str()); if (host == nullptr) { OutputError(L"Can't resolve host address: %s\n", address.c_str()); result = IC_NO_RESOLVE_HOSTADDR; break; } SetupHostPort(g_argc, g_argv, connection); if (cmdOptionExists(g_argv, g_argv + g_argc, L"-regservice") == true) { DoRegistration(apiKey, envInfo, address, connection.Getport(), intervalTimes); break; } struct sockaddr_in remoteServAddr; remoteServAddr.sin_family = host->h_addrtype; memcpy((char *)&remoteServAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length); remoteServAddr.sin_port = htons((u_short)connection.Getport()); /* socket creation */ udpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (udpSocket == INVALID_SOCKET) { OutputError(L"socket failed with error: %ld\n", WSAGetLastError()); break; } wstring appVersion = GetAppVersion(g_moduleFilePath.c_str(), NULL, NULL, NULL, NULL); OutputConsole(L"(%s) ServiceExecutionThread - data collect thread - start\n", appVersion.c_str()); { thread processCpuMemThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]() { ProcessCpuMemInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[0]); }); thread processDiskThread([apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes]() { ProcessDiskInfo(apiKey, envInfo, udpSocket, remoteServAddr, intervalTimes[1]); }); thread updateThread([]() { DWORD oneday = 1000 * 60 * 60 * 24; while (true) { ProcessLatestUpdate(); if (::WaitForSingleObject(g_killServiceEvent, oneday) == WAIT_TIMEOUT) { continue; } break; } }); if (g_isConsoleApp == TRUE) { printf("Press any key to exit...\n"); getchar(); } else { #if _DEBUG ::OutputDebugString(L"Service thread - WaitForSingleObject...\n"); #endif WaitForSingleObject(g_killServiceEvent, INFINITE); } #if _DEBUG ::OutputDebugString(L"Service thread waiting...\n"); Sleep(1000); #endif if (processCpuMemThread.joinable() == true) { processCpuMemThread.join(); } if (processDiskThread.joinable() == true) { processDiskThread.join(); } if (updateThread.joinable() == true) { updateThread.join(); } #if _DEBUG ::OutputDebugString(L"All Service-threads exited...\n"); #endif } ::SetEvent(g_killSafeExitEvent); #if _DEBUG ::OutputDebugString(L"Service thread detached\n"); #endif } while (false); #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ending\n"); #endif if (udpSocket != INVALID_SOCKET) { closesocket(udpSocket); } if (showHelp == true) { ShowHelp(); } WSACleanup(); #if _DEBUG ::OutputDebugString(L"ServiceExecutionThread - data collect thread - ended\n"); #endif return result; }