-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdriver_operation.c
More file actions
153 lines (133 loc) · 4.04 KB
/
Copy pathdriver_operation.c
File metadata and controls
153 lines (133 loc) · 4.04 KB
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
#include "driver_loader.h"
/*
Purpose: Create the driver service
Args: ServiceName: The name of the service
. ExecutablePath: File path of the driver file
Return: 0:Failed; 1: Succeed
*/
int Create_Service(const char *ServiceName, const char *ExecutablePath) {
printf("Creating service: %s\n", ServiceName);
SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //Open a handle to Service Manager
if (sh == INVALID_HANDLE_VALUE) {
printf("OpenSCManager() failed!\n");
return 0;
}
SC_HANDLE hService = CreateService(sh, ServiceName, ServiceName, //Create the driver service
SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
ExecutablePath, NULL, NULL, NULL, NULL, NULL);
CloseServiceHandle(sh); //Use this instead of CloseHandle()
if (hService == NULL) {
printf("CreateService() failed!\n");
return 0;
}
CloseServiceHandle(hService);
return 1;
}
/*
Purpose: Start the specified service
Args: ServiceName: Name of the service
Return: 0: Failed; 1: Succeed
*/
int Start_Service(const char *ServiceName) {
SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //Open a handle to Service Manager
if (sh == INVALID_HANDLE_VALUE) {
printf("OpenSCManager() failed!\n");
return 0;
}
SC_HANDLE hService = OpenService(sh, ServiceName, SERVICE_ALL_ACCESS); //Get a handle to the driver service
CloseServiceHandle(sh);
if (hService == NULL) {
printf("OpenService() failed!\n");
return 0;
}
if (StartService(hService, 0, NULL) == 0) {
int err_code = (int)GetLastError();
printf("StartService() failed: %i\n", err_code);
return 0;
}
CloseServiceHandle(hService);
return 1;
}
/*
Purpose: Stop the specified service
Args: ServiceName: Name of the service
Return: 0: Failed; 1: Succeed
*/
int Stop_Service(const char *ServiceName) {
SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //Open a handle to Service Manager
if (sh == INVALID_HANDLE_VALUE) {
printf("OpenSCManager() failed!\n");
return 0;
}
SC_HANDLE hService = OpenService(sh, ServiceName, SERVICE_ALL_ACCESS); //Get a handle to the driver service
CloseServiceHandle(sh);
if (hService == NULL) {
printf("OpenService() failed!\n");
return 0;
}
SERVICE_STATUS ss;
if (ControlService(hService, SERVICE_CONTROL_STOP, &ss) == 0) {
CloseServiceHandle(hService);
return 0;
}
CloseServiceHandle(hService);
return 1;
}
/*
Purpose: Delete the specified service
Args: ServiceName: Name of the service
Return: 0: Failed; 1: Succeed
*/
int Delete_Service(const char *ServiceName) {
SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //Open a handle to Service Manager
if (sh == INVALID_HANDLE_VALUE) {
printf("OpenSCManager() failed!\n");
return 0;
}
SC_HANDLE hService = OpenService(sh, ServiceName, SERVICE_ALL_ACCESS); //Get a handle to the driver service
CloseServiceHandle(sh);
if (hService == NULL) {
printf("OpenService() failed!\n");
return 0;
}
if (DeleteService(hService) == 0) {
CloseServiceHandle(hService);
return 0;
}
CloseServiceHandle(hService);
return 1;
}
/*
Purpose: Get an IO handle
Args: DeviceName: Device name
Return: The IO handle. -1 if failed
Note: Remember to close the handle with Close_IO_Handle()
*/
HANDLE Get_IO_Handle(char *DeviceName) {
printf("Opening: %s\n", DeviceName);
return (HANDLE)CreateFile(DeviceName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
}
/*
Purpose: Close an IO handle
Args: hIO: The IO handle
*/
void Close_IO_Handle(HANDLE hIO) {
if (hIO != INVALID_HANDLE_VALUE) {
CloseHandle(hIO);
}
}
/*
Purpose: Write data to an IO handle
Args: hDevice: The IO handle
. Buffer: Data to write
. WriteSize: The length of data to write
Return: 0: failed; 1: succeed
*/
int Write_IO_Handle(HANDLE hDevice, const char *Buffer, const int WriteSize) {
if (WriteFile(hDevice, Buffer, WriteSize, NULL, NULL) != 0) {
return 1;
}
else {
return 0;
}
}