我用 1. m_hControlHook = ::SetWindowsHookEx(WH_CALLWNDPROC, WinControlHookEx_Zi, 
AfxGetApp()->m_hInstance, ::GetCurrentThreadId());2. m_hControlHook = ::SetWindowsHookEx(WH_CALLWNDPROC, WinControlHookEx_Zi, 
0, ::GetCurrentThreadId());3. m_hControlHook = ::SetWindowsHookEx(WH_CALLWNDPROC, WinControlHookEx_Zi, 
AfxGetApp()->m_hInstance, ::GetCurrentPrcessId());// 其中 WinControlHookEx_Zi是用钩子函数!1、2、3在exe 中和dll中都钩不到用CreateThread();创建的线程里面的消息,我该怎么做呢?

解决方案 »

  1.   


    ::SetWindowsHookEx(WH_CALLWNDPROC, WinControlHookEx_Zi, 
    AfxGetApp()->m_hInstance, dwThreadID/*这个市你要挂钩的线程ID,而不是用进程ID*/);
      

  2.   

    Installing and Releasing Hook Procedures
    You can install a hook procedure by calling the SetWindowsHookEx function and specifying the type of hook calling the procedure, whether the procedure should be associated with all threads in the same desktop as the calling thread or with a particular thread, and a pointer to the procedure entry point. You must place a global hook procedure in a dynamic-link library (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. HOOKPROC hkprcSysMsg; 
    static HINSTANCE hinstDLL; 
    static HHOOK hhookSysMsg; 
     
    hinstDLL = LoadLibrary((LPCTSTR) "c:\\windows\\sysmsg.dll"); 
    hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "SysMessageProc"); 
    hhookSysMsg = SetWindowsHookEx(WH_SYSMSGFILTER,hkprcSysMsg,hinstDLL,0); 
    You can release a thread-specific hook procedure (remove its address from the hook chain) by calling the UnhookWindowsHookEx function, specifying the handle to the hook procedure to release. Release a hook procedure as soon as your application no longer needs it. 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. An alternative method for installing a global hook procedure is to provide an installation function in the DLL, along with the hook procedure. With this method, the installing application does not need the handle to the DLL module. By linking with the DLL, the application gains access to the installation function. The installation function can supply the DLL module handle and other details in the call to SetWindowsHookEx. The DLL can also contain a function that releases the global hook procedure; the application can call this hook-releasing function when terminating.
      

  3.   

    如果不是Hook当前线程的消息,必须使用global的钩子,而global的钩子必须放在一个DLL中。
      

  4.   

    如果把ThreadId用0,它不是系统的钩子了吗!!
    我只想钩到当前进程的消息!---------------------我是搂主、
      

  5.   

    而且,把dwThreadId写成0的话,我的系统就发生异常错误。各种方法都试过了,怎么办呢!?我就是想钩我当前系统进程内的所有线程,其实我是在做换肤!
      

  6.   

    那你就在创建一个线程后马上用SetWindowsHookEx对其设置钩子。