summaryrefslogtreecommitdiff
path: root/pyparallel/src/win32/loaddrv.c
blob: 51498775c1b959c2f79451dc3a769c212ef1d410 (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
// loaddrv.c - Dynamic driver install/start/stop/remove
// original by Paula Tomlinson

#include <windows.h>
#include "loaddrv.h"

SC_HANDLE hSCMan = NULL;

DWORD LoadDriverInit(void) {
    // connect to local service control manager
    if ((hSCMan = OpenSCManager(NULL, NULL, 
        SC_MANAGER_ALL_ACCESS)) == NULL) {
        return -1;
    }
    return OKAY;
}

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

DWORD DriverInstall(LPSTR lpPath, LPSTR lpDriver) {
   BOOL dwStatus = OKAY;
   SC_HANDLE hService = NULL;

   // add to service control manager's database
   if ((hService = CreateService(hSCMan, lpDriver, 
      lpDriver, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
      SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, lpPath, 
      NULL, NULL, NULL, NULL, NULL)) == NULL) 
   {
         dwStatus = GetLastError();
   } else {
       CloseServiceHandle(hService);
   }

   return dwStatus;
}

DWORD DriverStart(LPSTR lpDriver) {
   BOOL dwStatus = OKAY;
   SC_HANDLE hService = NULL;

   // get a handle to the service
   if ((hService = OpenService(hSCMan, lpDriver, 
      SERVICE_ALL_ACCESS)) != NULL) 
   {
      // start the driver
      if (!StartService(hService, 0, NULL))
         dwStatus = GetLastError();
   } else {
       dwStatus = GetLastError();
   }

   if (hService != NULL) {
       CloseServiceHandle(hService);
   }
   return dwStatus;
}

DWORD DriverStop(LPSTR lpDriver) {
   BOOL dwStatus = OKAY;
   SC_HANDLE hService = NULL;
   SERVICE_STATUS serviceStatus;

   // get a handle to the service
   if ((hService = OpenService(hSCMan, lpDriver, 
      SERVICE_ALL_ACCESS)) != NULL) 
   {
      // stop the driver
      if (!ControlService(hService, SERVICE_CONTROL_STOP,
         &serviceStatus))
            dwStatus = GetLastError();
   } else {
       dwStatus = GetLastError();
   }

   if (hService != NULL) {
       CloseServiceHandle(hService);
   }
   return dwStatus;
}

DWORD DriverRemove(LPSTR lpDriver) {
   BOOL dwStatus = OKAY;
   SC_HANDLE hService = NULL;

   // get a handle to the service
   if ((hService = OpenService(hSCMan, lpDriver, 
      SERVICE_ALL_ACCESS)) != NULL) 
   {
      // remove the driver
      if (!DeleteService(hService))
         dwStatus = GetLastError();
   } else {
       dwStatus = GetLastError();
   }

   if (hService != NULL) {
       CloseServiceHandle(hService);
   }
   return dwStatus;
}