using System;
using System
using System
using System
using System
namespace WAT
{
/**//// <summary>
/// Description: Hook Helper類
/// Author: ZhangRongHua
/// Create DateTime:
/// UpdateHistory:
/// </summary>
public class HookHelper
{
Delegates#region Delegates
public delegate int HookProc(int nCode
#endregion
變量聲明#region 變量聲明
private HookProc KeyboardHookProcedure;
private FileStream MyFs; // 用流來屏蔽ctrl alt delete
private const byte LLKHF_ALTDOWN =
private const byte VK_CAPITAL =
private const byte VK_ESCAPE =
private const byte VK_F
private const byte VK_LCONTROL =
private const byte VK_NUMLOCK =
private const byte VK_RCONTROL =
private const byte VK_SHIFT =
private const byte VK_TAB =
public const int WH_KEYBOARD =
private const int WH_KEYBOARD_LL =
private const int WH_MOUSE =
private const int WH_MOUSE_LL =
private const int WM_KEYDOWN =
private const int WM_KEYUP =
private const int WM_LBUTTONDBLCLK =
private const int WM_LBUTTONDOWN =
private const int WM_LBUTTONUP =
private const int WM_MBUTTONDBLCLK =
private const int WM_MBUTTONDOWN =
private const int WM_MBUTTONUP =
private const int WM_MOUSEMOVE =
private const int WM_MOUSEWHEEL =
private const int WM_RBUTTONDBLCLK =
private const int WM_RBUTTONDOWN =
private const int WM_RBUTTONUP =
private const int WM_SYSKEYDOWN =
private const int WM_SYSKEYUP =
private static int hKeyboardHook =
#endregion
函數轉換#region 函數轉換
[DllImport(
public static extern int SetWindowsHookEx(int idHook
// 卸載鉤子
[DllImport(
public static extern bool UnhookWindowsHookEx(int idHook);
// 繼續下一個鉤子
[DllImport(
public static extern int CallNextHookEx(int idHook
// 取得當前線程編號
[DllImport(
private static extern int GetCurrentThreadId();
[DllImport(
private static extern short GetKeyState(int vKey);
#endregion
方法#region 方法
/**//// <summary>
/// 鉤子回調函數
/// <remark>
/// Author:ZhangRongHua
/// Create DateTime:
/// Update History:
/// </remark>
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <returns></returns>
private int KeyboardHookProc(int nCode
{
KeyMSG m = (KeyMSG) Marshal
if (((Keys) m
((m
((m
((m
(m
(m
)
{
return
}
return CallNextHookEx(hKeyboardHook
}
/**//// <summary>
/// 啟動Hook
/// <remark>
/// Author:ZhangRongHua
/// Create DateTime:
/// Update History:
/// </remark>
/// </summary>
public void HookStart()
{
if (hKeyboardHook ==
{
// 創建HookProc實例
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD
KeyboardHookProcedure
Marshal
// 如果設置鉤子失敗
if (hKeyboardHook ==
{
HookStop();
//throw new Exception(
}
//用二進制流的方法打開任務管理器
MyFs = new FileStream(Environment
FileMode
byte[] MyByte = new byte[(int) MyFs
MyFs
}
}
/**//// <summary>
/// 卸載hook
/// <remark>
/// Author:ZhangRongHua
/// Create DateTime:
/// Update History:
/// </remark>
/// </summary>
public void HookStop()
{
bool retKeyboard = true;
if (hKeyboardHook !=
{
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook =
}
if (null != MyFs)
{
MyFs
}
if (!(retKeyboard))
{
throw new Exception(
}
}
#endregion
Nested type: KeyMSG#region Nested type: KeyMSG
public struct KeyMSG
{
public int dwExtraInfo;
public int flags;
public int scanCode;
public int time;
public int vkCode;
}
#endregion
}
}
//PS
//下的DisableTaskmgr項的值設為
From:http://tw.wingwit.com/Article/program/net/201311/12339.html