1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.Runtime.InteropServices;
5
using
System.IO;
6
using
System.Windows.Forms;
7
using
System.Reflection;
8
9
namespace
SISS.Client
10
{
11
public
class
KeyBoardHook
12
{
13
#region
Delegates
14
15
public
delegate
int
HookProc(
int
nCode, Int32 wParam, IntPtr lParam);
16
17
#endregion
18
19
#region
变量声明
20
21
private
HookProc KeyboardHookProcedure;
22
private
FileStream MyFs;
//
用流来屏蔽ctrl alt delete
23
24
private
const
byte
LLKHF_ALTDOWN
=
0x20
;
//
ALT
25
private
const
byte
VK_ESCAPE
=
0x1B
;
//
ESC键
26
private
const
byte
VK_F4
=
0x73
;
//
F4健
27
private
const
byte
VK_NUMLOCK
=
0x90
;
//
小键盘
28
private
const
byte
VK_SHIFT
=
0x10
;
//
SHIFT键盘
29
private
const
byte
VK_TAB
=
0x09
;
//
TAB键盘
30
public
const
int
WH_KEYBOARD
=
13
;
//
获得键盘
31
private
const
byte
VK_RCONTROL
=
0xA3
;
//
CTRL键
32
private
static
int
hKeyboardHook
=
0
;
33
#endregion
34
35
#region
对应COM的函数
36
37
[DllImport(
"
user32.dll
"
, CharSet
=
CharSet.Auto, CallingConvention
=
CallingConvention.StdCall)]
38
public
static
extern
int
SetWindowsHookEx(
int
idHook, HookProc lpfn, IntPtr hInstance,
int
threadId);
39
40
//
卸载钩子
41
42
[DllImport(
"
user32.dll
"
, CharSet
=
CharSet.Auto, CallingConvention
=
CallingConvention.StdCall)]
43
public
static
extern
bool
UnhookWindowsHookEx(
int
idHook);
44
45
//
继续下一个钩子
46
47
[DllImport(
"
user32.dll
"
, CharSet
=
CharSet.Auto, CallingConvention
=
CallingConvention.StdCall)]
48
public
static
extern
int
CallNextHookEx(
int
idHook,
int
nCode, Int32 wParam, IntPtr lParam);
49
50
#endregion
51
52
#region
方法
53
private
int
KeyboardHookProc(
int
nCode, Int32 wParam, IntPtr lParam)
54
{
55
KeyMSG m
=
(KeyMSG)Marshal.PtrToStructure(lParam,
typeof
(KeyMSG));
56
57
58
if
(((Keys)m.vkCode
==
Keys.LWin
||
(Keys)m.vkCode
==
Keys.RWin)
59
||
((m.vkCode
==
VK_TAB)
&&
((m.flags
&
LLKHF_ALTDOWN)
!=
0
))
//
屏蔽alt+TAB
60
||
(m.vkCode
==
VK_F4)
&&
((m.flags
&
LLKHF_ALTDOWN)
!=
0
)
//
屏蔽alt+F4
61
||
((Keys)m.vkCode
==
Keys.LWin)
&&
((Keys)m.vkCode
==
Keys.D)
//
屏蔽windows+D键
62
||
((Keys)m.vkCode
==
Keys.RWin)
&&
((Keys)m.vkCode
==
Keys.D)
63
||
(m.vkCode
==
VK_ESCAPE)
&&
((m.flags
&
LLKHF_ALTDOWN)
!=
0
)
//
屏蔽alt+Esc
64
||
(m.vkCode
==
VK_RCONTROL)
&&
(m.vkCode
==
VK_ESCAPE)
//
屏蔽Ctrl+Esc
65
66
67
)
68
{
69
return
1
;
70
}
71
72
return
CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
73
}
74
///
<summary>
75
///
启动钩子
76
///
</summary>
77
public
void
HookStart()
78
{
79
if
(hKeyboardHook
==
0
)
80
{
81
//
创建HookProc实例
82
83
KeyboardHookProcedure
=
new
HookProc(KeyboardHookProc);
84
85
hKeyboardHook
=
SetWindowsHookEx(WH_KEYBOARD,
86
KeyboardHookProcedure,
87
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[
0
]),
88
0
);
89
90
//
如果设置钩子失败
91
92
if
(hKeyboardHook
==
0
)
93
{
94
HookStop();
95
}
96
97
//
用二进制流的方法打开任务管理器。而且不关闭流.这样任务管理器就打开不了
98
MyFs
=
new
FileStream(Environment.ExpandEnvironmentVariables(
"
%windir%\\system32\\taskmgr.exe
"
),
99
FileMode.Open);
100
byte
[] MyByte
=
new
byte
[(
int
)MyFs.Length];
101
MyFs.Write(MyByte,
0
, (
int
)MyFs.Length);
102
}
103
}
104
///
<summary>
105
///
卸载钩子
106
///
</summary>
107
public
void
HookStop()
108
{
109
bool
retKeyboard
=
true
;
110
111
if
(hKeyboardHook
!=
0
)
112
{
113
retKeyboard
=
UnhookWindowsHookEx(hKeyboardHook);
114
115
hKeyboardHook
=
0
;
116
}
117
118
if
(
null
!=
MyFs)
119
{
120
MyFs.Close();
121
}
122
123
if
(
!
(retKeyboard))
124
{
125
throw
new
Exception(
"
卸载钩子失败
"
);
126
}
127
}
128
129
130
#endregion
131
132
#region
Nested type: KeyMSG
133
134
public
struct
KeyMSG
135
{
136
public
int
vkCode;
//
获得键盘输入的直
137
public
int
scanCode;
138
public
int
flags;
//
键盘的状态
139
}
140
141
#endregion
142
143
144
145
}
146
}