HOOK使用失败望解答
首先,我在HOOK的DLL里这么写了:
Hook.h
#ifndef __MAIN_H__
#define __MAIN_H__#include <windows.h>/* To use this exported function of dll, include this header
* in your project.
*/#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam);
BOOL DLL_EXPORT installHook();#ifdef __cplusplus
}
#endif#endif // __MAIN_H__
HOOK.cpp:
#include "main.h"// a sample exported function
static HHOOK hkb = NULL;
HINSTANCE hookInst;LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//if(lParam&0x40000000 && HC_ACTION == nCode)
//{
//switch(wParam)
//{
//default:MessageBeep(0);
MessageBeep(0);
//}
//}
LRESULT RetVal = CallNextHookEx(hkb, nCode, wParam, lParam );
return RetVal;
}
BOOL installHook()
{
hkb = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyBoardProc, hookInst,0);
return true;
}BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break; case DLL_PROCESS_DETACH:
// detach from process
break; case DLL_THREAD_ATTACH:
// attach to thread
break; case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}然后在使用hook的程序里:
这个地方,我放了DLL文件,lib文件和.h文件
连接里面加lib文件不过没效果
#include <iostream>
#include "globalHook.h"
#include <windows.h>
using namespace std;int main()
{
installHook();
return 0;
}然后又试了下动态连接:
#include <iostream>
//#include "globalHook.h"
#include <windows.h>
using namespace std;static HINSTANCE hinstDll;
typedef BOOL (CALLBACK *insHook)();
insHook instkbHook;
int main()
{
hinstDll = LoadLibrary("GlobalHook.dll");
instkbHook = (insHook)GetProcAddress(hinstDll, "installHook");
instkbHook();
return 0;
}
依旧失败。求解答
首先,我在HOOK的DLL里这么写了:
Hook.h
#ifndef __MAIN_H__
#define __MAIN_H__#include <windows.h>/* To use this exported function of dll, include this header
* in your project.
*/#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam);
BOOL DLL_EXPORT installHook();#ifdef __cplusplus
}
#endif#endif // __MAIN_H__
HOOK.cpp:
#include "main.h"// a sample exported function
static HHOOK hkb = NULL;
HINSTANCE hookInst;LRESULT CALLBACK KeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
//if(lParam&0x40000000 && HC_ACTION == nCode)
//{
//switch(wParam)
//{
//default:MessageBeep(0);
MessageBeep(0);
//}
//}
LRESULT RetVal = CallNextHookEx(hkb, nCode, wParam, lParam );
return RetVal;
}
BOOL installHook()
{
hkb = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyBoardProc, hookInst,0);
return true;
}BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break; case DLL_PROCESS_DETACH:
// detach from process
break; case DLL_THREAD_ATTACH:
// attach to thread
break; case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}然后在使用hook的程序里:
这个地方,我放了DLL文件,lib文件和.h文件
连接里面加lib文件不过没效果
#include <iostream>
#include "globalHook.h"
#include <windows.h>
using namespace std;int main()
{
installHook();
return 0;
}然后又试了下动态连接:
#include <iostream>
//#include "globalHook.h"
#include <windows.h>
using namespace std;static HINSTANCE hinstDll;
typedef BOOL (CALLBACK *insHook)();
insHook instkbHook;
int main()
{
hinstDll = LoadLibrary("GlobalHook.dll");
instkbHook = (insHook)GetProcAddress(hinstDll, "installHook");
instkbHook();
return 0;
}
依旧失败。求解答
这个地方,我放了DLL文件,lib文件和.h文件
连接里面加lib文件不过没效果
=================
文件位置对不对啊,一般DLL文件要和EXE文件放在同一个文件夹下,而h文件,LIB文件则放在源程序目录中
int main()
{
installHook();
return 0;
}
这个进程是不能退出的.
我在installHook()下面加了个system("pause");也不行
hinstDll = LoadLibrary("GlobalHook.dll");
instkbHook = (insHook)GetProcAddress(hinstDll, "installHook");
instkbHook();
放在WM_CREATE消息里还是不行。。
===============
从你上面代码上没看到对hookInst的赋值啊,这个值应该是DLL的模块句柄,一般用DllMain函数的第一个参数.