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
|
/*
简单的驱动, 主要是感受下驱动的写法!这里是3环,安装端!
编译方法参见makefile.
*/
#include <stdio.h>
#include <windows.h>
#include <winsvc.h>
#include <conio.h>
#define DRIVER_NAME "TestNtDriver"
#define DRIVER_PATH "TestNtDriver.sys"
/*
装载驱动程序
pDriverName :驱动程序名称
pDriverPath :驱动程序路径
成功返回非0, 失败返回0
*/
int LoadNTDriver( char* pDriverName, char* pDriverPath ) {
SC_HANDLE hServiceMgr=NULL;
SC_HANDLE hServiceDDK=NULL;
char SzDriverPath[MAX_PATH];
DWORD dwRet = 0;
//得到完整的驱动路径
GetFullPathName( pDriverPath, 256, SzDriverPath, NULL);
do {
//打开SCM管理器
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( hServiceMgr == NULL ) {
break;
}
//创建驱动所对应的服务
hServiceDDK = CreateService( hServiceMgr,
pDriverName, // 驱动程序的在注册表中的名字
pDriverName, // 注册表驱动程序的 DisplayName 值
SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限
SERVICE_KERNEL_DRIVER, // 表示加载的服务是驱动程序
SERVICE_DEMAND_START, // 注册表驱动程序的 Start 值
SERVICE_ERROR_IGNORE, // 注册表驱动程序的 ErrorControl 值
SzDriverPath, // 注册表驱动程序的 ImagePath 值
NULL, NULL, NULL, NULL, NULL);
//如果服务安装失败
if( hServiceDDK == NULL ) {
dwRet = GetLastError();
//确实发生了错误
if ( dwRet != ERROR_IO_PENDING && dwRet != ERROR_SERVICE_EXISTS ) {
break;
}
//在其他情况下是因为该服务已经安装了. 这里只需要打开就可以
hServiceDDK = OpenService( hServiceMgr, pDriverName, SERVICE_ALL_ACCESS );
if ( !hServiceDDK ) {
break;
}
}
//开启此项服务
dwRet = StartService( hServiceDDK, 0, NULL );
} while ( FALSE );
if( hServiceDDK ) {
CloseServiceHandle( hServiceDDK );
}
if( hServiceMgr ) {
CloseServiceHandle( hServiceMgr );
}
return dwRet;
}
//卸载驱动程序
BOOL UnloadNTDriver( char * SzServiceName ) {
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
SC_HANDLE hServiceDDK=NULL;//NT驱动程序的服务句柄
SERVICE_STATUS SvrSta;
do {
//打开SCM管理器
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( hServiceMgr == NULL ) {
break;
}
//打开驱动所对应的服务
hServiceDDK = OpenService( hServiceMgr, SzServiceName, SERVICE_ALL_ACCESS );
if ( hServiceDDK == NULL ) {
break;
}
//停止驱动
ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta );
//删除驱动
if( DeleteService( hServiceDDK ) ) {
bRet = TRUE;
}
} while ( FALSE );
if( hServiceDDK ) {
CloseServiceHandle( hServiceDDK );
}
if( hServiceMgr ) {
CloseServiceHandle( hServiceMgr );
}
return bRet;
}
//============================================================================
int Jmain() {
int bRet;
HANDLE hDevice = INVALID_HANDLE_VALUE;
byte byBuf[1024] = {0};
DWORD dwByteRead;
DWORD dwByteWrite;
do {
//安装驱动
bRet = LoadNTDriver( DRIVER_NAME, DRIVER_PATH );
if ( bRet ) {
printf( "驱动安装成功!\n" );
}else {
printf( "驱动安装失败!\n" );
break;
}
hDevice = CreateFile("\\\\.\\LinkJoenDevice",GENERIC_READ | GENERIC_WRITE,0,
NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL );
if ( hDevice != INVALID_HANDLE_VALUE ) {
printf( "打开设备成功!\n" );
}else {
printf( "打开设备失败!\n" );
break;
}
if( ReadFile( hDevice, &byBuf, sizeof(byBuf), &dwByteRead, NULL ) ) {
printf( "读取设备成功!\n" );
}else {
printf( "读取设备失败!\n" );
break;
}
if ( WriteFile(hDevice,&byBuf, dwByteRead, &dwByteWrite, NULL ) ) {
printf( "写入设备成功!\n" );
}else {
printf( "写入设备失败!\n" );
break;
}
//发送控制码
if( DeviceIoControl( hDevice,
CTL_CODE( FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS ),
&byBuf, dwByteRead, &byBuf, sizeof(byBuf), &dwByteRead, NULL ) ) {
printf( "发送控制码成功!\n" );
}else {
printf( "发送控制码失败!\n" );
break;
}
}while( FALSE );
if ( hDevice != INVALID_HANDLE_VALUE) {
if ( CloseHandle( hDevice ) ) {
printf( "关闭设备句柄成功!\n" );
}else {
printf( "关闭设备句柄失败!\n" );
}
}
//卸载驱动
if ( UnloadNTDriver( DRIVER_NAME ) ) {
printf( "驱动卸载成功!\n" );
}else {
printf( "驱动卸载失败!\n" );
}
getch();
return 0;
}
|