C# 挂钩前景窗口

C# 挂钩前景窗口,c#,.net,windows,C#,.net,Windows,我怎样才能勾住前景窗口 目前我正在尝试与 using (Process processTmp = Process.GetCurrentProcess()) { using (ProcessModule module = processTmp.MainModule) { IntPtr hModule = Win32.GetModuleHandle(module.ModuleName); uint threadid = Win32.GetWindowThreadProcess

我怎样才能勾住前景窗口

目前我正在尝试与

using (Process processTmp = Process.GetCurrentProcess())
{
  using (ProcessModule module = processTmp.MainModule)
  {
    IntPtr hModule = Win32.GetModuleHandle(module.ModuleName);
    uint threadid = Win32.GetWindowThreadProcessId(Win32.GetForegroundWindow(), IntPtr.Zero);

    //WH_CALLWNDPROC = 4
    m_HookId = Win32.SetWindowsHookEx(4, m_HookProcedure, hModule, threadid); 
    if (m_HookId == IntPtr.Zero)
    {
      int tmp =
        Marshal.GetLastWin32Error();
    }
  }
}
但是m_HookID始终为IntPtr.Zero,错误为1428-无法在没有模块句柄的情况下设置非本地hook(错误为hook需要)。 因此,hModule似乎有问题

有什么建议吗?
Tnx.

您不能将托管程序集注入到进程中。需要一个DLL本地语言,用C语言、C++语言或Delphi语言编写。作为所需DLL的源代码可能很有用。

您的第一个问题是,您正在尝试使用托管代码注入挂钩。这会将CLR注入每个进程,这可能会产生问题,因为一个进程不能承载两个冲突版本的CLR。如果要挂接的任何进程也使用托管代码,则可能存在冲突。