如何用程序加载/卸载sys驱动 - 阿九
保存下 省得下次再找 sys文件跟程序放在同个目录下 如果生产的sys名为test.sys 那么调用方式就是 load_sysfile(“test”) 不需要加扩展名;卸载sys也是类似的调用过程, unload_sys(“tes”);
Code
1 bool load_sysfile(char *theDriverName)
2 {
3 char aPath[1024];
4 char aCurrentDirectory[515];
5 SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
6 if(!sh)
7 {
8 return false;
9 }
10 GetCurrentDirectory( 512, aCurrentDirectory);
11 _snprintf(aPath,
12 1022,
13 “%s\\%s.sys“,
14 aCurrentDirectory,
15 theDriverName);
16
17 SC_HANDLE rh = CreateService(sh,
18 theDriverName,
19 theDriverName,
20 SERVICE_ALL_ACCESS,
21 SERVICE_KERNEL_DRIVER,
22 SERVICE_DEMAND_START,
23 SERVICE_ERROR_NORMAL,
24 aPath,
25 NULL,
26 NULL,
27 NULL,
28 NULL,
29 NULL);
30 if(!rh)
31
32 {
33 if (GetLastError() == ERROR_SERVICE_EXISTS)
34 {
35 // service exists
36 rh = OpenService(sh,
37 theDriverName,
38 SERVICE_ALL_ACCESS);
39 if(!rh)
40 {
41 CloseServiceHandle(sh);
42 return false;
43 }
44 }
45 else
46 {
47 CloseServiceHandle(sh);
48 return false;
49 }
50 }
51 // start the drivers
52
53 if(rh)
54 {
55 if(0 == StartService(rh, 0, NULL))
56 {
57 if(ERROR_SERVICE_ALREADY_RUNNING == GetLastError())
58 {
59
60 // no real problem
61 }
62 else
63 {
64 CloseServiceHandle(sh);
65 CloseServiceHandle(rh);
66 return false;
67 }
68 }
69 CloseServiceHandle(sh);
70 CloseServiceHandle(rh);
71 }
72 return true;
73 }
74
1 bool load_sysfile(char *theDriverName)
2 {
3 char aPath[1024];
4 char aCurrentDirectory[515];
5 SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
6 if(!sh)
7 {
8 return false;
9 }
10 GetCurrentDirectory( 512, aCurrentDirectory);
11 _snprintf(aPath,
12 1022,
13 “%s\\%s.sys“,
14 aCurrentDirectory,
15 theDriverName);
16
17 SC_HANDLE rh = CreateService(sh,
18 theDriverName,
19 theDriverName,
20 SERVICE_ALL_ACCESS,
21 SERVICE_KERNEL_DRIVER,
22 SERVICE_DEMAND_START,
23 SERVICE_ERROR_NORMAL,
24 aPath,
25 NULL,
26 NULL,
27 NULL,
28 NULL,
29 NULL);
30 if(!rh)
31
32 {
33 if (GetLastError() == ERROR_SERVICE_EXISTS)
34 {
35 // service exists
36 rh = OpenService(sh,
37 theDriverName,
38 SERVICE_ALL_ACCESS);
39 if(!rh)
40 {
41 CloseServiceHandle(sh);
42 return false;
43 }
44 }
45 else
46 {
47 CloseServiceHandle(sh);
48 return false;
49 }
50 }
51 // start the drivers
52
53 if(rh)
54 {
55 if(0 == StartService(rh, 0, NULL))
56 {
57 if(ERROR_SERVICE_ALREADY_RUNNING == GetLastError())
58 {
59
60 // no real problem
61 }
62 else
63 {
64 CloseServiceHandle(sh);
65 CloseServiceHandle(rh);
66 return false;
67 }
68 }
69 CloseServiceHandle(sh);
70 CloseServiceHandle(rh);
71 }
72 return true;
73 }
74
Code
1 bool unload_sys(char* theDriverName){
2 char aPath[1024];
3 char aCurrentDirectory[515];
4 SERVICE_STATUS SvrSta;
5 SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
6 if(!sh)
7 {
8 return false;
9 }
10 GetCurrentDirectory( 512, aCurrentDirectory);
11 _snprintf(aPath,
12 1022,
13 “%s\\%s.sys“,
14 aCurrentDirectory,
15 theDriverName);
16 //MessageBox(NULL,aPath,””,0);
17
18 SC_HANDLE hService= OpenService(sh,theDriverName, SERVICE_STOP | DELETE);
19 if (hService==NULL){
20 CloseServiceHandle(sh);
21 return false;
22 }
23
24 if (!ControlService(hService, SERVICE_CONTROL_STOP, &SvrSta)){
25
26 }else{
27
28 }
29 DeleteService(hService);
30 CloseServiceHandle(hService);
31 CloseServiceHandle(sh);
32
33 return true;
34
35
36 }
1 bool unload_sys(char* theDriverName){
2 char aPath[1024];
3 char aCurrentDirectory[515];
4 SERVICE_STATUS SvrSta;
5 SC_HANDLE sh = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
6 if(!sh)
7 {
8 return false;
9 }
10 GetCurrentDirectory( 512, aCurrentDirectory);
11 _snprintf(aPath,
12 1022,
13 “%s\\%s.sys“,
14 aCurrentDirectory,
15 theDriverName);
16 //MessageBox(NULL,aPath,””,0);
17
18 SC_HANDLE hService= OpenService(sh,theDriverName, SERVICE_STOP | DELETE);
19 if (hService==NULL){
20 CloseServiceHandle(sh);
21 return false;
22 }
23
24 if (!ControlService(hService, SERVICE_CONTROL_STOP, &SvrSta)){
25
26 }else{
27
28 }
29 DeleteService(hService);
30 CloseServiceHandle(hService);
31 CloseServiceHandle(sh);
32
33 return true;
34
35
36 }
版权声明:本文为jokerfox原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。