The fact that I'm a green hand,and I try to understand some of the technical like HOOK.Uh....Unfortunately I failed,maybe I fail,Please help me,I try to set a window hook in some application ,such as IE,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace 笔记本hook
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int GetCurrentThreadId ();
[DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
[DllImport("User32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr OpenProcess(uint fdwAccess,bool fInherit,uint IDProcess);
[DllImport("kernel32", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr VirtualAllocEx(IntPtr hProcess,int lpAddress,int dwSize,int flAllocationType,int flProtect);
public Form1()
{
InitializeComponent();
}
static int hHook = 0;
const int WH_KEYBOARD = 13;
const int PROCESS_ALL_ACCESS = 0x1F0FFF;
[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
public void HOOK_Start()
{
if (hHook == 0)
{
IntPtr hWnd = NativeMethods.FindWindow(null, "Internet Explorer");
NativeMethods.HookProc hp = new NativeMethods.HookProc(KbHookProc);
int outId; int pId;
hHook = NativeMethods.SetWindowsHookEx(2,
hp,
IntPtr.Zero,
GetWindowThreadProcessId(hWnd, out outId));
if (hHook == 0)
{
MessageBox.Show("dead start");
toolStripStatusLabel1.Text = "dead start";
return;
}
else toolStripStatusLabel1.Text = "success!";
}
}
private void button1_Click(object sender, EventArgs e)
{
HOOK_Start();
}
void UnHook()
{
if (hHook > 0)
{
bool ret = NativeMethods.UnhookWindowsHookEx(hHook);
if (ret == false)
{
MessageBox.Show("failed!");
return;
}
hHook = 0;
toolStripStatusLabel1.Text="failed";
}
}
public static int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0)
{
return NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
int r = NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam);
if (wParam == (IntPtr)256)
{
}
else
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
MessageBox.Show(MyKeyboardHookStruct.vkCode.ToString());
}
return r;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnHook();
}
internal static class NativeMethods
{
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
}
private void button2_Click(object sender, EventArgs e)
{
UnHook();
}
}
}
it dosen't works.SetWindowsHookEx was failed.Oh my God!
Maybe a bit messy.However thank you for reading the above,I have been searching for the resolvent in many website 3 weeks.Please solve it,or show your best code!Thanks!