#include <windows.h>
#pragma hdrstop
#pragma argsused
static HINSTANCE hInst;
static HHOOK hhk;
extern "C" __declspec(dllexport) int test();
extern "C" __declspec(dllexport) bool starR();
extern "C" __declspec(dllexport) bool StopR();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(NULL,nCode,wParam,lParam);
}
bool starR()
{
// FARPROC lpfnKeyHook=(FARPROC)KeyProc;
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
{
return FALSE;
}
return TRUE;
}
bool StopR()
{
return UnhookWindowsHookEx(hhk);
}
int test()
{
return 5;
}
这里的test 可以正常运行 可 bool starR(); 都没用。帮帮看看着急用他做其他工具呢
谢谢!!!!!!!!!!
13 个解决方案
#1
// FARPROC lpfnKeyHook=(FARPROC)KeyProc; 这行不要注释也不可以
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
原来直接写的
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyProc,hInst,0)))
都不可以
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
原来直接写的
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyProc,hInst,0)))
都不可以
#2
你的hInst没有符初值
#3
static 在dll中有用么。反正我在vc下作的时候,钩子handle是放在一个共享的数据段里才行的。
#4
你这么写,加:
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst=hinst;
return 1;
}
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst=hinst;
return 1;
}
#5
是starR(); 没有调用还是安装钩子失败,如果是后者,用sprewellkobe(我为软件狂!) 的方法。
hhk不放在共享数据段也可以的。
hhk不放在共享数据段也可以的。
#6
昨晚代码就修改了:
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
static HINSTANCE hInst;
static HHOOK hhk;
extern "C" __declspec(dllexport) int kkk();
extern "C" __declspec(dllexport) bool BeginR();
extern "C" __declspec(dllexport) bool StopR();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst = hinst ;// 已经改过了 调试已经负值了
return 1;
}
LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
bool BeginR()
{
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
{
return false;//hhk 返回不为NULL 是一个数字。
}
return true;
}
bool StopR()
{
return UnhookWindowsHookEx(hhk);
}
int kkk()
{ int i ;
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0);
i=GetLastError();
return i;//在这里测试的返回 0
}
结果3个函数都能运行,但根本没有任何安装hook的效果。
调用处的代码:
bool loaddll(const char* dllname,const char* funname,int i)
{
try{
HINSTANCE DllHandle;
FARPROC lpFarProc;
DllHandle=LoadLibrary(dllname);
lpFarProc=GetProcAddress(DllHandle,funname);
if (i==0)
{
pFarProc();//这里可以运行单没反应
// MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
}else if(i==1)
{
Form1->Edit1->Text=IntToStr(lpFarProc());
}
FreeLibrary(DllHandle);
return true ;
}
catch(Exception &exception)
{
return false;
}
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_BeginR",0);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_kkk",1);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_StopR",0);
}
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
static HINSTANCE hInst;
static HHOOK hhk;
extern "C" __declspec(dllexport) int kkk();
extern "C" __declspec(dllexport) bool BeginR();
extern "C" __declspec(dllexport) bool StopR();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst = hinst ;// 已经改过了 调试已经负值了
return 1;
}
LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
bool BeginR()
{
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
{
return false;//hhk 返回不为NULL 是一个数字。
}
return true;
}
bool StopR()
{
return UnhookWindowsHookEx(hhk);
}
int kkk()
{ int i ;
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0);
i=GetLastError();
return i;//在这里测试的返回 0
}
结果3个函数都能运行,但根本没有任何安装hook的效果。
调用处的代码:
bool loaddll(const char* dllname,const char* funname,int i)
{
try{
HINSTANCE DllHandle;
FARPROC lpFarProc;
DllHandle=LoadLibrary(dllname);
lpFarProc=GetProcAddress(DllHandle,funname);
if (i==0)
{
pFarProc();//这里可以运行单没反应
// MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
}else if(i==1)
{
Form1->Edit1->Text=IntToStr(lpFarProc());
}
FreeLibrary(DllHandle);
return true ;
}
catch(Exception &exception)
{
return false;
}
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_BeginR",0);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_kkk",1);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_StopR",0);
}
#7
不好意思:调用处
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
这几行不要,忘记删掉了。
另外:
谢谢:: glyc(大灰狼) : sprewellkobe(我为软件狂!): sjd107(phil)
:) 不过还没解决问题呢
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
这几行不要,忘记删掉了。
另外:
谢谢:: glyc(大灰狼) : sprewellkobe(我为软件狂!): sjd107(phil)
:) 不过还没解决问题呢
#8
全局键盘hook 不用这么麻烦吧??
hookkey.cpp
//---------------------------------------------------------------------------
#pragma option -zRSHSEG // 改变缺省数据段名
#pragma option -zTSHCLASS // 改变缺省数据类名
#include <windows.h>
//---------------------------------------------------------------------------
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204) // 自定义消息号
#define HOOKAPI_SECONDPARAM (WM_USER + 205) // 自定义消息第2参数
extern "C" __declspec(dllexport) void __stdcall SetHook(HWND,bool);
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam) ;
//---------------------------------------------------------------------------
#pragma argsused
static HINSTANCE hInstance; // 应用实例句柄
static HWND hWndMain; // MainForm句柄
static HHOOK hKeyHook; // HOOK句柄
int LoadCount=0; // DLL装入次数计数
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
if ( hInstance == NULL )
hInstance=hinst;
if(reason==DLL_PROCESS_ATTACH) // DLL入口
LoadCount+=1; // 装入计数
else if(reason==DLL_PROCESS_DETACH) // DLL出口处理
LoadCount-=1;
return 1;
}
//----------------------------------------------------------------------------------------------------
void __stdcall SetHook(HWND hMainWin,bool nCode)
{
/*
WINDOW下可进行挂接的过滤函数包括11种:
WH_CALLWNDPROC 窗口函数的过滤函数
WH_CBT 计算机培训过滤函数
WH_DEBUG 调试过滤函数
WH_GETMESSAGE 获取消息过滤函数
WH_HARDWARE 硬件消息过滤函数
WH_JOURNALPLAYBACK 消息重放过滤函数
WH_JOURNALRECORD 消息记录过滤函数
WH_MOUSE 鼠标过滤函数
WH_MSGFILTER 消息过滤函数
WH_SYSMSGFILTER 系统消息过滤函数
WH_KEYBOARD 键盘过滤函数
*/
if(nCode) // 安放HOOK
{
hWndMain=hMainWin;
hKeyHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)HookProc,hInstance,0);
}
else // 卸下HOOK
UnhookWindowsHookEx(hKeyHook);
}
//----------------------------------------------------------------------------------------------------
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
// 向调用窗体发消息HOOKAPI_FIRSTCOMMAND和虚拟键码(char)(keyMSG->paramL)
EVENTMSG *keyMSG=(EVENTMSG *)lParam;
if((nCode==HC_ACTION)&&(keyMSG->message==WM_KEYUP))
PostMessage(hWndMain,HOOKAPI_FIRSTCOMMAND,(char)(keyMSG->paramL),HOOKAPI_SECONDPARAM);
return((int)CallNextHookEx(hKeyHook,nCode,wParam,lParam));
}
//----------------------------------------------------------------------------------------------------
调用
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainUnit.h"
static HINSTANCE hDLL; // DLL句柄
typedef void __stdcall (*DLLFUN)(HWND,bool);
DLLFUN DLLSetHook;
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204)
#define HOOKAPI_SECONDPARAM (WM_USER + 205)
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
hDLL=LoadLibrary((LPCTSTR)"Hookkey.dll"); // DLL文件名:Project1.dll
if(hDLL==NULL)
{
ShowMessage("DLL: 不能加载!程序退出。");
exit(1);
}
DLLSetHook =(DLLFUN)GetProcAddress(hDLL,"SetHook");
if(DLLSetHook==NULL)
{
ShowMessage("DLL: 函数没找到!程序退出。");
FreeLibrary(hDLL);
exit(1);
}
DLLSetHook(this->Handle,true);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
DLLSetHook(NULL,false); // 卸下HOOK
FreeLibrary(hDLL); // 卸下DLL
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
{
if(( Msg.message == HOOKAPI_FIRSTCOMMAND )&&(Msg.lParam==HOOKAPI_SECONDPARAM))
ShowMessage(" 收到HOOK按键消息!\n\n 【键虚拟码】:"+IntToStr(Msg.wParam));
}
//---------------------------------------------------------------------------
hookkey.cpp
//---------------------------------------------------------------------------
#pragma option -zRSHSEG // 改变缺省数据段名
#pragma option -zTSHCLASS // 改变缺省数据类名
#include <windows.h>
//---------------------------------------------------------------------------
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204) // 自定义消息号
#define HOOKAPI_SECONDPARAM (WM_USER + 205) // 自定义消息第2参数
extern "C" __declspec(dllexport) void __stdcall SetHook(HWND,bool);
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam) ;
//---------------------------------------------------------------------------
#pragma argsused
static HINSTANCE hInstance; // 应用实例句柄
static HWND hWndMain; // MainForm句柄
static HHOOK hKeyHook; // HOOK句柄
int LoadCount=0; // DLL装入次数计数
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
if ( hInstance == NULL )
hInstance=hinst;
if(reason==DLL_PROCESS_ATTACH) // DLL入口
LoadCount+=1; // 装入计数
else if(reason==DLL_PROCESS_DETACH) // DLL出口处理
LoadCount-=1;
return 1;
}
//----------------------------------------------------------------------------------------------------
void __stdcall SetHook(HWND hMainWin,bool nCode)
{
/*
WINDOW下可进行挂接的过滤函数包括11种:
WH_CALLWNDPROC 窗口函数的过滤函数
WH_CBT 计算机培训过滤函数
WH_DEBUG 调试过滤函数
WH_GETMESSAGE 获取消息过滤函数
WH_HARDWARE 硬件消息过滤函数
WH_JOURNALPLAYBACK 消息重放过滤函数
WH_JOURNALRECORD 消息记录过滤函数
WH_MOUSE 鼠标过滤函数
WH_MSGFILTER 消息过滤函数
WH_SYSMSGFILTER 系统消息过滤函数
WH_KEYBOARD 键盘过滤函数
*/
if(nCode) // 安放HOOK
{
hWndMain=hMainWin;
hKeyHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)HookProc,hInstance,0);
}
else // 卸下HOOK
UnhookWindowsHookEx(hKeyHook);
}
//----------------------------------------------------------------------------------------------------
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
// 向调用窗体发消息HOOKAPI_FIRSTCOMMAND和虚拟键码(char)(keyMSG->paramL)
EVENTMSG *keyMSG=(EVENTMSG *)lParam;
if((nCode==HC_ACTION)&&(keyMSG->message==WM_KEYUP))
PostMessage(hWndMain,HOOKAPI_FIRSTCOMMAND,(char)(keyMSG->paramL),HOOKAPI_SECONDPARAM);
return((int)CallNextHookEx(hKeyHook,nCode,wParam,lParam));
}
//----------------------------------------------------------------------------------------------------
调用
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainUnit.h"
static HINSTANCE hDLL; // DLL句柄
typedef void __stdcall (*DLLFUN)(HWND,bool);
DLLFUN DLLSetHook;
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204)
#define HOOKAPI_SECONDPARAM (WM_USER + 205)
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
hDLL=LoadLibrary((LPCTSTR)"Hookkey.dll"); // DLL文件名:Project1.dll
if(hDLL==NULL)
{
ShowMessage("DLL: 不能加载!程序退出。");
exit(1);
}
DLLSetHook =(DLLFUN)GetProcAddress(hDLL,"SetHook");
if(DLLSetHook==NULL)
{
ShowMessage("DLL: 函数没找到!程序退出。");
FreeLibrary(hDLL);
exit(1);
}
DLLSetHook(this->Handle,true);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
DLLSetHook(NULL,false); // 卸下HOOK
FreeLibrary(hDLL); // 卸下DLL
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
{
if(( Msg.message == HOOKAPI_FIRSTCOMMAND )&&(Msg.lParam==HOOKAPI_SECONDPARAM))
ShowMessage(" 收到HOOK按键消息!\n\n 【键虚拟码】:"+IntToStr(Msg.wParam));
}
//---------------------------------------------------------------------------
#9
不明白程序怎么这么麻烦,我写过一个一键启动程序的东东,你想要就看看吧
#10
lpFarProc=GetProcAddress(DllHandle,funname);
if (i==0)
{
pFarProc();//这里可以运行单没反应
少一个l当然没反映
还有LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
不太可能吧
if (i==0)
{
pFarProc();//这里可以运行单没反应
少一个l当然没反映
还有LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
不太可能吧
#11
各位大师,到这边看看吧...
http://expert.csdn.net/Expert/topic/1617/1617428.xml?temp=.6163294
http://expert.csdn.net/Expert/topic/1617/1617428.xml?temp=.6163294
#12
HUANG_JH(偶尔灌水,在体验广州的深度与湿度的民工)
编译过后这么多错 怎么搞得?
[Linker Error] Unresolved external '__BeforeDestruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::~AnsiString()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassDestroy' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassCreate' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const System::AnsiString&)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__AfterConstruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const char *)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__InitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
[Linker Error] Unresolved external '__ExitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
编译过后这么多错 怎么搞得?
[Linker Error] Unresolved external '__BeforeDestruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::~AnsiString()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassDestroy' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassCreate' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const System::AnsiString&)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__AfterConstruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const char *)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__InitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
[Linker Error] Unresolved external '__ExitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
#13
天啊,怎么这么多错误???
我在bcb5.0下用的好好的.......
另外补充:
做一个def文件 ,该文件名与DllEntryPoint所在的cpp文件名要一致,如你的cpp叫hookkey.cpp,那么该def文件叫hookkey.def
def文件 内容如下:
SEGMENTS SHSEG CLASS 'SHCLASS' SHARED
然后添加到工程中去,再编译
我在bcb5.0下用的好好的.......
另外补充:
做一个def文件 ,该文件名与DllEntryPoint所在的cpp文件名要一致,如你的cpp叫hookkey.cpp,那么该def文件叫hookkey.def
def文件 内容如下:
SEGMENTS SHSEG CLASS 'SHCLASS' SHARED
然后添加到工程中去,再编译
#1
// FARPROC lpfnKeyHook=(FARPROC)KeyProc; 这行不要注释也不可以
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
原来直接写的
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyProc,hInst,0)))
都不可以
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
原来直接写的
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyProc,hInst,0)))
都不可以
#2
你的hInst没有符初值
#3
static 在dll中有用么。反正我在vc下作的时候,钩子handle是放在一个共享的数据段里才行的。
#4
你这么写,加:
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst=hinst;
return 1;
}
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst=hinst;
return 1;
}
#5
是starR(); 没有调用还是安装钩子失败,如果是后者,用sprewellkobe(我为软件狂!) 的方法。
hhk不放在共享数据段也可以的。
hhk不放在共享数据段也可以的。
#6
昨晚代码就修改了:
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
static HINSTANCE hInst;
static HHOOK hhk;
extern "C" __declspec(dllexport) int kkk();
extern "C" __declspec(dllexport) bool BeginR();
extern "C" __declspec(dllexport) bool StopR();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst = hinst ;// 已经改过了 调试已经负值了
return 1;
}
LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
bool BeginR()
{
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
{
return false;//hhk 返回不为NULL 是一个数字。
}
return true;
}
bool StopR()
{
return UnhookWindowsHookEx(hhk);
}
int kkk()
{ int i ;
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0);
i=GetLastError();
return i;//在这里测试的返回 0
}
结果3个函数都能运行,但根本没有任何安装hook的效果。
调用处的代码:
bool loaddll(const char* dllname,const char* funname,int i)
{
try{
HINSTANCE DllHandle;
FARPROC lpFarProc;
DllHandle=LoadLibrary(dllname);
lpFarProc=GetProcAddress(DllHandle,funname);
if (i==0)
{
pFarProc();//这里可以运行单没反应
// MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
}else if(i==1)
{
Form1->Edit1->Text=IntToStr(lpFarProc());
}
FreeLibrary(DllHandle);
return true ;
}
catch(Exception &exception)
{
return false;
}
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_BeginR",0);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_kkk",1);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_StopR",0);
}
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
static HINSTANCE hInst;
static HHOOK hhk;
extern "C" __declspec(dllexport) int kkk();
extern "C" __declspec(dllexport) bool BeginR();
extern "C" __declspec(dllexport) bool StopR();
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
hInst = hinst ;// 已经改过了 调试已经负值了
return 1;
}
LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
bool BeginR()
{
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
if(!(hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0)))
{
return false;//hhk 返回不为NULL 是一个数字。
}
return true;
}
bool StopR()
{
return UnhookWindowsHookEx(hhk);
}
int kkk()
{ int i ;
FARPROC lpfnKeyHook=(FARPROC)KeyProc;
hhk=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)lpfnKeyHook,hInst,0);
i=GetLastError();
return i;//在这里测试的返回 0
}
结果3个函数都能运行,但根本没有任何安装hook的效果。
调用处的代码:
bool loaddll(const char* dllname,const char* funname,int i)
{
try{
HINSTANCE DllHandle;
FARPROC lpFarProc;
DllHandle=LoadLibrary(dllname);
lpFarProc=GetProcAddress(DllHandle,funname);
if (i==0)
{
pFarProc();//这里可以运行单没反应
// MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
}else if(i==1)
{
Form1->Edit1->Text=IntToStr(lpFarProc());
}
FreeLibrary(DllHandle);
return true ;
}
catch(Exception &exception)
{
return false;
}
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_BeginR",0);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_kkk",1);
}
void __fastcall TForm1::Button3Click(TObject *Sender)
{
loaddll("hookdll.dll","_StopR",0);
}
#7
不好意思:调用处
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
这几行不要,忘记删掉了。
另外:
谢谢:: glyc(大灰狼) : sprewellkobe(我为软件狂!): sjd107(phil)
:) 不过还没解决问题呢
}else
{
MessageBox((HWND)hInst,"ok","系统警告",MB_OK);
Form1->Edit1->Text= "1";
}
这几行不要,忘记删掉了。
另外:
谢谢:: glyc(大灰狼) : sprewellkobe(我为软件狂!): sjd107(phil)
:) 不过还没解决问题呢
#8
全局键盘hook 不用这么麻烦吧??
hookkey.cpp
//---------------------------------------------------------------------------
#pragma option -zRSHSEG // 改变缺省数据段名
#pragma option -zTSHCLASS // 改变缺省数据类名
#include <windows.h>
//---------------------------------------------------------------------------
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204) // 自定义消息号
#define HOOKAPI_SECONDPARAM (WM_USER + 205) // 自定义消息第2参数
extern "C" __declspec(dllexport) void __stdcall SetHook(HWND,bool);
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam) ;
//---------------------------------------------------------------------------
#pragma argsused
static HINSTANCE hInstance; // 应用实例句柄
static HWND hWndMain; // MainForm句柄
static HHOOK hKeyHook; // HOOK句柄
int LoadCount=0; // DLL装入次数计数
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
if ( hInstance == NULL )
hInstance=hinst;
if(reason==DLL_PROCESS_ATTACH) // DLL入口
LoadCount+=1; // 装入计数
else if(reason==DLL_PROCESS_DETACH) // DLL出口处理
LoadCount-=1;
return 1;
}
//----------------------------------------------------------------------------------------------------
void __stdcall SetHook(HWND hMainWin,bool nCode)
{
/*
WINDOW下可进行挂接的过滤函数包括11种:
WH_CALLWNDPROC 窗口函数的过滤函数
WH_CBT 计算机培训过滤函数
WH_DEBUG 调试过滤函数
WH_GETMESSAGE 获取消息过滤函数
WH_HARDWARE 硬件消息过滤函数
WH_JOURNALPLAYBACK 消息重放过滤函数
WH_JOURNALRECORD 消息记录过滤函数
WH_MOUSE 鼠标过滤函数
WH_MSGFILTER 消息过滤函数
WH_SYSMSGFILTER 系统消息过滤函数
WH_KEYBOARD 键盘过滤函数
*/
if(nCode) // 安放HOOK
{
hWndMain=hMainWin;
hKeyHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)HookProc,hInstance,0);
}
else // 卸下HOOK
UnhookWindowsHookEx(hKeyHook);
}
//----------------------------------------------------------------------------------------------------
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
// 向调用窗体发消息HOOKAPI_FIRSTCOMMAND和虚拟键码(char)(keyMSG->paramL)
EVENTMSG *keyMSG=(EVENTMSG *)lParam;
if((nCode==HC_ACTION)&&(keyMSG->message==WM_KEYUP))
PostMessage(hWndMain,HOOKAPI_FIRSTCOMMAND,(char)(keyMSG->paramL),HOOKAPI_SECONDPARAM);
return((int)CallNextHookEx(hKeyHook,nCode,wParam,lParam));
}
//----------------------------------------------------------------------------------------------------
调用
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainUnit.h"
static HINSTANCE hDLL; // DLL句柄
typedef void __stdcall (*DLLFUN)(HWND,bool);
DLLFUN DLLSetHook;
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204)
#define HOOKAPI_SECONDPARAM (WM_USER + 205)
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
hDLL=LoadLibrary((LPCTSTR)"Hookkey.dll"); // DLL文件名:Project1.dll
if(hDLL==NULL)
{
ShowMessage("DLL: 不能加载!程序退出。");
exit(1);
}
DLLSetHook =(DLLFUN)GetProcAddress(hDLL,"SetHook");
if(DLLSetHook==NULL)
{
ShowMessage("DLL: 函数没找到!程序退出。");
FreeLibrary(hDLL);
exit(1);
}
DLLSetHook(this->Handle,true);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
DLLSetHook(NULL,false); // 卸下HOOK
FreeLibrary(hDLL); // 卸下DLL
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
{
if(( Msg.message == HOOKAPI_FIRSTCOMMAND )&&(Msg.lParam==HOOKAPI_SECONDPARAM))
ShowMessage(" 收到HOOK按键消息!\n\n 【键虚拟码】:"+IntToStr(Msg.wParam));
}
//---------------------------------------------------------------------------
hookkey.cpp
//---------------------------------------------------------------------------
#pragma option -zRSHSEG // 改变缺省数据段名
#pragma option -zTSHCLASS // 改变缺省数据类名
#include <windows.h>
//---------------------------------------------------------------------------
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204) // 自定义消息号
#define HOOKAPI_SECONDPARAM (WM_USER + 205) // 自定义消息第2参数
extern "C" __declspec(dllexport) void __stdcall SetHook(HWND,bool);
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam) ;
//---------------------------------------------------------------------------
#pragma argsused
static HINSTANCE hInstance; // 应用实例句柄
static HWND hWndMain; // MainForm句柄
static HHOOK hKeyHook; // HOOK句柄
int LoadCount=0; // DLL装入次数计数
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
if ( hInstance == NULL )
hInstance=hinst;
if(reason==DLL_PROCESS_ATTACH) // DLL入口
LoadCount+=1; // 装入计数
else if(reason==DLL_PROCESS_DETACH) // DLL出口处理
LoadCount-=1;
return 1;
}
//----------------------------------------------------------------------------------------------------
void __stdcall SetHook(HWND hMainWin,bool nCode)
{
/*
WINDOW下可进行挂接的过滤函数包括11种:
WH_CALLWNDPROC 窗口函数的过滤函数
WH_CBT 计算机培训过滤函数
WH_DEBUG 调试过滤函数
WH_GETMESSAGE 获取消息过滤函数
WH_HARDWARE 硬件消息过滤函数
WH_JOURNALPLAYBACK 消息重放过滤函数
WH_JOURNALRECORD 消息记录过滤函数
WH_MOUSE 鼠标过滤函数
WH_MSGFILTER 消息过滤函数
WH_SYSMSGFILTER 系统消息过滤函数
WH_KEYBOARD 键盘过滤函数
*/
if(nCode) // 安放HOOK
{
hWndMain=hMainWin;
hKeyHook=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)HookProc,hInstance,0);
}
else // 卸下HOOK
UnhookWindowsHookEx(hKeyHook);
}
//----------------------------------------------------------------------------------------------------
LRESULT CALLBACK HookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
// 向调用窗体发消息HOOKAPI_FIRSTCOMMAND和虚拟键码(char)(keyMSG->paramL)
EVENTMSG *keyMSG=(EVENTMSG *)lParam;
if((nCode==HC_ACTION)&&(keyMSG->message==WM_KEYUP))
PostMessage(hWndMain,HOOKAPI_FIRSTCOMMAND,(char)(keyMSG->paramL),HOOKAPI_SECONDPARAM);
return((int)CallNextHookEx(hKeyHook,nCode,wParam,lParam));
}
//----------------------------------------------------------------------------------------------------
调用
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainUnit.h"
static HINSTANCE hDLL; // DLL句柄
typedef void __stdcall (*DLLFUN)(HWND,bool);
DLLFUN DLLSetHook;
#define HOOKAPI_FIRSTCOMMAND (WM_USER + 204)
#define HOOKAPI_SECONDPARAM (WM_USER + 205)
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
hDLL=LoadLibrary((LPCTSTR)"Hookkey.dll"); // DLL文件名:Project1.dll
if(hDLL==NULL)
{
ShowMessage("DLL: 不能加载!程序退出。");
exit(1);
}
DLLSetHook =(DLLFUN)GetProcAddress(hDLL,"SetHook");
if(DLLSetHook==NULL)
{
ShowMessage("DLL: 函数没找到!程序退出。");
FreeLibrary(hDLL);
exit(1);
}
DLLSetHook(this->Handle,true);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
DLLSetHook(NULL,false); // 卸下HOOK
FreeLibrary(hDLL); // 卸下DLL
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ApplicationEvents1Message(tagMSG &Msg,
bool &Handled)
{
if(( Msg.message == HOOKAPI_FIRSTCOMMAND )&&(Msg.lParam==HOOKAPI_SECONDPARAM))
ShowMessage(" 收到HOOK按键消息!\n\n 【键虚拟码】:"+IntToStr(Msg.wParam));
}
//---------------------------------------------------------------------------
#9
不明白程序怎么这么麻烦,我写过一个一键启动程序的东东,你想要就看看吧
#10
lpFarProc=GetProcAddress(DllHandle,funname);
if (i==0)
{
pFarProc();//这里可以运行单没反应
少一个l当然没反映
还有LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
不太可能吧
if (i==0)
{
pFarProc();//这里可以运行单没反应
少一个l当然没反映
还有LRESULT CALLBACK KeyProc(int nCode,WPARAM wParam,LPARAM lParam)
{
MessageBox((HWND)hInst,"测试!!!!!!!","系统警告",MB_OK);
return CallNextHookEx(hhk,nCode,wParam,lParam);//单步跟踪调试这里从来没运行过
}
不太可能吧
#11
各位大师,到这边看看吧...
http://expert.csdn.net/Expert/topic/1617/1617428.xml?temp=.6163294
http://expert.csdn.net/Expert/topic/1617/1617428.xml?temp=.6163294
#12
HUANG_JH(偶尔灌水,在体验广州的深度与湿度的民工)
编译过后这么多错 怎么搞得?
[Linker Error] Unresolved external '__BeforeDestruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::~AnsiString()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassDestroy' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassCreate' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const System::AnsiString&)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__AfterConstruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const char *)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__InitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
[Linker Error] Unresolved external '__ExitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
编译过后这么多错 怎么搞得?
[Linker Error] Unresolved external '__BeforeDestruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::~AnsiString()' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassDestroy' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__ClassCreate' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const System::AnsiString&)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__AfterConstruction' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__fastcall System::AnsiString::AnsiString(const char *)' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|xx
[Linker Error] Unresolved external '__InitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
[Linker Error] Unresolved external '__ExitVCL' referenced from D:\PROGRAM FILES\BORLAND\CBUILDER6\LIB\CP32MT.LIB|crtlvcl
#13
天啊,怎么这么多错误???
我在bcb5.0下用的好好的.......
另外补充:
做一个def文件 ,该文件名与DllEntryPoint所在的cpp文件名要一致,如你的cpp叫hookkey.cpp,那么该def文件叫hookkey.def
def文件 内容如下:
SEGMENTS SHSEG CLASS 'SHCLASS' SHARED
然后添加到工程中去,再编译
我在bcb5.0下用的好好的.......
另外补充:
做一个def文件 ,该文件名与DllEntryPoint所在的cpp文件名要一致,如你的cpp叫hookkey.cpp,那么该def文件叫hookkey.def
def文件 内容如下:
SEGMENTS SHSEG CLASS 'SHCLASS' SHARED
然后添加到工程中去,再编译