HWND g_hLogin = NULL; 
DWORD dwThreadID ;
g_hLogin=FindWindowEx(NULL,g_hLogin,"Notepad",NULL); //修改
if(g_hLogin!=NULL)
{
      dwThreadID = GetWindowThreadProcessId(g_hLogin, NULL); 
      glhHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,dwThreadID);
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
{
         .....
}
发现不能钩住记事本的键盘
改为g_hLogin=FindWindowEx(NULL,g_hLogin,"#32770","KeyHook"); //修改
则可以钩住本身程序的键盘,不知道是什么原因.
我检查过能够找到记事本的线程id

解决方案 »

  1.   

    是否在dll中实现,要想勾其他程序的消息,必须用dll来实现钩子。
      

  2.   

    忘记说明一点了,我就是在dll中实现,然后在程序中调用dll。
      

  3.   

    dwThreadId
    [in] Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. dwThreadId为0时是全局钩子The global hooks are a shared resource, and installing one affects all applications in the same desktop as the calling thread. All global hook functions must be in libraries. 全局钩子必须在DLL中实现
      

  4.   

    谢谢大家关注,我的程序是从网上当下来的,这是地址http://www.vckbase.com/code/downcode.asp?id=2730,我怎么都钩不上
      

  5.   

    楼主自己修改过代码吧?glhHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,dwThreadID);//这一个调用有问题
    // 第3个参数指定的是CALLBACK函数所在的模块。如果为NULL,则表示在创建该线程的进程内。
    //你应该改成DLL的模块句柄hMod 
    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.//www.vckbase.com的源代码可是这样写的
    g_hKey = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hInstDLL, dwThreadID);
      

  6.   

    不好意思,翻译不准确哈:  
    hMod为NULL表示dwThreadId参数指定的线程是由当前进程创建的并且Hook函数在当前进程关联代码内。
      

  7.   

    FindWindowEx(NULL,g_hLogin,"无标题 - 记事本
    ",NULL);