Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I put LowLevelMouseProc and LowLevelKeyboardProc in the main EXE?
    text
    copied!<p>Global Windows hooks must be in a DLL because the hook is going to be called in the context of a different process, so the hook procedure's code must be injected into that process. However, there are <a href="http://msdn.microsoft.com/en-us/library/ms644990%28VS.85%29.aspx" rel="nofollow noreferrer">limitations</a>:</p> <blockquote> <p><code>SetWindowsHookEx</code> can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call <code>SetWindowsHookEx</code> to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call <code>SetWindowsHookEx</code> to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.</p> </blockquote> <p>For this reason, I'd rather use the low-level hooks <code>WH_MOUSE_LL</code> and <code>WH_KEYBOARD_LL</code>, instead of <code>WH_MOUSE</code> and <code>WH_KEYBOARD</code>. As seen from <a href="http://msdn.microsoft.com/en-us/library/ms644986%28VS.85%29.aspx" rel="nofollow noreferrer">their</a> <a href="http://msdn.microsoft.com/en-us/library/ms644985%28VS.85%29.aspx" rel="nofollow noreferrer">documentation</a>:</p> <blockquote> <p>This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.</p> </blockquote> <p>This leads me to think that these particular hook procedures do not need to be in a separate DLL, and can just live inside the EXE that hooked them up. The <a href="http://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx" rel="nofollow noreferrer">documentation for <code>SetWindowsHookEx</code></a>, however, says:</p> <blockquote> <p><code>lpfn</code></p> <p>[in] Pointer to the hook procedure. If the <code>dwThreadId</code> parameter is zero or specifies the identifier of a thread created by a different process, the <code>lpfn</code> parameter must point to a hook procedure in a DLL.</p> </blockquote> <p>No explicit exception for the two low-level hooks is mentioned.</p> <p>I have seen several .NET applications that use the low-level hooks without having their hook procedures in a separate DLL. That is another hint that this is acceptable. However, I'm a bit scared to do this myself since the documentation forbids it.</p> <p>Does anyone foresee any trouble if I don't use a DLL and just put these low-level hook procedures straight into my EXE?</p> <p><strong>Edit</strong>: For the bounty, I would like a definitive "yes, this is ok, because..." or "no, this can go wrong, because...".</p>
 

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