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
|
/*
Windows内核下挂起当前IRP, 在IRP_MJ_CLEANUP中统一完成 3环代码
编译方法参见makefile. TAB = 8
*/
#include <stdio.h>;
#include <windows.h>;
#pragma comment( linker, "/Entry:Jmain"; )
#define SYS_LINK_NAME "\\\\.\\SysLinkPendingIrp";
//===========================================================================
//线程等待函数
//===========================================================================
DWORD WINAPI ThreadProc( LPVOID lpParameter ) {
HANDLE hEvent;
hEvent = *( HANDLE* )lpParameter;
printf( "线程开始进入等待状态!\n"; );
WaitForSingleObject( hEvent, INFINITE );
ExitThread( 0 );
}
//===========================================================================
//用户层的启动函数
//===========================================================================
int __stdcall Jmain( int argc, char* argv[] ) {
HANDLE hFile = 0;
BOOL bRet;
DWORD dwByteWrite;
HANDLE hThread[2] = {0};
DWORD dwThreadId[2];
BYTE byBuf[10];
OVERLAPPED StOverLapped1 = {0};
OVERLAPPED StOverLapped2 = {0};
__try {
//异步打开设备. 下面操作的函数都是异步的
hFile = CreateFile( SYS_LINK_NAME, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0 );
if ( hFile == INVALID_HANDLE_VALUE ) {
printf( "打开设备失败!\n"; );
return -1;
}
//创建两个事件用于异步读取文件
StOverLapped1.hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
StOverLapped2.hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
//创建两个线程等待内核设置事件, 不然看不到效果
hThread[0] = CreateThread( NULL, 0, &ThreadProc, &StOverLapped1.hEvent, 0, &dwThreadId[0] );
if ( hThread[0] == NULL ) {
printf( "创建线程1失败!\n"; );
return -1;
}
hThread[1] = CreateThread( NULL, 0, &ThreadProc, &StOverLapped2.hEvent, 0, &dwThreadId[1] );
if ( hThread[1] == NULL ) {
printf( "创建线程2失败!\n"; );
}
Sleep( 1000 );
//---------------------------------------------------------------------------
//向设备发送了两次写入请求, 都会被挂起.
//---------------------------------------------------------------------------
RtlFillMemory( byBuf, sizeof( byBuf ), 0; );
bRet = WriteFile( hFile, byBuf, sizeof( byBuf ), &dwByteWrite, &StOverLapped1 );
if ( !bRet & GetLastError() != ERROR_IO_PENDING ) {
printf( "写入设备失败\n"; );
return -1;
}
RtlFillMemory( byBuf, sizeof( byBuf ), 'b' );
bRet = WriteFile( hFile, byBuf, sizeof( byBuf ), &dwByteWrite, &StOverLapped2 );
if ( !bRet && GetLastError() != ERROR_IO_PENDING ) {
printf( "写入设备失败\n"; );
return -1;
}
if ( hFile ) {
CloseHandle( hFile );
}
//等待多个对象返回(必须同时返回)
WaitForMultipleObjects( 2, hThread, TRUE, INFINITE );
printf( "两个对象都已经返回!\n"; );
//---------------------------------------------------------------------------
} __finally {
if ( hThread[0] ) {
CloseHandle( hThread[0] );
}
if ( hThread[1] ) {
CloseHandle( hThread[1] );
}
if ( StOverLapped1.hEvent ) {
CloseHandle( StOverLapped1.hEvent );
}
if ( StOverLapped2.hEvent ) {
CloseHandle( StOverLapped2.hEvent );
}
if ( hFile ) {
CloseHandle( hFile );
}
system( "pause"; );
}
return 0;
}
|