Nejchy
Nejchy

Reputation: 666

Hook foreground window

How can i hook the foreground window?

Currently I'm trying with

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();
    }
  }
}

But m_HookID is always IntPtr.Zero with error 1428 - Cannot set nonlocal hook without a module handle(ERROR_HOOK_NEEDS_HMOD). So it seems that there is something wrong with hModule.

Any suggestions? Tnx.

Upvotes: 0

Views: 662

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

You cannot inject a managed assembly into a process. A native DLL is required, written in a language like C, C++ or Delphi. This project might be useful as a source for the DLL you'll need.

Upvotes: 2

Related Questions