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
|
/*
Windows内核下DeviceIoControl与驱动进行交互 3环代码
编译方法参见makefile. TAB = 8
*/
#include <windows.h>
#include <stdio.h>
#include <winioctl.h>
#define SYS_LINK_NAME "\\\\.\\SysLinkJoenDevice"
#define MAX_LENGTH 1024
// 设备类型 功能号 I/O访问内存使用方式 权限
#define IOCTL_CONTROL_BUFFERED CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS )
#define IOCTL_CONTROL_DIRECT CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_IN_DIRECT, FILE_ANY_ACCESS )
#define IOCTL_CONTROL_NEITHER CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_NEITHER, FILE_ANY_ACCESS )
#pragma comment(linker, "/Entry:Jmain")
#pragma comment(linker, "/subsystem:console")
//===========================================================================
int Jmain() {
HANDLE hFile = 0;
BYTE byBuf[10];
BYTE byBuf2[10];
BOOL bRet;
DWORD dwByteRead, i;
//打开设备
hFile = CreateFile( SYS_LINK_NAME, GENERIC_READ | GENERIC_WRITE, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
if ( hFile == INVALID_HANDLE_VALUE ) {
printf( "打开设备错误了!\n" );
return -1;
}
printf( "打开设备成功!\n" );
RtlFillMemory( byBuf, sizeof( byBuf ), '1' );
bRet = DeviceIoControl( hFile, IOCTL_CONTROL_BUFFERED, byBuf, sizeof( byBuf ), \
byBuf2, sizeof( byBuf2 ), &dwByteRead, NULL );
if ( bRet ) {
printf( "BUFFERED 返回缓冲区长度%d, 数据:" );
for ( i = 0; i< dwByteRead; i++ ) {
printf( "%c", byBuf2[i] );
}
printf( "\n" );
}
RtlFillMemory( byBuf, sizeof( byBuf ), '2' );
bRet = DeviceIoControl( hFile, IOCTL_CONTROL_DIRECT, byBuf, sizeof( byBuf ),
byBuf2, sizeof( byBuf2 ), &dwByteRead, NULL );
if ( bRet ) {
printf( "DIRECT 返回缓冲区长度%d, 数据:" );
for ( i = 0; i< dwByteRead; i++ ) {
printf( "%c", byBuf2[i] );
}
printf( "\n" );
}
RtlFillMemory( byBuf, sizeof( byBuf ), '3' );
bRet = DeviceIoControl( hFile, IOCTL_CONTROL_NEITHER, byBuf, sizeof( byBuf ),
byBuf2, sizeof( byBuf2 ), &dwByteRead, NULL );
if ( bRet ) {
printf( "NEITHER 返回缓冲区长度%d, 数据:" );
for ( i = 0; i< dwByteRead; i++ ) {
printf( "%c", byBuf2[i] );
}
printf( "\n" );
}
if ( hFile ) {
CloseHandle( hFile );
}
system( "pause" );
ExitProcess(0);
return 0;
}
|