#include "stdafx.h"
#include "setwindowhook.h"BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
 )
{
    return TRUE;
}extern "C" _declspec(dllexport) BOOL InstallHook()
{
HookKeybroad=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hi,0);
if(HookKeybroad==NULL)
{
return FALSE;

}
return TRUE;
}LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)

    if(((DWORD)lParam&0x40000000) && (HC_ACTION==nCode))
    {
switch(wParam) 
        {
case VK_ESCAPE:
MessageBox(NULL,"ESC","",0);
break;


        }
}
LRESULT RetVal = CallNextHookEx( HookKeybroad, nCode, wParam, lParam ); 
return RetVal;
}
然后我在EXE中 INSTALLHOOK,但是结果总是返回FALSE;贴上SETWINDOWHOOK.H
#ifndef _SETWINDOWHOOK_H
#define _SETWINDOWHOOK_H
static HHOOK HookKeybroad=NULL;
HINSTANCE hi=NULL;
LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam);
extern "C" _declspec(dllexport) BOOL InstallHook();
#endif

解决方案 »

  1.   

    HookKeybroad=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hi,0);
    hi没有赋值,在DllMain里面赋值
      

  2.   

    lpfn 
    Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process. 
    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. 
    dwThreadId 
    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. 
      

  3.   


    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
     )
    {
    hi=(HINSTANCE)hModule;
        return TRUE;
    }是不是这么写?
      

  4.   

    还是不行- -我在EXE里这样调用的BOOL t=InstallHook();
    if(0==t)
    {
    MessageBox(NULL,"","",0);
    }结果永远是返回0。
    谁教下DLL里该怎么改
      

  5.   

    俺是这样用的:exe---调用dll里面的一个输出函数---->dll输出函数----里面安装钩子.dll
    hHook = ::SetWindowsHookEx(WH_MOUSE,(HOOKPROC)HookMouseProc, hDllMod,0)
      

  6.   

    你在调用失败后用GetLastError(),看看到底是什么错误.
      

  7.   

    你不用定义hi  直接传DllMain的首参数。HookKeybroad=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hModule,0);如果还有问题,[email protected] 发过来,我帮你看看。
      

  8.   

    BOOL APIENTRY DllMain( HINSTANCE hInstance, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
                         )
    {
        return TRUE;
    }extern "C" _declspec(dllexport) BOOL InstallHook()
    {
        HookKeybroad=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hInstance,0);
        if(HookKeybroad==NULL)
        {
            return FALSE;
            
        }
        return TRUE;
    }LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)

        if(((DWORD)lParam&0x40000000) && (HC_ACTION==nCode))
        {
            switch(wParam) 
            {
                case VK_ESCAPE:
                    MessageBox(NULL,"ESC","",0);
                    break;
            
            
            }
        }
        LRESULT RetVal = CallNextHookEx( HookKeybroad, nCode, wParam, lParam ); 
        return RetVal;
    }
    你试试。
      

  9.   

    我这样定义的,
    hi=(HINSTANCE)hModule;
    只有在本EXE调用没问题- -!!
      

  10.   

    HMODULE GetSelfModuleHandle()
    {
     MEMORY_BASIC_INFORMATION mbi;
     return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0) 
      ? (HMODULE) mbi.AllocationBase : NULL);
    }
    加上这个函数在InstallHook()中加上语句:hi = (HINSTANCE)GetSelfModuleHandle();
      

  11.   

    我估计你的DllMain函数就没有调用过,应该是个win32 dll
      

  12.   


    嗯是的,我不知道怎么获得句柄,我用的getmodulehandle,谢谢,我先去试试
      

  13.   

    GetSelfModuleHandle这个函数好像没有,汗- -
    我想问下例如在case IDC_BTN1:下面响应,该怎么写这个代码,大虾能留个邮箱么