summaryrefslogtreecommitdiff
path: root/board/MAI/bios_emulator/scitech/src/pm/win32/ntservc.c
blob: a3324d33b84e1aa604129b485befa733e55d98a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/****************************************************************************
*
*                         SciTech Display Doctor
*
*               Copyright (C) 1991-2001 SciTech Software, Inc.
*                            All rights reserved.
*
*  ======================================================================
*  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
*  |                                                                    |
*  |This copyrighted computer code is a proprietary trade secret of     |
*  |SciTech Software, Inc., located at 505 Wall Street, Chico, CA 95928 |
*  |USA (www.scitechsoft.com).  ANY UNAUTHORIZED POSSESSION, USE,       |
*  |VIEWING, COPYING, MODIFICATION OR DISSEMINATION OF THIS CODE IS     |
*  |STRICTLY PROHIBITED BY LAW.  Unless you have current, express       |
*  |written authorization from SciTech to possess or use this code, you |
*  |may be subject to civil and/or criminal penalties.                  |
*  |                                                                    |
*  |If you received this code in error or you would like to report      |
*  |improper use, please immediately contact SciTech Software, Inc. at  |
*  |530-894-8400.                                                       |
*  |                                                                    |
*  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
*  ======================================================================
*
* Language:     ANSI C
* Environment:  Windows NT, Windows 2K or Windows XP.
*
* Description:  Main module to do the installation of the SDD and GLDirect
*               device driver components under Windows NT/2K/XP.
*
****************************************************************************/

#include "pmapi.h"
#include "win32/oshdr.h"

/*----------------------------- Implementation ----------------------------*/

/****************************************************************************
PARAMETERS:
szDriverName    - Actual name of the driver to install in the system
szServiceName   - Name of the service to create
szLoadGroup     - Load group for the driver (NULL for normal drivers)
dwServiceType   - Service type to create

RETURNS:
True on success, false on failure.

REMARKS:
This function does all the work to install the driver into the system.
The driver is not however activated; for that you must use the Start_SddFilt
function.
****************************************************************************/
ulong PMAPI PM_installService(
    const char *szDriverName,
    const char *szServiceName,
    const char *szLoadGroup,
    ulong dwServiceType)
{
    SC_HANDLE   scmHandle;
    SC_HANDLE   driverHandle;
    char        szDriverPath[MAX_PATH];
    HKEY        key;
    char        keyPath[MAX_PATH];
    ulong       status;

    // Obtain a handle to the service control manager requesting all access
    if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
        return GetLastError();

    // Find the path to the driver in system directory
    GetSystemDirectory(szDriverPath, sizeof(szDriverPath));
    strcat(szDriverPath, "\\drivers\\");
    strcat(szDriverPath, szDriverName);

    // Create the service with the Service Control Manager.
    driverHandle = CreateService(scmHandle,
                                 szServiceName,
                                 szServiceName,
                                 SERVICE_ALL_ACCESS,
                                 dwServiceType,
                                 SERVICE_BOOT_START,
                                 SERVICE_ERROR_NORMAL,
                                 szDriverPath,
                                 szLoadGroup,
                                 NULL,
                                 NULL,
                                 NULL,
                                 NULL);

    // Check to see if the driver could actually be installed.
    if (!driverHandle) {
        status = GetLastError();
        CloseServiceHandle(scmHandle);
        return status;
        }

    // Get a handle to the key for driver so that it can be altered in the
    // next step.
    strcpy(keyPath, "SYSTEM\\CurrentControlSet\\Services\\");
    strcat(keyPath, szServiceName);
    if ((status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyPath,0,KEY_ALL_ACCESS,&key)) != ERROR_SUCCESS) {
        // A problem has occured. Delete the service so that it is not installed.
        status = GetLastError();
        DeleteService(driverHandle);
        CloseServiceHandle(driverHandle);
        CloseServiceHandle(scmHandle);
        return status;
        }

    // Delete the ImagePath value in the newly created key so that the
    // system looks for the driver in the normal location.
    if ((status = RegDeleteValue(key, "ImagePath")) != ERROR_SUCCESS) {
        // A problem has occurred. Delete the service so that it is not
        // installed and will not try to start.
        RegCloseKey(key);
        DeleteService(driverHandle);
        CloseServiceHandle(driverHandle);
        CloseServiceHandle(scmHandle);
        return status;
        }

    // Clean up and exit
    RegCloseKey(key);
    CloseServiceHandle(driverHandle);
    CloseServiceHandle(scmHandle);
    return ERROR_SUCCESS;
}

