HHOOK hMouseHook=NULL;
HINSTANCE MyInst=NULL;
..........
MyInst=::AfxGetApp()->m_hInstance;   //我自己写的,不知对不对!
hMouseHook= SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc,MyInst, NULL);
结果不是全局钩子,只有当鼠标位于程序窗口内时,MouseProc函数才响应.这是为什么?(这些代码是在一个具有全局鼠标钩子功能的程序中拷贝过来的)
谢谢

解决方案 »

  1.   

    这样写:#pragma data_seg("Shared")
    HHOOK hMouseHook=NULL;
    HINSTANCE MyInst=NULL;
    #pragma data_seg()
      

  2.   

    这样写:#pragma data_seg("Shared")
    HHOOK hMouseHook=NULL;
    HINSTANCE MyInst=NULL;
    #pragma data_seg()
      

  3.   

    http://www.codeguru.com/system/index.shtml
    http://www.codeguru.com/system/Hookwww.html
      

  4.   

    全局钩子一般是做成DLL,不过也可以用日志钩子做成EXE,你做的没什么错啊,你要问什么?
      

  5.   

    哦。对了。还要把你的全局数据保护起来,用
    #pragma data_seg("Shared")
    HHOOK hMouseHook=NULL;
    HINSTANCE MyInst=NULL;
    #pragma data_seg()
    然后在.DEF文件中导出
      

  6.   

    使用 WH_JOURNALRECORD 类型的钩子就可以了,否则还需要将全局的数据据
    保护起来。然后在钩子函数里判断是不是鼠标消息就可以了。
      

  7.   

    You must place a global hook procedure in a DLL separate from the application installing the hook procedure. The installing application must have the handle to the DLL module before it can install the hook procedure. To retrieve a handle to the DLL module, call the LoadLibrary function with the name of the DLL. After you have obtained the handle, you can call the GetProcAddress function to retrieve a pointer to the hook procedure. Finally, use SetWindowsHookEx to install the hook procedure address in the appropriate hook chain. SetWindowsHookEx passes the module handle, a pointer to the hook-procedure entry point, and 0 for the thread identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread. This sequence is shown in the following example. You can release a global hook procedure by using UnhookWindowsHookEx, but this function does not free the DLL containing the hook procedure. This is because global hook procedures are called in the process context of every application in the desktop, causing an implicit call to the LoadLibrary function for all of those processes. Because a call to the FreeLibrary function cannot be made for another process, there is then no way to free the DLL. The system eventually frees the DLL after all processes explicitly linked to the DLL have either terminated or called FreeLibrary and all processes that called the hook procedure have resumed processing outside the DLL. 
      

  8.   

    #pragma data_seg("Shared")
    HHOOK hMouseHook=NULL;
    HINSTANCE MyInst=NULL;
    #pragma data_seg()
    // Instruct the linker to make the Shared section
    // readable, writtable and shared
    #pragma comment(linker, "/section:Shared,rws")
      

  9.   

    问题还是没有解决   :(to  jishiping(JSP 季世平) :
    我的程序:取色工具
    我现在正在完成第一步,就是作一个全局钩子捕获鼠标消息,即鼠标每移动一下,都进行一次取色,但是就这个第一步却进展不下去,具体情况如下:
    dll为MFC规则DLL,然后将下面的代码都写在了dll的cpp文件中了,exe为对话框形式:
    #pragma data_seg("Shared")
    HHOOK hMouseHook=NULL;
    HINSTANCE MyInst=NULL;
    #pragma data_seg()
    // Instruct the linker to make the Shared section
    // readable, writtable and shared
    #pragma comment(linker, "/section:Shared,rws")
    ..........
    MyInst=::AfxGetApp()->m_hInstance;   hMouseHook= SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc,MyInst, NULL);但是运行后,只有当鼠标在程序窗口内时,MouseProc函数才响应.
      

  10.   

    我不是说了吗,最简单的方法是用 WH_JOURNALRECORD 类型的钩子就没事了。HWND  hWnd=NULL;
    HHOOK hHook=NULL;
    HINSTANCE MyInst=NULL;
    LRESULT CALLBACK JournalProc(int code, WPARAM wParam, LPARAM lParam)
    {
        HDC hDC;
        POINT Pos;
        char buf[33];
        COLORREF Color;    EVENTMSG* pMsg = (EVENTMSG*)lParam;
        if (code==HC_ACTION && pMsg->message==
            WM_MOUSEMOVE) {
            GetCursorPos(&Pos);
            hDC = GetWindowDC(NULL);
            Color = GetPixel(hDC, Pos.x, Pos.y);
            ReleaseDC(NULL, hDC);
            wsprintf(buf, "R=%d G=%d B=%d", (int)
                GetRValue(Color), (int)GetGValue(
                Color), (int)GetBValue(Color));
            SetWindowText(hWnd, buf);
        }
        return CallNextHookEx(hHook, code, wParam, lParam);
    }__declspec(dllexport) void EnableHook(HWND hwnd)
    {
        hWnd = hwnd;
        if (hHook != NULL) {
            MyInst = ::AfxGetApp()->m_hInstance;
            hHook = SetWindowsHookEx(WH_JOURNALRECORD,
                (HOOKPROC)JournalProc, MyInst, NULL);
        }
    }