Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
loaddrv.c File Reference
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "loaddrv.h"
+ Include dependency graph for loaddrv.c:

Go to the source code of this file.

Functions

void DisplayErrorText (DWORD dwLastError)
 
int exists (char *filename)
 
void usage (void)
 
int main (int argc, char *argv[])
 
DWORD LoadDriverInit (void)
 
void LoadDriverCleanup (void)
 
DWORD DriverInstall (LPSTR lpPath, LPSTR lpDriver)
 
DWORD DriverStart (LPSTR lpDriver)
 
DWORD DriverStop (LPSTR lpDriver)
 
DWORD DriverRemove (LPSTR lpDriver)
 
DWORD DriverStatus (LPSTR lpDriver)
 
DWORD DriverStartType (LPSTR lpDriver, DWORD dwStartType)
 

Variables

SC_HANDLE hSCMan = NULL
 

Function Documentation

◆ DisplayErrorText()

void DisplayErrorText ( DWORD  dwLastError)
20 {
21 LPSTR MessageBuffer;
22 DWORD dwBufferLength;
23
24 DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
25 FORMAT_MESSAGE_IGNORE_INSERTS |
26 FORMAT_MESSAGE_FROM_SYSTEM;
27
28 dwBufferLength = FormatMessageA(
29 dwFormatFlags,
30 NULL, // module to get message from (NULL == system)
31 dwLastError,
32 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
33 (LPSTR) &MessageBuffer,
34 0,
35 NULL
36 );
37 if (dwBufferLength) {
38 // Output message
39 puts(MessageBuffer);
40 // Free the buffer allocated by the system.
41 LocalFree(MessageBuffer);
42 }
43}

Referenced by main().

+ Here is the caller graph for this function:

◆ DriverInstall()

DWORD DriverInstall ( LPSTR  lpPath,
LPSTR  lpDriver 
)

195 {
196 BOOL dwStatus = OKAY;
197 SC_HANDLE hService = NULL;
198
199 // add to service control manager's database
200 if ((hService = CreateService(hSCMan, lpDriver,
201 lpDriver, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
202 SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, lpPath,
203 NULL, NULL, NULL, NULL, NULL)) == NULL)
204 dwStatus = GetLastError();
205 else CloseServiceHandle(hService);
206
207 return dwStatus;
208} // DriverInstall
SC_HANDLE hSCMan
Definition loaddrv.c:17
#define OKAY
Definition loaddrv.h:6

References hSCMan, and OKAY.

Referenced by main().

+ Here is the caller graph for this function:

◆ DriverRemove()

DWORD DriverRemove ( LPSTR  lpDriver)

251{
252 BOOL dwStatus = OKAY;
253 SC_HANDLE hService = NULL;
254
255 // get a handle to the service
256 if ((hService = OpenService(hSCMan, lpDriver,
257 SERVICE_ALL_ACCESS)) != NULL)
258 { // remove the driver
259 if (!DeleteService(hService))
260 dwStatus = GetLastError();
261 } else dwStatus = GetLastError();
262
263 if (hService != NULL) CloseServiceHandle(hService);
264 return dwStatus;
265} // DriverRemove

References hSCMan, and OKAY.

Referenced by main().

+ Here is the caller graph for this function:

◆ DriverStart()

DWORD DriverStart ( LPSTR  lpDriver)

211 {
212 BOOL dwStatus = OKAY;
213 SC_HANDLE hService = NULL;
214
215 // get a handle to the service
216 if ((hService = OpenService(hSCMan, lpDriver,
217 SERVICE_ALL_ACCESS)) != NULL)
218 {
219 // start the driver
220 if (!StartService(hService, 0, NULL))
221 dwStatus = GetLastError();
222 } else dwStatus = GetLastError();
223
224 if (hService != NULL) CloseServiceHandle(hService);
225 return dwStatus;
226} // DriverStart

References hSCMan, and OKAY.

Referenced by main().

+ Here is the caller graph for this function:

◆ DriverStartType()

DWORD DriverStartType ( LPSTR  lpDriver,
DWORD  dwStartType 
)

