summaryrefslogtreecommitdiff
path: root/pyparallel/src/win32/loaddrv_console/loaddrv.c
blob: 9024ce2b7567aeb6bc71a18047f496e2c3316f3f (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
// loaddrv.c - Dynamic driver install/start/stop/remove
// based on Paula Tomlinson's LOADDRV program. 
// She describes it in her May 1995 article in Windows/DOS Developer's
// Journal (now Windows Developer's Journal).
// i removed the old/ugly dialog, it now accepts command line options and
// prints error messages with textual description from the OS.

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "loaddrv.h"

// globals
SC_HANDLE hSCMan = NULL;

//get ext messages for windows error codes:
void DisplayErrorText(DWORD dwLastError) {
    LPSTR MessageBuffer;
    DWORD dwBufferLength;
    
    DWORD dwFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_IGNORE_INSERTS |
        FORMAT_MESSAGE_FROM_SYSTEM ;
    
    if (dwBufferLength = FormatMessageA(
        dwFormatFlags,
        NULL, // module to get message from (NULL == system)
        dwLastError,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // default language
        (LPSTR) &MessageBuffer,
        0,
        NULL
        ))
    {
        // Output message
        puts(MessageBuffer);
        // Free the buffer allocated by the system.
        LocalFree(MessageBuffer);
    }
}

int main(int argc, char *argv[]) {
    DWORD status = 0;
    if (argc < 3) {
        printf("USGAE: loaddrv start|stop|install|remove drivername [fullpathforinstall]");
        exit(1);
    }
    LoadDriverInit();
    if (strcmp(argv[1], "start") == 0) {
        printf("starting %s... ", argv[2]);
        status = DriverStart(argv[2]);
        if ( status != OKAY) {
            printf("start failed (status %ld):\n", status);
        } else {
            printf("ok.\n");
        }
    } else if (strcmp(argv[1], "stop") == 0) {
        printf("stoping %s... ", argv[2]);
        status = DriverStop(argv[2]);
        if ( status != OKAY) {
            printf("stop failed (status %ld):\n", status);
        } else {
            printf("ok.\n");
        }
    } else if (strcmp(argv[1], "install") == 0) {
        char path[MAX_PATH*2];
        if (argc<4) {
            char cwd[MAX_PATH];
            getcwd(cwd, sizeof cwd);
            sprintf(path, "%s\\%s.sys", cwd, argv[2]);
        } else {
            strncpy(path, argv[3], MAX_PATH);
        }
        printf("installing %s from %s... ", argv[2], path);
        status = DriverInstall(path, argv[2]);
        if ( status != OKAY) {
            printf("install failed (status %ld):\n", status);
        } else {
            printf("ok.\n");
        }
    } else if (strcmp(argv[1], "remove") == 0) {
        printf("removing %s... ", argv[2]);
        status = DriverRemove(argv[2]);
        if ( status != OKAY) {
            printf("remove failed (status %ld):\n", status);
        } else {
            printf("ok.\n");
        }
    } else {
        printf("USGAE: loaddrv start|stop|install|remove drivername [fullpathforinstall]");
        exit(1);
    }
    if (status) DisplayErrorText(status);
    LoadDriverCleanup();
    return 0;
}


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;
} // DriverInstall

/**-----------------------------------------------------**/
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;
} // DriverStart

/**-----------------------------------------------------**/
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;
} // DriverStop

/**-----------------------------------------------------**/
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;
} // DriverRemove