24 static DWORD dwCheckPoint = 1;
27 if (
status->dwCurrentState == SERVICE_START_PENDING)
29 status->dwControlsAccepted = 0;
33 status->dwControlsAccepted = SERVICE_ACCEPT_STOP;
36 if (
status->dwCurrentState == SERVICE_RUNNING ||
status->dwCurrentState == SERVICE_STOPPED)
42 status->dwCheckPoint = dwCheckPoint++;
59 SC_HANDLE svc_ctl_mgr;
63 if (GetModuleFileName(NULL, path + 1, _countof(path) - 2) == 0)
70 wcscat_s(path, _countof(path), L
"\"");
72 svc_ctl_mgr = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE);
73 if (svc_ctl_mgr == NULL)
83 SERVICE_QUERY_STATUS, SERVICE_WIN32_SHARE_PROCESS,
openvpn_service[
i].start_type,
84 SERVICE_ERROR_NORMAL, path, NULL, NULL,
openvpn_service[
i].dependencies, NULL, NULL);
97 CloseServiceHandle(svc_ctl_mgr);
106 SC_HANDLE svc_ctl_mgr;
109 svc_ctl_mgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
110 if (svc_ctl_mgr == NULL)
119 if (StartService(
service, 0, NULL))
121 wprintf(L
"Service Started\n");
136 CloseServiceHandle(svc_ctl_mgr);
145 SC_HANDLE svc_ctl_mgr;
149 svc_ctl_mgr = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
150 if (svc_ctl_mgr == NULL)
160 OpenService(svc_ctl_mgr, ovpn_svc->
name, DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);
175 if (
status.dwCurrentState == SERVICE_STOP_PENDING)
186 if (
status.dwCurrentState == SERVICE_STOPPED)
192 wprintf(L
"\n%ls failed to stop.\n", ovpn_svc->
display_name);
211 CloseServiceHandle(svc_ctl_mgr);
223 const SERVICE_TABLE_ENTRY dispatchTable_shared[] = {
231 const SERVICE_TABLE_ENTRY *dispatchTable = dispatchTable_shared;
235 for (
int i = 1;
i < argc;
i++)
239 if (_wcsicmp(L
"install",
argv[
i] + 1) == 0)
243 else if (_wcsicmp(L
"remove",
argv[
i] + 1) == 0)
247 else if (_wcsicmp(L
"start",
argv[
i] + 1) == 0)
251 else if (argc >
i + 2 && _wcsicmp(L
"instance",
argv[
i] + 1) == 0)
253 if (_wcsicmp(L
"interactive",
argv[
i + 1]) == 0)
255 dispatchTable = dispatchTable_interactive;
262 L
"Invalid argument to -instance <%s>. Service not started.",
269 wprintf(L
"%ls -install to install the interactive service\n",
APPNAME);
271 L
"%ls -start [name] to start the service (name = \"interactive\") is optional\n",
273 wprintf(L
"%ls -remove to remove the service\n",
APPNAME);
275 wprintf(L
"\nService run-time parameters:\n");
276 wprintf(L
"-instance interactive <id>\n"
277 L
" Runs the service as an alternate instance.\n"
278 L
" The service settings will be loaded from\n"
279 L
" HKLM\\Software\\" _L(
280 PACKAGE_NAME) L
"<id> registry key, and the service will accept\n"
281 L
" requests on \\\\.\\pipe\\" _L(
282 PACKAGE) L
"<id>\\service named pipe.\n");
293 wprintf(L
"\nStartServiceCtrlDispatcher being called.\n");
294 wprintf(L
"This may take several seconds. Please wait.\n");
296 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]
int wmain(int argc, WCHAR *argv[])