388 {
389 BOOL dwStatus = OKAY;
390 SC_HANDLE hService = NULL;
391
392 SC_LOCK sclLock;
393 LPQUERY_SERVICE_LOCK_STATUS lpqslsBuf;
394 DWORD dwBytesNeeded;
395
396 // Need to acquire database lock before reconfiguring.
397 sclLock = LockServiceDatabase(hSCMan);
398
399 // If the database cannot be locked, report the details.
400 if (sclLock == NULL) {
401 // Exit if the database is not locked by another process.
402 if (GetLastError() == ERROR_SERVICE_DATABASE_LOCKED) {
403
404 // Allocate a buffer to get details about the lock.
405 lpqslsBuf = (LPQUERY_SERVICE_LOCK_STATUS) LocalAlloc(
406 LPTR, sizeof(QUERY_SERVICE_LOCK_STATUS)+256);
407 if (lpqslsBuf != NULL) {
408 // Get and print the lock status information.
409 if (QueryServiceLockStatus(
410 hSCMan,
411 lpqslsBuf,
412 sizeof(QUERY_SERVICE_LOCK_STATUS)+256,
413 &dwBytesNeeded) )
414 {
415 if (lpqslsBuf->fIsLocked) {
416 printf("Locked by: %s, duration: %ld seconds\n",
417 lpqslsBuf->lpLockOwner,
418 lpqslsBuf->dwLockDuration
419 );
420 } else {
421 printf("No longer locked\n");
422 }
423 }
424 LocalFree(lpqslsBuf);
425 }
426 }
427 dwStatus = GetLastError();
428 } else {
429 // The database is locked, so it is safe to make changes.
430 // Open a handle to the service.
431 hService = OpenService(
432 hSCMan, // SCManager database
433 lpDriver, // name of service
434 SERVICE_CHANGE_CONFIG
435 ); // need CHANGE access
436 if (hService != NULL) {
437 // Make the changes.
438 if (!ChangeServiceConfig(
439 hService, // handle of service
440 SERVICE_NO_CHANGE, // service type: no change
441 dwStartType, // change service start type
442 SERVICE_NO_CHANGE, // error control: no change
443 NULL, // binary path: no change
444 NULL, // load order group: no change
445 NULL, // tag ID: no change
446 NULL, // dependencies: no change
447 NULL, // account name: no change
448 NULL, // password: no change
449 NULL) ) // display name: no change
450 {
451 dwStatus = GetLastError();
452 }
453 }
454 // Release the database lock.
455 UnlockServiceDatabase(sclLock);
456 }
457
458 if (hService != NULL) CloseServiceHandle(hService);
459 return dwStatus;
460} // DriverStartType

References hSCMan, and OKAY.

Referenced by main().

+ Here is the caller graph for this function:

◆ DriverStatus()

DWORD DriverStatus ( LPSTR  lpDriver)


270 {
271 BOOL dwStatus = OKAY;
272 SC_HANDLE hService = NULL;
273 DWORD dwBytesNeeded;
274
275 // get a handle to the service
276 if ((hService = OpenService(hSCMan, lpDriver,
277 SERVICE_ALL_ACCESS)) != NULL)
278 {
279 LPQUERY_SERVICE_CONFIG lpqscBuf;
280 //~ LPSERVICE_DESCRIPTION lpqscBuf2;
281 // Allocate a buffer for the configuration information.
282 if ((lpqscBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc(
283 LPTR, 4096)) != NULL)
284 {
285 //~ if ((lpqscBuf2 = (LPSERVICE_DESCRIPTION) LocalAlloc(
286 //~ LPTR, 4096)) != NULL)
287 {
288 // Get the configuration information.
289 if (QueryServiceConfig(
290 hService,
291 lpqscBuf,
292 4096,
293 &dwBytesNeeded) //&&
294 //~ QueryServiceConfig2(
295 //~ hService,
296 //~ SERVICE_CONFIG_DESCRIPTION,
297 //~ lpqscBuf2,
298 //~ 4096,
299 //~ &dwBytesNeeded
300 )
301 {
302 // Print the configuration information.
303 printf("Type: [0x%02lx] ", lpqscBuf->dwServiceType);
304 switch (lpqscBuf->dwServiceType) {
305 case SERVICE_WIN32_OWN_PROCESS:
306 printf("The service runs in its own process.");
307 break;
308 case SERVICE_WIN32_SHARE_PROCESS:
309 printf("The service shares a process with other services.");
310 break;
311 case SERVICE_KERNEL_DRIVER:
312 printf("Kernel driver.");
313 break;
314 case SERVICE_FILE_SYSTEM_DRIVER:
315 printf("File system driver.");
316 break;
317 case SERVICE_INTERACTIVE_PROCESS:
318 printf("The service can interact with the desktop.");
319 break;
320 default:
321 printf("Unknown type.");
322 }
323 printf("\nStart Type: [0x%02lx] ", lpqscBuf->dwStartType);
324 switch (lpqscBuf->dwStartType) {
325 case SERVICE_BOOT_START:
326 printf("Boot");
327 break;
328 case SERVICE_SYSTEM_START:
329 printf("System");
330 break;
331 case SERVICE_AUTO_START:
332 printf("Automatic");
333 break;
334 case SERVICE_DEMAND_START:
335 printf("Manual");
336 break;
337 case SERVICE_DISABLED:
338 printf("Disabled");
339 break;
340 default:
341 printf("Unknown.");
342 }
343 printf("\nError Control: [0x%02lx] ", lpqscBuf->dwErrorControl);
344 switch (lpqscBuf->dwErrorControl) {
345 case SERVICE_ERROR_IGNORE:
346 printf("IGNORE: Ignore.");
347 break;
348 case SERVICE_ERROR_NORMAL:
349 printf("NORMAL: Display a message box.");
350 break;
351 case SERVICE_ERROR_SEVERE:
352 printf("SEVERE: Restart with last-known-good config.");
353 break;
354 case SERVICE_ERROR_CRITICAL:
355 printf("CRITICAL: Restart w/ last-known-good config.");
356 break;
357 default:
358 printf("Unknown.");
359 }
360 printf("\nBinary path: %s\n", lpqscBuf->lpBinaryPathName);
361
362 if (lpqscBuf->lpLoadOrderGroup != NULL)
363 printf("Load order grp: %s\n", lpqscBuf->lpLoadOrderGroup);
364 if (lpqscBuf->dwTagId != 0)
365 printf("Tag ID: %ld\n", lpqscBuf->dwTagId);
366 if (lpqscBuf->lpDependencies != NULL)
367 printf("Dependencies: %s\n", lpqscBuf->lpDependencies);
368 if (lpqscBuf->lpServiceStartName != NULL)
369 printf("Start Name: %s\n", lpqscBuf->lpServiceStartName);
370 //~ if (lpqscBuf2->lpDescription != NULL)
371 //~ printf("Description: %s\n", lpqscBuf2->lpDescription);
372 }
373 //~ LocalFree(lpqscBuf2);
374 }
375 LocalFree(lpqscBuf);
376 } else {
377 dwStatus = GetLastError();
378 }
379 } else {
380 dwStatus = GetLastError();
381 }
382
383 if (hService != NULL) CloseServiceHandle(hService);
384 return dwStatus;
385} // DriverStatus

