[英]SetWindowsHookEx is always returning zero in C#
I'm trying to hook a 3rd party app so that I can interact with the controls. 我正在尝试挂钩第三方应用程序,以便可以与控件进行交互。 But I am having issues with using
SetWindowsHookEx
to handle WH_KEYBOARD
. 但是我在使用
SetWindowsHookEx
处理WH_KEYBOARD
时遇到问题。 It seems there is some problem with parameters that I am passing to SetWindowsHookEx
. 我传递给
SetWindowsHookEx
参数似乎存在一些问题。
public partial class Form1 : Form
{
private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
static IntPtr hHook;
IntPtr windowHandle;
uint processHandle;
HookProc PaintHookProcedure;
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern System.IntPtr FindWindowByCaption(int ZeroOnly, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowsHookEx", SetLastError = true)]
static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
// When you don't want the ProcessId, use this overload and pass IntPtr.Zero for the second parameter
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PaintHookProcedure = new HookProc(PaintHookProc);
windowHandle = FindWindowByCaption(0, "Untitled - Notepad");
uint threadID = GetWindowThreadProcessId(windowHandle, out processHandle);
IntPtr hMod = System.Runtime.InteropServices.Marshal.GetHINSTANCE(typeof(Form1).Module);
// HERE IS THE PROBLEM. It returns always zero. No matter what parameters you pass.
hHook = SetWindowsHookEx(WH_KEYBOARD, PaintHookProcedure, hMod, threadID);
}
public int PaintHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
// Do something here
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
private const int WH_KEYBOARD = 2;
}
Above is the sample code. 上面是示例代码。
SetWindowsHookEx
is returning always zero. SetWindowsHookEx
始终返回零。 Any suggestions would be very helpful. 任何建议将非常有帮助。 Thanks in advance.
提前致谢。
Did you look at this page: https://support.microsoft.com/en-us/help/318804/how-to-set-a-windows-hook-in-visual-c--net ? 您是否看过此页面: https : //support.microsoft.com/en-us/help/318804/how-to-set-a-windows-hook-in-visual-c--net ?
Your Dllimports are subtly different 您的Dllimports有所不同
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetWindowsHookEx", SetLastError = true)]
static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
vs 与
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.