int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { char exe_file_name[MAX_PATH], dir[MAX_PATH], *ptr; static struct WSAData wsa_state; ghInst=hInstance; GetModuleFileName(0, exe_file_name, MAX_PATH); /* set current directory */ strcpy(dir, exe_file_name); ptr=strrchr(dir, '\\'); /* last backslash */ if(ptr) ptr[1]='\0'; /* truncate program name */ if(!SetCurrentDirectory(dir)) { MessageBox(hwnd, "Cannot set current directory", options.win32_name, MB_ICONERROR); return 1; } /* setup service_path for CreateService() */ strcpy(service_path, "\""); strcat(service_path, exe_file_name); strcat(service_path, "\" -service"); /* strcat(service_path, lpszCmdLine); */ if(WSAStartup(0x0101, &wsa_state)) { win_log("Failed to initialize winsock"); error_mode=1; } if(!strcmpi(lpszCmdLine, "-service")) { if(!setjmp(jump_buf)) main_initialize(NULL, NULL); return start_service(); /* Always start service with -service option */ } if(!error_mode && !setjmp(jump_buf)) { /* TRY */ if(!strcmpi(lpszCmdLine, "-install")) { main_initialize(NULL, NULL); return install_service(); } else if(!strcmpi(lpszCmdLine, "-uninstall")) { main_initialize(NULL, NULL); return uninstall_service(); } else { /* not -service, -install or -uninstall */ main_initialize(lpszCmdLine[0] ? lpszCmdLine : NULL, NULL); } } /* CATCH */ return win_main(hInstance, hPrevInstance, lpszCmdLine, nCmdShow); }
static void WINAPI service_main(DWORD argc, LPTSTR* argv) { /* initialise service status */ serviceStatus.dwServiceType=SERVICE_WIN32; serviceStatus.dwCurrentState=SERVICE_STOPPED; serviceStatus.dwControlsAccepted=0; serviceStatus.dwWin32ExitCode=NO_ERROR; serviceStatus.dwServiceSpecificExitCode=NO_ERROR; serviceStatus.dwCheckPoint=0; serviceStatus.dwWaitHint=0; serviceStatusHandle= RegisterServiceCtrlHandler(options.win32_service, control_handler); if(serviceStatusHandle) { /* service is starting */ serviceStatus.dwCurrentState=SERVICE_START_PENDING; SetServiceStatus(serviceStatusHandle, &serviceStatus); /* do initialisation here */ stopServiceEvent=CreateEvent(0, FALSE, FALSE, 0); /* running */ serviceStatus.dwControlsAccepted|= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN); serviceStatus.dwCurrentState=SERVICE_RUNNING; SetServiceStatus(serviceStatusHandle, &serviceStatus); win_main(ghInst, NULL, "", 0); /* service was stopped */ serviceStatus.dwCurrentState=SERVICE_STOP_PENDING; SetServiceStatus(serviceStatusHandle, &serviceStatus); /* do cleanup here */ CloseHandle(stopServiceEvent); stopServiceEvent=0; /* service is now stopped */ serviceStatus.dwControlsAccepted&= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN); serviceStatus.dwCurrentState=SERVICE_STOPPED; SetServiceStatus(serviceStatusHandle, &serviceStatus); } }
int main(void) { win_main(); return 0; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, #ifdef _WIN32_WCE LPWSTR lpCmdLine, #else LPSTR lpCmdLine, #endif int nCmdShow) { LPSTR command_line; #ifdef _WIN32_WCE command_line=tstr2str(lpCmdLine); #else command_line=lpCmdLine; #endif ghInst=hInstance; parse_cmdline(command_line); /* setup global cmdline structure */ #ifndef _WIN32_WCE if(set_cwd()) /* set current working directory */ return 1; #endif /* setup the windo caption before reading the configuration file * options.win32_service is not available here and may not be used */ #ifdef _WIN32_WCE _tcscpy(win32_name, TEXT("stunnel ") TEXT(VERSION) TEXT(" on Windows CE (not configured)")); #else _tcscpy(win32_name, TEXT("stunnel ") TEXT(VERSION) TEXT(" on Win32 (not configured)")); #endif if(initialize_winsock()) return 1; if(!setjmp(jump_buf)) { /* TRY */ main_initialize( cmdline.config_file[0] ? cmdline.config_file : NULL, NULL); #ifdef _WIN32_WCE _tcscpy(win32_name, TEXT("stunnel ") TEXT(VERSION) TEXT(" on Windows CE")); #else _snprintf(win32_name, STRLEN, "stunnel %s on Win32 (%s)", VERSION, options.win32_service); /* update the information */ if(!cmdline.service) { if(cmdline.install) return service_install(command_line); if(cmdline.uninstall) return service_uninstall(); if(cmdline.start) return service_start(); if(cmdline.stop) return service_stop(); } #endif } /* CATCH */ #ifndef _WIN32_WCE if(cmdline.service) return service_initialize(); else #endif return win_main(hInstance, hPrevInstance, command_line, nCmdShow); }