Note that there are some explanatory texts on larger screens.

plurals
  1. POPInvoke errors calling external SetWindowsHookEx and GetModuleHandle
    primarykey
    data
    text
    <p>I am trying to set windows hooks in my program to an external EXE. This will be used to monitor resizing/minimizing of the window, so I can resize my program similarly, docking to the window. </p> <p>How do I get around error codes <strong>1428</strong> and <strong>126</strong> below?</p> <p>When calling <strong>SetWindowsHookEx</strong> with a null hMod, I was getting this error <strong>1428</strong>. I get the same error if passing the current module (instead of IntPtr.Zero), which it seems to get correctly, as so:</p> <pre><code>IntPtr module = PInvoke.GetModuleHandle(null); [...] SetWindowsHookEx(...,...,module,...); int error = PInvoke.GetLastError(); </code></pre> <p><strong>1428</strong> = <em>Cannot set nonlocal hook without a module handle</em></p> <p>I also tried to grab the external program I'm hooking as a module using <strong>GetModuleHandle</strong>:</p> <pre><code>IntPtr module = PInvoke.GetModuleHandle("communicator.exe"); int error = PInvoke.GetLastError(); </code></pre> <p>But <strong>error</strong> is then set to:</p> <p><strong>126</strong> = <em>The specified module could not be found.</em></p> <p>I am using the following PInvoke statements:</p> <pre><code>[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId); </code></pre> <p>This is the procedure that is having the issue:</p> <pre><code>public void Install(IntPtr hWnd) { uint threadId; uint processId; if (hWnd == IntPtr.Zero) { threadId = (uint)AppDomain.GetCurrentThreadId(); throw new Exception("Lync thread not found!"); } else { threadId = PInvoke.GetWindowThreadProcessId(hWnd, out processId); } //IntPtr module = PInvoke.GetModuleHandle(null); //IntPtr module = PInvoke.GetModuleHandle(GetType().Module.FullyQualifiedName); IntPtr module = PInvoke.GetModuleHandle("communicator.exe"); int error = PInvoke.GetLastError(); m_hhook = PInvoke.SetWindowsHookEx( m_hookType, m_filterFunc, //Process.GetCurrentProcess().Handle, //threadId); //IntPtr.Zero, //module, //Marshal.GetHINSTANCE( // System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0] // ).ToInt32() module, threadId); //IntPtr hinst = Marshal.GetHINSTANCE(Process.GetCurrentProcess().Handle); // http://msdn.microsoft.com/en-us/library/ms681385 // ERROR_HOOK_NEEDS_HMOD - 1428 = Cannot set nonlocal hook without a module handle error = PInvoke.GetLastError(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload