C#如何做一个HOOK钩子,截获系统消息。
本人初学C#。要做一个HOOK钩子实现在任务管理器隐藏自己的应用程序。
听说HOOK技术很成熟了。但始终没找到用C#如何做。
请高手指教。最好附完成源代码
------解决方案--------------------调用钩子函数。参照如下详细代码:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;
namespace OpenLogHook
{
/// <summary>
/// Class1
/// </summary>
public class Class1
{
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
//Declare hook handle as int.
static int hHook = 0;
//Declare mouse hook constant.
//For other hook types, you can obtain these values from Winuser.h in Microsoft SDK.
public const int WH_MOUSE = 7;
//public string temp;
public IntPtr glhLogWnd;
public delegate uint aw();
//Declare MouseHookProcedure as HookProc type.
HookProc MouseHookProcedure;
//Declare wrapper managed POINT class.
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}
//Declare wrapper managed MouseHookStruct class.
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public IntPtr hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
//Declare wrapper managed MouseHookStruct class.
[StructLayout(LayoutKind.Sequential)]
public class COPYDATASTRUCT
{
public int dwData;
public int cbData;
public string lpData;
}
public struct Mystruct//mousehook struct
{
public string szCaption;
public string szHwnd;
public string szOperation;
public string szPtx;
public string szPty;
}
//public delegate int COPYDATASTRUCT();
public const int WM_MOUSEMOVE= 0x0200;
public const int WM_LBUTTONDOWN= 0x0201;
public const int WM_LBUTTONUP= 0x0202;
public const int WM_RBUTTONDOWN= 0x0204;
public const int WM_RBUTTONUP= 0x0205;
public const int WM_COPYDATA= 0x004A;
public const int WM_USER= 0x0400;
public const int WM_SENDDATA= WM_USER + 101;
//Import for SetWindowsHookEx function.
//Use this function to install thread-specific hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
IntPtr hInstance, int threadId);
//Import for UnhookWindowsHookEx.
//Call this function to uninstall the hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//Import for CallNextHookEx.
//Use this function to pass the hook information to next hook procedure in chain.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode,
IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll")]//,CharSet=CharSet.Auto,
//CallingConvention=CallingConvention.StdCall)]
private static extern IntPtr WindowFromPoint(POINT point);
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]//,CharSet=CharSet.Auto,
//CallingConvention=CallingConvention.StdCall)]