/****************************************************************************
PARAMETERS:
szServiceName   - Name of the service to start

RETURNS:
True on success, false on failure.

REMARKS:
This function is used to start the specified service and make it active.
****************************************************************************/
ulong PMAPI PM_startService(
    const char *szServiceName)
{
    SC_HANDLE       scmHandle;
    SC_HANDLE       driverHandle;
    SERVICE_STATUS	serviceStatus;
    ulong           status;

    // Obtain a handle to the service control manager requesting all access
    if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
        return GetLastError();

    // Open the service with the Service Control Manager.
    if ((driverHandle = OpenService(scmHandle,szServiceName,SERVICE_ALL_ACCESS)) == NULL) {
        status = GetLastError();
        CloseServiceHandle(scmHandle);
        return status;
        }

    // Start the service
    if (!StartService(driverHandle,0,NULL)) {
        status = GetLastError();
        CloseServiceHandle(driverHandle);
        CloseServiceHandle(scmHandle);
        return status;
        }

    // Query the service to make sure it is there
    if (!QueryServiceStatus(driverHandle,&serviceStatus)) {	
        status = GetLastError();
        CloseServiceHandle(driverHandle);
        CloseServiceHandle(scmHandle);
        return status;
        }
    CloseServiceHandle(driverHandle);
    CloseServiceHandle(scmHandle);
    return ERROR_SUCCESS;
}

/****************************************************************************
PARAMETERS:
szServiceName   - Name of the service to stop

RETURNS:
True on success, false on failure.

REMARKS:
This function is used to stop the specified service and disable it.
****************************************************************************/
ulong PMAPI PM_stopService(
    const char *szServiceName)
{
    SC_HANDLE       scmHandle;
    SC_HANDLE       driverHandle;
    SERVICE_STATUS	serviceStatus;
    ulong           status;

    // Obtain a handle to the service control manager requesting all access
    if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
        return GetLastError();

    // Open the service with the Service Control Manager.
    if ((driverHandle = OpenService(scmHandle,szServiceName,SERVICE_ALL_ACCESS)) == NULL) {
        status = GetLastError();
        CloseServiceHandle(scmHandle);
        return status;
        }

    // Stop the service from running
    if (!ControlService(driverHandle, SERVICE_CONTROL_STOP, &serviceStatus)) {
        status = GetLastError();
        CloseServiceHandle(driverHandle);
        CloseServiceHandle(scmHandle);
        return status;
        }
    CloseServiceHandle(driverHandle);
    CloseServiceHandle(scmHandle);
    return ERROR_SUCCESS;
}

/****************************************************************************
PARAMETERS:
szServiceName   - Name of the service to remove

RETURNS:
True on success, false on failure.

REMARKS:
This function is used to remove a service completely from the system.
****************************************************************************/
ulong PMAPI PM_removeService(
    const char *szServiceName)
{
    SC_HANDLE   scmHandle;
    SC_HANDLE   driverHandle;
    ulong       status;

    // Obtain a handle to the service control manager requesting all access
    if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
        return GetLastError();

    // Open the service with the Service Control Manager.
    if ((driverHandle = OpenService(scmHandle,szServiceName,SERVICE_ALL_ACCESS)) == NULL) {
        status = GetLastError();
        CloseServiceHandle(scmHandle);
        return status;
        }

    // Remove the service
    if (!DeleteService(driverHandle)) {
        status = GetLastError();
        CloseServiceHandle(driverHandle);
        CloseServiceHandle(scmHandle);
        return status;
        }
    CloseServiceHandle(driverHandle);
    CloseServiceHandle(scmHandle);
    return ERROR_SUCCESS;
}