Application level mouse hook RRS feed

  • Question

  • Hi. I wrote a global mouse hook class with a hook type of 14. It works good but I cannot show dialogs right after uninstalling the hook without a 5 second mouse lag.  I no longer need it to be global anyhow so I want to make it application level. In a popular global hook article it says to change the hook type to 7. Which I did but it doesn't work. SetWindowsHookEx yields error 1428 "Cannot set nonlocal hook without a module handle."

    The dialog problem occurs also with a popular hook lib I downloaded aswell, if you know of this then great but I really just want to make an application level mousehook class.

    Heres a relevant function: (Ill post the class if you wish)

            private IntPtr SetHook(MouseProc proc)
                using (Process curProcess = Process.GetCurrentProcess())
                using (ProcessModule curModule = curProcess.MainModule)
                    // hooktype 7
                    return SetWindowsHookEx(_hookType, proc,
                        GetModuleHandle(curModule.ModuleName), 0);
                    // 1428 = Cannot set nonlocal hook without a module handle. (SetWindowsHookEx)

    Friday, February 8, 2013 12:28 AM


  • Hi Brian,

    Welcome to the MSDN Forum.

    Please set the third parameter to 0 and the fourth parameter to current thread ID: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85).aspx  

    hMod [in]


    A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.

    dwThreadId [in]

    Type: DWORD

    The identifier of the thread with which the hook procedure is to be associated. For desktop apps, if this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. For Windows Store apps, see the Remarks section.

    Best regards,

    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Brian-Ferguson Friday, February 8, 2013 12:03 PM
    Friday, February 8, 2013 10:11 AM