26 if (
status->dwCurrentState == SERVICE_START_PENDING)
28 status->dwControlsAccepted = 0;
32 status->dwControlsAccepted = SERVICE_ACCEPT_STOP;
35 if (
status->dwCurrentState == SERVICE_RUNNING ||
status->dwCurrentState == SERVICE_STOPPED)
41 static DWORD dwCheckPoint = 1;
42 status->dwCheckPoint = dwCheckPoint++;
58 SC_HANDLE svc_ctl_mgr;
62 if (GetModuleFileName(NULL, path + 1, _countof(path) - 2) == 0)
69 wcscat_s(path, _countof(path), L
"\"");
71 svc_ctl_mgr = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE);
72 if (svc_ctl_mgr == NULL)
80 SC_HANDLE
service = CreateService(
82 SERVICE_QUERY_STATUS, SERVICE_WIN32_SHARE_PROCESS,
openvpn_service[i].start_type,
83 SERVICE_ERROR_NORMAL, path, NULL, NULL,
openvpn_service[i].dependencies, NULL, NULL);
96 CloseServiceHandle(svc_ctl_mgr);
105 SC_HANDLE svc_ctl_mgr;
108 svc_ctl_mgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
109 if (svc_ctl_mgr == NULL)
118 if (StartService(
service, 0, NULL))
120 wprintf(L
"Service Started\n");
135 CloseServiceHandle(svc_ctl_mgr);
144 SC_HANDLE svc_ctl_mgr;
148 svc_ctl_mgr = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
149 if (svc_ctl_mgr == NULL)
159 OpenService(svc_ctl_mgr, ovpn_svc->
name, DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);
174 if (
status.dwCurrentState == SERVICE_STOP_PENDING)
185 if (
status.dwCurrentState == SERVICE_STOPPED)
191 wprintf(L
"\n%ls failed to stop.\n", ovpn_svc->
display_name);
210 CloseServiceHandle(svc_ctl_mgr);
222 const SERVICE_TABLE_ENTRY dispatchTable_shared[] = {
230 const SERVICE_TABLE_ENTRY *dispatchTable = dispatchTable_shared;
234 for (
int i = 1; i < argc; i++)
236 if (*
argv[i] == L
'-' || *
argv[i] == L
'/')
238 if (_wcsicmp(L
"install",
argv[i] + 1) == 0)
242 else if (_wcsicmp(L
"remove",
argv[i] + 1) == 0)
246 else if (_wcsicmp(L
"start",
argv[i] + 1) == 0)
250 else if (argc > i + 2 && _wcsicmp(L
"instance",
argv[i] + 1) == 0)
252 if (_wcsicmp(L
"interactive",
argv[i + 1]) == 0)
254 dispatchTable = dispatchTable_interactive;
261 L
"Invalid argument to -instance <%s>. Service not started.",
268 wprintf(L
"%ls -install to install the interactive service\n",
APPNAME);
270 L
"%ls -start [name] to start the service (name = \"interactive\") is optional\n",
272 wprintf(L
"%ls -remove to remove the service\n",
APPNAME);
274 wprintf(L
"\nService run-time parameters:\n");
275 wprintf(L
"-instance interactive <id>\n"
276 L
" Runs the service as an alternate instance.\n"
277 L
" The service settings will be loaded from\n"
278 L
" HKLM\\Software\\" _L(
279 PACKAGE_NAME) L
"<id> registry key, and the service will accept\n"
280 L
" requests on \\\\.\\pipe\\" _L(
281 PACKAGE) L
"<id>\\service named pipe.\n");
292 wprintf(L
"\nStartServiceCtrlDispatcher being called.\n");
293 wprintf(L
"This may take several seconds. Please wait.\n");
295 if (!StartServiceCtrlDispatcher(dispatchTable))
DWORD MsgToEventLog(DWORD flags, LPCWSTR format,...)
LPCWSTR GetLastErrorText(void)
VOID WINAPI ServiceStartInteractive(DWORD dwArgc, LPWSTR *lpszArgv)
static SERVICE_STATUS status
openvpn_service_t interactive_service
VOID WINAPI ServiceStartInteractiveOwn(DWORD dwArgc, LPWSTR *lpszArgv)
static SERVICE_STATUS_HANDLE service
static int CmdStartService(openvpn_service_type type)
static int CmdInstallServices(void)
static int CmdRemoveServices(void)
BOOL ReportStatusToSCMgr(SERVICE_STATUS_HANDLE service, SERVICE_STATUS *status)
openvpn_service_t openvpn_service[_service_max]