钩子函数SetWindowsHookEx中的第三个参数hInstance是本程序的还是被监控程序的。有什么API可以得到这个hInstance吗?

解决方案 »

  1.   

    本程序就是被监控程序阿。钩子一般是动态库,需要监控的系统需要将其应用程序句柄传递给钩子。
    用AfxGetInstanceHandle  
    HINSTANCE AfxGetInstanceHandle( );
    就可以。
      

  2.   

    DllMain中给的模块地址。
    就是包含HookProc的动态库加载地址。
      

  3.   

    如果是钩自己(dwThreadId设置为自进程的某个线程的tID),设为NULL
      

  4.   

    如果是全局钩子, hInstance是DLL句柄
    否则给NULL就可以了
      

  5.   

    监控其他程序就是AfxGetInstanceHandle();
      

  6.   

    AfxGetInstanceHandle  
    HINSTANCE AfxGetInstanceHandle( );Return ValueAn HINSTANCE to the current instance of the application. If called from within a DLL linked with the USRDLL version of MFC, an HINSTANCE to the DLL is returned.ResThis function allows you to retrieve the instance handle of the current application. AfxGetInstanceHandle always returns the HINSTANCE of your executable file (.EXE) unless it is called from within a DLL linked with the USRDLL version of MFC. In this case, it returns an HINSTANCE to the DLL.
      

  7.   

    AfxGetInstanceHandle这个只能得到本程序的hInstance吧,是不是要传被监控程序的hInstance
      

  8.   

    呵呵,这帖怎么人气那么好啊CSDN上面全是星星的都来了呵呵,我都不敢发言了
      

  9.   

    给你个例子看看,这是安装钩子的一个函数
    extern "C" __declspec(dllexport)  BOOL starthook()

      AFX_MANAGE_STATE(AfxGetStaticModuleState());
      BOOL bResult=FALSE; 
      glhHookKeyboard=SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,AfxGetInstanceHandle(),0); 
      if(glhHookKeyboard!=NULL) 
      bResult=TRUE; 
      return bResult; 

    其中AfxGetInstanceHandle()就可以得到这个dll的实例句柄,如楼上所所HINSTANCE AfxGetInstanceHandle( );   HINSTANCE也是一种句柄 就像常用的HWND一样 只是那时窗体句柄