紧急求助:问个在VC中钩子的问题:(30分)

  • 主题发起人 主题发起人 netbug
  • 开始时间 开始时间
N

netbug

Unregistered / Unconfirmed
GUEST, unregistred user!
我想编写个对键盘的钩子函数:<br>我先编写了个DLL文件,请看:<br>这是cpp文件:<br>#include "windows.h"<br>HHOOK hhook;<br>LRESULT CALLBACK ProcKB(int nCode,WPARAM wParam,LPARAM lParam)<br>{<br> if(nCode&lt;0)<br> return CallNextHookEx(hhook,nCode,wParam,lParam);<br> else<br> {<br> MessageBeep(7);<br> return CallNextHookEx(hhook,nCode,wParam,lParam);<br> }<br>}<br>这是def文件:<br>LIBRARY "bbDll"<br>EXPORTS<br>ProcKB<br>并编译成功且形成了bbDll.dll文件,我把此dll文件拷到以下文件所在的目录下。<br>接着我用MFC形成了个单文档文件的框架,我只在MainFrm.cpp文件添加了一些:<br>// CMainFrame<br>HHOOK hhook;<br>typedef LRESULT (*PROCKB)(int,WPARAM,LPARAM);<br>PROCKB hkprcXX;<br>HINSTANCE hinstDLL;<br>int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)<br>{ &nbsp; //原先的代码<br> // TODO: Delete these three lines if you don't want the toolbar to<br> // &nbsp;be dockable<br>&nbsp; &nbsp; hinstDLL=LoadLibrary("bbDll.dll");<br>&nbsp; &nbsp; hkprcXX=(PROCKB)GetProcAddress(hinstDLL,"ProcKB");<br>&nbsp; &nbsp; hhook=SetWindowsHookEx(WH_KEYBOARD,hkprcXX,hinstDLL,0);<br>//原先的代码<br>}<br>可在编译时,老是提示:<br>e:/Vc++/bbTest/MainFrm.cpp(79) : error C2664: 'SetWindowsHookExA' : cannot convert parameter 2 from 'long (__cdecl *)(int,unsigned int,long)' to 'long (__stdcall *)(int,unsigned int,long)'<br>&nbsp; &nbsp; &nbsp; &nbsp; This conversion requires a reinterpret_cast, a C-style cast or function-style cast<br>也就是hhook=SetWindowsHookEx(WH_KEYBOARD,hkprcXX,hinstDLL,0)错误,我不知错在哪?<br>请各位大虾帮助、指正,谢谢。<br>
 
第一种方法:<br>去掉:typedef LRESULT (*PROCKB)(int,WPARAM,LPARAM);<br>改为:<br>HOOKPROC hkprcXX;<br>hkprcXX=(HOOKPROC)GetProcAddress(hinstDLL,"ProcKB");<br>SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)hkprcXX,hinstDLL,0)<br>第二种方法:<br>你这样写typedef:<br>typedef LRESULT (CALLBACK *PROCKB) (int,WPARAM,LPARAM);<br><br>
 
接受答案了.
 
后退
顶部