0

I'm trying to write a dll to inject a global mouse hook in Windows 7. It basically works, but after some seconds (maybe 10 - 15) the whole application just freezes up and the mouse wont work outside Visual Studio (I can still stop the app via vs but nothing else (like using the task manager to do it)).

I've been searching for some time now but I can't really find an answer to my problem. I've reduced my dll to the bare minimum (install the mouse hook without doing anything with it) but it still happens.

header

#ifndef MOUSE_HOOK_WINDOWS_H
#define MOUSE_HOOK_WINDOWS_H

#if defined DLL_EXPORT
    #define DECLDIR __declspec(dllexport)
#else
    #define DECLDIR __declspec(dllimport)
#endif

#pragma data_seg(".mouseHookSharedMemory")
    HHOOK g_hMouseHook = NULL;
    HINSTANCE g_hInst = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:.mouseHookSharedMemory,RWS")

extern "C"
{
    DECLDIR BOOL InstallHook();
    DECLDIR BOOL UninstallHook();

    LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);
}

#endif

cpp

#include <Windows.h>
#include "MouseHookWindows.h"

#include <sstream>

extern "C"
{
    DECLDIR BOOL InstallHook()
    {
        if(g_hMouseHook != NULL)
        {
            return true;
        }

        g_hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInst, 0);
        if(g_hMouseHook == NULL)
        {
            return false;
        }

        return true;
    }

    DECLDIR BOOL UninstallHook()
    {
        if(g_hMouseHook != NULL)
        {
            UnhookWindowsHookEx(g_hMouseHook);
            g_hMouseHook = NULL;
        }  

        return true;
    }

    LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        return CallNextHookEx(g_hMouseHook, nCode, wParam, lParam);
    }
}

BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD reason, LPVOID reserved)
{
    g_hInst = hInstance;

    return true;
}

There's no exception or anything. The application freezes and everything but visual studio wont react to the mouse anymore (as said before, not even the task manager)

I don't know whether it's important but I use the dll in a c++ console application.

Thanks in advance Manuel

wrongway
  • 1
  • 2
  • Process that installs the hook should have a working message loop or it can freeze all hooked applications, maybe this is the problem? – user2802841 Dec 22 '13 at 23:57
  • Your DllMain() is borken, it sets g_hMouseHook back to NULL. Don't ignore *reason*. – Hans Passant Dec 22 '13 at 23:57
  • @HansPassant Right, thanks, I changed that (also edited the above code), but I still get the same behavior as before. – wrongway Dec 23 '13 at 00:24
  • @user2802841 Isn't that only for low level mouse hooks (WH_MOUSE_LL)? where would the loop be in this case? in the dll, or in the app using the dll? – wrongway Dec 23 '13 at 00:27
  • 1
    Next bug is #pragma data_seg, you have every DLL overwrite the others' handles. Only share your own data. – Hans Passant Dec 23 '13 at 00:30
  • @HansPassant So I should use CreateFileMapping() rather than #pragma data_seg? I'm just wondering because in the link in this answer (http://stackoverflow.com/a/10818037/3127976) data_seg is used, as well as in some other articles I red. Anyway, thanks again for the answer, I'll try it. – wrongway Dec 23 '13 at 01:17

1 Answers1

0

user2802841 was right, the problem was the missing message loop. When using a mouse hook (WH_MOUSE or WH_MOUSE_LL) in a console application getMessage or peekMessage have to be called somewhere in the application.

wrongway
  • 1
  • 2