]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/MAI/bios_emulator/scitech/src/pm/win32/ntservc.c
* Patch by Thomas Frieden, 13 Nov 2002:
[karo-tx-uboot.git] / board / MAI / bios_emulator / scitech / src / pm / win32 / ntservc.c
1 /****************************************************************************
2 *
3 *                         SciTech Display Doctor
4 *
5 *               Copyright (C) 1991-2001 SciTech Software, Inc.
6 *                            All rights reserved.
7 *
8 *  ======================================================================
9 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
10 *  |                                                                    |
11 *  |This copyrighted computer code is a proprietary trade secret of     |
12 *  |SciTech Software, Inc., located at 505 Wall Street, Chico, CA 95928 |
13 *  |USA (www.scitechsoft.com).  ANY UNAUTHORIZED POSSESSION, USE,       |
14 *  |VIEWING, COPYING, MODIFICATION OR DISSEMINATION OF THIS CODE IS     |
15 *  |STRICTLY PROHIBITED BY LAW.  Unless you have current, express       |
16 *  |written authorization from SciTech to possess or use this code, you |
17 *  |may be subject to civil and/or criminal penalties.                  |
18 *  |                                                                    |
19 *  |If you received this code in error or you would like to report      |
20 *  |improper use, please immediately contact SciTech Software, Inc. at  |
21 *  |530-894-8400.                                                       |
22 *  |                                                                    |
23 *  |REMOVAL OR MODIFICATION OF THIS HEADER IS STRICTLY PROHIBITED BY LAW|
24 *  ======================================================================
25 *
26 * Language:     ANSI C
27 * Environment:  Windows NT, Windows 2K or Windows XP.
28 *
29 * Description:  Main module to do the installation of the SDD and GLDirect
30 *               device driver components under Windows NT/2K/XP.
31 *
32 ****************************************************************************/
33
34 #include "pmapi.h"
35 #include "win32/oshdr.h"
36
37 /*----------------------------- Implementation ----------------------------*/
38
39 /****************************************************************************
40 PARAMETERS:
41 szDriverName    - Actual name of the driver to install in the system
42 szServiceName   - Name of the service to create
43 szLoadGroup     - Load group for the driver (NULL for normal drivers)
44 dwServiceType   - Service type to create
45
46 RETURNS:
47 True on success, false on failure.
48
49 REMARKS:
50 This function does all the work to install the driver into the system.
51 The driver is not however activated; for that you must use the Start_SddFilt
52 function.
53 ****************************************************************************/
54 ulong PMAPI PM_installService(
55     const char *szDriverName,
56     const char *szServiceName,
57     const char *szLoadGroup,
58     ulong dwServiceType)
59 {
60     SC_HANDLE   scmHandle;
61     SC_HANDLE   driverHandle;
62     char        szDriverPath[MAX_PATH];
63     HKEY        key;
64     char        keyPath[MAX_PATH];
65     ulong       status;
66
67     // Obtain a handle to the service control manager requesting all access
68     if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
69         return GetLastError();
70
71     // Find the path to the driver in system directory
72     GetSystemDirectory(szDriverPath, sizeof(szDriverPath));
73     strcat(szDriverPath, "\\drivers\\");
74     strcat(szDriverPath, szDriverName);
75
76     // Create the service with the Service Control Manager.
77     driverHandle = CreateService(scmHandle,
78                                  szServiceName,
79                                  szServiceName,
80                                  SERVICE_ALL_ACCESS,
81                                  dwServiceType,
82                                  SERVICE_BOOT_START,
83                                  SERVICE_ERROR_NORMAL,
84                                  szDriverPath,
85                                  szLoadGroup,
86                                  NULL,
87                                  NULL,
88                                  NULL,
89                                  NULL);
90
91     // Check to see if the driver could actually be installed.
92     if (!driverHandle) {
93         status = GetLastError();
94         CloseServiceHandle(scmHandle);
95         return status;
96         }
97
98     // Get a handle to the key for driver so that it can be altered in the
99     // next step.
100     strcpy(keyPath, "SYSTEM\\CurrentControlSet\\Services\\");
101     strcat(keyPath, szServiceName);
102     if ((status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyPath,0,KEY_ALL_ACCESS,&key)) != ERROR_SUCCESS) {
103         // A problem has occured. Delete the service so that it is not installed.
104         status = GetLastError();
105         DeleteService(driverHandle);
106         CloseServiceHandle(driverHandle);
107         CloseServiceHandle(scmHandle);
108         return status;
109         }
110
111     // Delete the ImagePath value in the newly created key so that the
112     // system looks for the driver in the normal location.
113     if ((status = RegDeleteValue(key, "ImagePath")) != ERROR_SUCCESS) {
114         // A problem has occurred. Delete the service so that it is not
115         // installed and will not try to start.
116         RegCloseKey(key);
117         DeleteService(driverHandle);
118         CloseServiceHandle(driverHandle);
119         CloseServiceHandle(scmHandle);
120         return status;
121         }
122
123     // Clean up and exit
124     RegCloseKey(key);
125     CloseServiceHandle(driverHandle);
126     CloseServiceHandle(scmHandle);
127     return ERROR_SUCCESS;
128 }
129
130 /****************************************************************************
131 PARAMETERS:
132 szServiceName   - Name of the service to start
133
134 RETURNS:
135 True on success, false on failure.
136
137 REMARKS:
138 This function is used to start the specified service and make it active.
139 ****************************************************************************/
140 ulong PMAPI PM_startService(
141     const char *szServiceName)
142 {
143     SC_HANDLE       scmHandle;
144     SC_HANDLE       driverHandle;
145     SERVICE_STATUS      serviceStatus;
146     ulong           status;
147
148     // Obtain a handle to the service control manager requesting all access
149     if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
150         return GetLastError();
151
152     // Open the service with the Service Control Manager.
153     if ((driverHandle = OpenService(scmHandle,szServiceName,SERVICE_ALL_ACCESS)) == NULL) {
154         status = GetLastError();
155         CloseServiceHandle(scmHandle);
156         return status;
157         }
158
159     // Start the service
160     if (!StartService(driverHandle,0,NULL)) {
161         status = GetLastError();
162         CloseServiceHandle(driverHandle);
163         CloseServiceHandle(scmHandle);
164         return status;
165         }
166
167     // Query the service to make sure it is there
168     if (!QueryServiceStatus(driverHandle,&serviceStatus)) {     
169         status = GetLastError();
170         CloseServiceHandle(driverHandle);
171         CloseServiceHandle(scmHandle);
172         return status;
173         }
174     CloseServiceHandle(driverHandle);
175     CloseServiceHandle(scmHandle);
176     return ERROR_SUCCESS;
177 }
178
179 /****************************************************************************
180 PARAMETERS:
181 szServiceName   - Name of the service to stop
182
183 RETURNS:
184 True on success, false on failure.
185
186 REMARKS:
187 This function is used to stop the specified service and disable it.
188 ****************************************************************************/
189 ulong PMAPI PM_stopService(
190     const char *szServiceName)
191 {
192     SC_HANDLE       scmHandle;
193     SC_HANDLE       driverHandle;
194     SERVICE_STATUS      serviceStatus;
195     ulong           status;
196
197     // Obtain a handle to the service control manager requesting all access
198     if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
199         return GetLastError();
200
201     // Open the service with the Service Control Manager.
202     if ((driverHandle = OpenService(scmHandle,szServiceName,SERVICE_ALL_ACCESS)) == NULL) {
203         status = GetLastError();
204         CloseServiceHandle(scmHandle);
205         return status;
206         }
207
208     // Stop the service from running
209     if (!ControlService(driverHandle, SERVICE_CONTROL_STOP, &serviceStatus)) {
210         status = GetLastError();
211         CloseServiceHandle(driverHandle);
212         CloseServiceHandle(scmHandle);
213         return status;
214         }
215     CloseServiceHandle(driverHandle);
216     CloseServiceHandle(scmHandle);
217     return ERROR_SUCCESS;
218 }
219
220 /****************************************************************************
221 PARAMETERS:
222 szServiceName   - Name of the service to remove
223
224 RETURNS:
225 True on success, false on failure.
226
227 REMARKS:
228 This function is used to remove a service completely from the system.
229 ****************************************************************************/
230 ulong PMAPI PM_removeService(
231     const char *szServiceName)
232 {
233     SC_HANDLE   scmHandle;
234     SC_HANDLE   driverHandle;
235     ulong       status;
236
237     // Obtain a handle to the service control manager requesting all access
238     if ((scmHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS)) == NULL)
239         return GetLastError();
240
241     // Open the service with the Service Control Manager.
242     if ((driverHandle = OpenService(scmHandle,szServiceName,SERVICE_ALL_ACCESS)) == NULL) {
243         status = GetLastError();
244         CloseServiceHandle(scmHandle);
245         return status;
246         }
247
248     // Remove the service
249     if (!DeleteService(driverHandle)) {
250         status = GetLastError();
251         CloseServiceHandle(driverHandle);
252         CloseServiceHandle(scmHandle);
253         return status;
254         }
255     CloseServiceHandle(driverHandle);
256     CloseServiceHandle(scmHandle);
257     return ERROR_SUCCESS;
258 }
259