为什么安装WH_GETMESSAGE全局钩子只得到一次消息,同样的代码,安装成键盘钩子能正常运行
我安装上WH_GETMESSAGE全局钩子只能得到一次消息.请大家帮帮忙看一下吧!我把源程序放入附件里了.using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
#region
public static int i = 0;
StreamWriter sw;
// StreamWriter sw1;
[StructLayout(LayoutKind.Sequential)]
public class Msg
{
public IntPtr hwnd;
public IntPtr lparam;
public uint message;
public Point pt;
public uint time;
}
[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo;
}
// 安装钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
[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, Int32 wParam, IntPtr lParam);
[DllImport("imm32.dll")]
static extern IntPtr ImmGetContext(IntPtr hWnd);
[DllImport("imm32.dll")]
static extern int ImmGetCompositionString(IntPtr hIMC, int dwIndex, StringBuilder lpBuf, int dwBufLen);
private const int GCS_RESULTSTR = 2048;
private const int GCS_RESURTREADSTR = 0x0200;
private const int WM_IME_COMPOSITION = 271;
private const int WM_CHAR = 258;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
static int hKeyboardHook = 0;
HookProc KeyboardHookProcedure;
public Form1()
{
InitializeComponent();
}
#endregion
//start hook
private void button1_Click(object sender, EventArgs e)
{
HookStart();
}
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
IntPtr j=lParam;
sw = new StreamWriter(@"C:\Documents and Settings\Administrator\桌面\键盘记录信息.txt", true);
sw.Write(j.ToString()+"\r\n");
sw.Close();
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
//put hook
public void HookStart()
{
if (hKeyboardHook == 0)
{
Process p = Process.GetCurrentProcess();
// 创建HookProc实例
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
// 设置线程钩子
hKeyboardHook = SetWindowsHookEx(3, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule),0);
// 如果设置钩子失败
if (hKeyboardHook == 0)
{
throw new Exception("SetWindowsHookEx failed.");
}
}
}
//end hook
private void button2_Click(object sender, EventArgs e)
{
//卸载钩子
UnhookWindowsHookEx(hKeyboardHook);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
} --------------------编程问答-------------------- 为什么没有人回答啊 --------------------编程问答-------------------- 你的附件里没东西。
补充:.NET技术 , C#