#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
#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
hi没有赋值,在DllMain里面赋值
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.
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
hi=(HINSTANCE)hModule;
return TRUE;
}是不是这么写?
if(0==t)
{
MessageBox(NULL,"","",0);
}结果永远是返回0。
谁教下DLL里该怎么改
hHook = ::SetWindowsHookEx(WH_MOUSE,(HOOKPROC)HookMouseProc, hDllMod,0)
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;
}
你试试。
hi=(HINSTANCE)hModule;
只有在本EXE调用没问题- -!!
{
MEMORY_BASIC_INFORMATION mbi;
return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0)
? (HMODULE) mbi.AllocationBase : NULL);
}
加上这个函数在InstallHook()中加上语句:hi = (HINSTANCE)GetSelfModuleHandle();
嗯是的,我不知道怎么获得句柄,我用的getmodulehandle,谢谢,我先去试试
我想问下例如在case IDC_BTN1:下面响应,该怎么写这个代码,大虾能留个邮箱么