#ifdef _WIN32 #include "huslib/huslib.h" #include "huslib/huslibex.h" #include #ifdef _DEBUG #include #endif // _DEBUG #include "windows/WinService.h" #define WRITE_MINI_DUMP #if defined(_WIN32) && !defined(_DEBUG) && defined(WRITE_MINI_DUMP) #include "windows/DebugAssist.h" #endif // WRITE_MINI_DUMP #else #include #endif // _WIN32 #include #include "Object.h" #include "HusApp.h" HApp * HApp::m_pApp = NULL; HApp::HApp() { m_pApp = this; } /////////////////////////////////////// #ifdef _WIN32 #define TIMEOUT_SERVICE_START_PENDING 5000 VOID ServiceStart(DWORD nArgc, LPTSTR *pArgv, BOOL bConsoleMode) { #if defined(_WIN32) && !defined(_DEBUG) InitialDumpWriting(); // for Windows Dump File #endif #ifdef _DEBUG _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF); #endif // _DEBUG WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); if (WSAStartup(wVersionRequested, &wsaData) != 0) { printf("[ERROR] Windows sockets initialization failed. WSAGetLastError = %d\n", WSAGetLastError()); return; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { printf("[ERROR] Could not find a usable WinSock DLL.\n"); WSACleanup(); return; } if (!ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, TIMEOUT_SERVICE_START_PENDING)) return; if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0)) return; HApp::m_pApp->OnMain(nArgc, pArgv); WSACleanup(); } VOID ServiceStop() { HApp::m_pApp->OnExit(0); if (!ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, TIMEOUT_SERVICE_START_PENDING)) return; if (!ReportStatusToSCMgr(SERVICE_STOPPED, NO_ERROR, 0)) return; } #endif // _WIN32 int main(int nArgc, char ** pArgv) { if (HApp::m_pApp == NULL) { printf("ERROR: Can't find an object that derives from HApp.\n"); return 0; } #ifdef _WIN32 WinServiceMain(nArgc, pArgv); return 0; #else signal(SIGPIPE, SIG_IGN); #endif // _WIN32 return HApp::m_pApp->OnMain(nArgc, pArgv); }