1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2006 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
7
// This library is free software; you can redistribute it and/or //
8
// modify it under the terms of the GNU Lesser General Public //
9
// License as published by the Free Software Foundation; either //
10
// version 2.1 of the License, or (at your option) any later //
13
// This library is distributed in the hope that it will be //
14
// useful, but WITHOUT ANY WARRANTY; without even the implied //
15
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
16
// PURPOSE. See the GNU Lesser General Public License for more //
19
// You should have received a copy of the GNU Lesser General //
20
// Public License along with this library; if not, write to the //
21
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, //
22
// Boston, MA 02111-1307, USA. //
24
// Author contact: P.H.Welch@ukc.ac.uk //
27
//////////////////////////////////////////////////////////////////////
29
#include "svcclasses.h"
33
CServiceManager::CServiceManager(TCHAR *machine) {
34
schSCManager = OpenSCManager(machine, NULL, SC_MANAGER_ALL_ACCESS);
37
CServiceManager::~CServiceManager(void) {
38
if (schSCManager) CloseServiceHandle(schSCManager);
41
int CServiceManager::ok(void) {
42
return (schSCManager != NULL);
45
CService::CService(CServiceManager *mgr, TCHAR *name) {
46
schService = OpenService(schSCManager = mgr->schSCManager, name, SERVICE_ALL_ACCESS);
49
CService::~CService(void) {
50
if (schService) CloseServiceHandle(schService);
53
int CService::ok(void) {
54
return (schService != NULL);
57
void CService::install(TCHAR *name, TCHAR *disp, TCHAR *path) {
58
if (schService) CloseServiceHandle(schService);
59
schService = CreateService(
60
schSCManager, // SCManager database
61
name, // name of service
62
disp, // name to display
63
SERVICE_ALL_ACCESS, // desired access
64
SERVICE_WIN32_OWN_PROCESS, // service type
65
SERVICE_AUTO_START, // start type
66
SERVICE_ERROR_NORMAL, // error control type
67
path, // service's binary
68
NULL, // no load ordering group
69
NULL, // no tag identifier
71
NULL, // LocalSystem account
75
int CService::destroy(void) {
76
return DeleteService(schService);
79
int CService::getState(void) {
81
if (!QueryServiceStatus(schService, &sta)) return 0;
82
return sta.dwCurrentState;
85
int CService::isStarted(void) { return (getState() == SERVICE_RUNNING); }
87
int CService::isStopped(void) { return (getState() == SERVICE_STOPPED); }
89
void CService::start(void) {
103
StartService(schService, 0, NULL);
106
void CService::stop(void) {
108
ControlService(schService, SERVICE_CONTROL_STOP, &sta);
111
static SERVICE_STATUS sta;
112
static SERVICE_STATUS_HANDLE hsta;
113
static CMyService *service = NULL;
114
static TCHAR *service_name = NULL;
116
static void setstate(int s) {
117
if ((s == SERVICE_RUNNING) || (s == SERVICE_STOPPED)) {
118
sta.dwCheckPoint = 0;
122
sta.dwCurrentState = s;
123
SetServiceStatus(hsta, &sta);
126
static void WINAPI ctrlproc(DWORD ctrl) {
127
if (ctrl == SERVICE_CONTROL_STOP) {
128
setstate(SERVICE_STOP_PENDING);
131
setstate(sta.dwCurrentState);
135
static void WINAPI startproc(int argc, TCHAR **argv) {
136
hsta = RegisterServiceCtrlHandler(service_name, ctrlproc);
137
sta.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
138
sta.dwServiceSpecificExitCode = 0;
139
sta.dwControlsAccepted = SERVICE_ACCEPT_STOP;
140
sta.dwWin32ExitCode = 0;
141
sta.dwWaitHint = 3000;
142
sta.dwCheckPoint = 0;
143
setstate(SERVICE_RUNNING);
144
service->start(argc, argv);
145
setstate(SERVICE_STOPPED);
148
static SERVICE_TABLE_ENTRY dispatch[2] = { { NULL, (LPSERVICE_MAIN_FUNCTION)startproc }, { NULL, NULL } };
150
void ntservice_schedule (TCHAR *name, CMyService *svc) {
151
service_name = dispatch[0].lpServiceName = strdup(name);
153
StartServiceCtrlDispatcher (dispatch);