References hSCMan, and OKAY.

Referenced by main().

+ Here is the caller graph for this function:

◆ DriverStop()

DWORD DriverStop ( LPSTR  lpDriver)

230{
231 BOOL dwStatus = OKAY;
232 SC_HANDLE hService = NULL;
233 SERVICE_STATUS serviceStatus;
234
235 // get a handle to the service
236 if ((hService = OpenService(hSCMan, lpDriver,
237 SERVICE_ALL_ACCESS)) != NULL)
238 {
239 // stop the driver
240 if (!ControlService(hService, SERVICE_CONTROL_STOP,
241 &serviceStatus))
242 dwStatus = GetLastError();
243 } else dwStatus = GetLastError();
244
245 if (hService != NULL) CloseServiceHandle(hService);
246 return dwStatus;
247} // DriverStop

References hSCMan, and OKAY.

Referenced by main().

+ Here is the caller graph for this function:

◆ exists()

int exists ( char *  filename)
45 {
46 FILE * pFile;
47 pFile = fopen(filename, "r");
48 return pFile != NULL;
49}

Referenced by Slic3r::GUI::BedShapePanel::init_model_panel(), Slic3r::GUI::BedShapePanel::init_texture_panel(), main(), and Slic3r::PresetUpdater::priv::sync_config().

+ Here is the caller graph for this function:

◆ LoadDriverCleanup()

void LoadDriverCleanup ( void  )
190 {
191 if (hSCMan != NULL) CloseServiceHandle(hSCMan);
192}

References hSCMan.

Referenced by main().

+ Here is the caller graph for this function:

◆ LoadDriverInit()

DWORD LoadDriverInit ( void  )
181 {
182 // connect to local service control manager
183 if ((hSCMan = OpenSCManager(NULL, NULL,
184 SC_MANAGER_ALL_ACCESS)) == NULL) {
185 return -1;
186 }
187 return OKAY;
188}

References hSCMan, and OKAY.

Referenced by main().

+ Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *  argv[] 
)
73 {
74 DWORD status = 0;
75 int level = 0;
76 if (argc < 3) {
77 usage();
78 exit(1);
79 }
81 if (strcmp(argv[1], "start") == 0) {
82 printf("starting %s... ", argv[2]);
83 status = DriverStart(argv[2]);
84 if ( status != OKAY) {
85 printf("start failed (status %ld):\n", status);
86 level = 1;
87 } else {
88 printf("ok.\n");
89 }
90 } else if (strcmp(argv[1], "stop") == 0) {
91 printf("stoping %s... ", argv[2]);
92 status = DriverStop(argv[2]);
93 if ( status != OKAY) {
94 printf("stop failed (status %ld):\n", status);
95 level = 1;
96 } else {
97 printf("ok.\n");
98 }
99 } else if (strcmp(argv[1], "install") == 0) {
100 char path[MAX_PATH*2];
101 if (argc<4) {
102 char cwd[MAX_PATH];
103 getcwd(cwd, sizeof cwd);
104 sprintf(path, "%s\\%s.sys", cwd, argv[2]);
105 } else {
106 strncpy(path, argv[3], MAX_PATH);
107 }
108 if (exists(path)) {
109 printf("installing %s from %s... ", argv[2], path);
110 status = DriverInstall(path, argv[2]);
111 if ( status != OKAY) {
112 printf("install failed (status %ld):\n", status);
113 level = 2;
114 } else {
115 printf("ok.\n");
116 }
117 } else {
118 printf("install failed, file not found: %s\n", path);
119 level = 1;
120 }
121 } else if (strcmp(argv[1], "remove") == 0) {
122 printf("removing %s... ", argv[2]);
123 status = DriverRemove(argv[2]);
124 if ( status != OKAY) {
125 printf("remove failed (status %ld):\n", status);
126 level = 1;
127 } else {
128 printf("ok.\n");
129 }
130 } else if (strcmp(argv[1], "status") == 0) {
131 printf("status of %s:\n", argv[2]);
132 status = DriverStatus(argv[2]);
133 if ( status != OKAY) {
134 printf("stat failed (status %ld):\n", status);
135 level = 1;
136 } else {
137 printf("ok.\n");
138 }
139 } else if (strcmp(argv[1], "starttype") == 0) {
140 if (argc < 4) {
141 printf("Error: need start type (string) as argument.\n");
142 level = 2;
143 } else {
144 DWORD type = 0;
145 printf("set start type of %s to %s... ", argv[2], argv[3]);
146 if (strcmp(argv[1], "boot") == 0) {
147 type = SERVICE_BOOT_START;
148 } else if (strcmp(argv[3], "system") == 0) {
149 type = SERVICE_SYSTEM_START;
150 } else if (strcmp(argv[3], "auto") == 0) {
151 type = SERVICE_AUTO_START;
152 } else if (strcmp(argv[3], "manual") == 0) {
153 type = SERVICE_DEMAND_START;
154 } else if (strcmp(argv[3], "disabled") == 0) {
155 type = SERVICE_DISABLED;
156 } else {
157 printf("unknown type\n");
158 level = 1;
159 }
160 if (level == 0) {
161 status = DriverStartType(argv[2], type);
162 if ( status != OKAY) {
163 printf("set start type failed (status %ld):\n", status);
164 level = 1;
165 } else {
166 printf("ok.\n");
167 }
168 }
169 }
170 } else {
171 usage();
172 level = 1;
173 }
174 if (status) DisplayErrorText(status);
176 exit(level);
177 return 0;
178}
DWORD DriverStartType(LPSTR lpDriver, DWORD dwStartType)
Definition loaddrv.c:388
DWORD DriverInstall(LPSTR lpPath, LPSTR lpDriver)
Definition loaddrv.c:195
DWORD LoadDriverInit(void)
Definition loaddrv.c:181
DWORD DriverStatus(LPSTR lpDriver)
Definition loaddrv.c:270
void DisplayErrorText(DWORD dwLastError)
Definition loaddrv.c:20
DWORD DriverRemove(LPSTR lpDriver)
Definition loaddrv.c:250
DWORD DriverStart(LPSTR lpDriver)
Definition loaddrv.c:211
void LoadDriverCleanup(void)
Definition loaddrv.c:190
void usage(void)
Definition loaddrv.c:51
DWORD DriverStop(LPSTR lpDriver)
Definition loaddrv.c:229
int exists(char *filename)
Definition loaddrv.c:45
#define getcwd
Definition unistd.h:44

References DisplayErrorText(), DriverInstall(), DriverRemove(), DriverStart(), DriverStartType(), DriverStatus(), DriverStop(), exists(), getcwd, LoadDriverCleanup(), LoadDriverInit(), OKAY, and usage().

+ Here is the call graph for this function:

◆ usage()

void usage ( void  )
51 {
52 printf("USGAE: loaddrv command drivername [args...]\n\n"
53 "NT/2k/XP Driver and Service modification tool.\n"
54 "(C)2002 Chris Liechti <cliechti@gmx.net>\n\n"
55 "Suported commands:\n\n"
56 " install [fullpathforinstall]\n"
57 " Install new service. Loaded from given path. If path is not present,\n"
58 " the local directory is searched for a .sys file. If the service\n"
59 " already exists, it must be removed first.\n"
60 " start\n"
61 " Start service. It must be installed in advance.\n"
62 " stop\n"
63 " Stop service.\n"
64 " remove\n"
65 " Remove service. It must be stopped in advance.\n"
66 " status\n"
67 " Show status information about service.\n"
68 " starttype auto|manual|system|disable\n"
69 " Change startup type to the given type.\n"
70 );
71}

Referenced by main().

+ Here is the caller graph for this function:

Variable Documentation

◆ hSCMan