source: nscp/include/ServiceCmd.cpp @ a0528c4

0.4.00.4.10.4.2stable
Last change on this file since a0528c4 was a0528c4, checked in by Michael Medin <michael@…>, 8 years ago

Initial release

  • Property mode set to 100644
File size: 5.1 KB
Line 
1//////////////////////////////////////////////////////////////////////////
2// Service Helpers.
3//
4// Functions to stop/start/install and uninstall a service.
5//
6// Copyright (c) 2004 MySolutions NORDIC (http://www.medin.nu)
7//
8// Date: 2004-03-13
9// Author: Michael Medin (mickem@medin.nu)
10//
11// This software is provided "AS IS", without a warranty of any kind.
12// You are free to use/modify this code but leave this header intact.
13//
14//////////////////////////////////////////////////////////////////////////
15
16#include "stdafx.h"
17#include "ServiceCmd.h"
18
19namespace serviceControll {
20        /**
21         * Installs the service
22         *
23         * @param szName
24         * @param szDisplayName
25         * @param szDependencies
26         *
27         * @author mickem
28         *
29         * @date 03-13-2004
30         *
31         */
32        void Install(LPCTSTR szName, LPCTSTR szDisplayName, LPCTSTR szDependencies) {
33                SC_HANDLE   schService;
34                SC_HANDLE   schSCManager;
35                TCHAR szPath[512];
36
37                if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
38                        throw SCException("Could not get module");
39
40                schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
41                if (!schSCManager)
42                        throw SCException("OpenSCManager failed.");
43                schService = CreateService(
44                        schSCManager,               // SCManager database
45                        TEXT(szName),                           // name of service
46                        TEXT(szDisplayName),             // name to display
47                        SERVICE_ALL_ACCESS,         // desired access
48                        SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS ,  // service type
49                        SERVICE_DEMAND_START,       // start type
50                        SERVICE_ERROR_NORMAL,       // error control type
51                        szPath,                     // service's binary
52                        NULL,                       // no load ordering group
53                        NULL,                       // no tag identifier
54                        TEXT(szDependencies),       // dependencies
55                        NULL,                       // LocalSystem account
56                        NULL);                      // no password
57
58                if (!schService) {
59                        CloseServiceHandle(schSCManager);
60                        throw SCException("Unable to install service.");
61                }
62                CloseServiceHandle(schService);
63                CloseServiceHandle(schSCManager);
64        }
65
66        /**
67         * Stars the service.
68         *
69         * @param szName
70         *
71         * @author mickem
72         *
73         * @date 03-13-2004
74         *
75         */
76        void Start(std::string name) {
77                SC_HANDLE   schService;
78                SC_HANDLE   schSCManager;
79                SERVICE_STATUS ssStatus;
80
81                schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS );
82                if (!schSCManager)
83                        throw SCException("OpenSCManager failed.");
84                schService = OpenService(schSCManager, TEXT(name.c_str()), SERVICE_ALL_ACCESS);
85                if (schService) {
86                        // try to stop the service
87                        if ( StartService(schService,0,NULL) ) {
88                                std::cout << "Starting " << name;
89                                Sleep( 1000 );
90                                while( QueryServiceStatus( schService, &ssStatus ) ) {
91                                        if ( ssStatus.dwCurrentState == SERVICE_START_PENDING ) {
92                                                std::cout << ".";
93                                                Sleep( 1000 );
94                                        } else
95                                                break;
96                                }
97                                if ( ssStatus.dwCurrentState != SERVICE_RUNNING ) {
98                                        CloseServiceHandle(schService);
99                                        CloseServiceHandle(schSCManager);
100                                        throw SCException("Service failed to start.");
101                                }
102                        }
103                        CloseServiceHandle(schService);
104                } else {
105                        CloseServiceHandle(schSCManager);
106                        throw SCException("OpenService failed.");
107                }
108                CloseServiceHandle(schSCManager);
109        }
110
111        /**
112         * Stops and removes the service
113         *
114         * @param szName
115         *
116         * @author mickem
117         *
118         * @date 03-13-2004
119         *
120         */
121        void Uninstall(std::string name) {
122                SC_HANDLE   schService;
123                SC_HANDLE   schSCManager;
124                Stop(name);
125
126                schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
127                if (!schSCManager)
128                        throw SCException("OpenSCManager failed.");
129                schService = OpenService(schSCManager, TEXT(name.c_str()), SERVICE_ALL_ACCESS);
130                if (schService) {
131                        if(!DeleteService(schService)) {
132                                CloseServiceHandle(schService);
133                                CloseServiceHandle(schSCManager);
134                                throw SCException("DeleteService failed.");
135                        }
136                        CloseServiceHandle(schService);
137                } else {
138                        CloseServiceHandle(schSCManager);
139                        throw SCException("OpenService failed.");
140                }
141                CloseServiceHandle(schSCManager);
142        }
143
144        /**
145         * Stops the service
146         *
147         * @param szName
148         *
149         * @author MickeM
150         *
151         * @date 03-13-2004
152         *
153         */
154        void Stop(std::string name) {
155                SC_HANDLE   schService;
156                SC_HANDLE   schSCManager;
157                SERVICE_STATUS ssStatus;
158
159                schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
160                if (!schSCManager)
161                        throw SCException("OpenSCManager failed.");
162                schService = OpenService(schSCManager, TEXT(name.c_str()), SERVICE_ALL_ACCESS);
163                if (schService) {
164                        // try to stop the service
165                        if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) ) {
166                                std::cout << "Stopping service.";
167                                Sleep( 1000 );
168                                while( QueryServiceStatus( schService, &ssStatus ) ) {
169                                        if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING ) {
170                                                std::cout << ".";
171                                                Sleep( 1000 );
172                                        } else
173                                                break;
174                                }
175                                std::cout << std::endl;
176                                if ( ssStatus.dwCurrentState != SERVICE_STOPPED ) {
177                                        CloseServiceHandle(schService);
178                                        CloseServiceHandle(schSCManager);
179                                        throw SCException("Service failed to stop.");
180                                }
181                        }
182                        CloseServiceHandle(schService);
183                } else {
184                        CloseServiceHandle(schSCManager);
185                        throw SCException("OpenService failed.");
186                }
187                CloseServiceHandle(schSCManager);
188        }
189}
Note: See TracBrowser for help on using the repository browser.