如何在CBuilder中实现一个简单的鼠标钩子?(100分)

  • 主题发起人 主题发起人 Bench
  • 开始时间 开始时间
B

Bench

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;各位大虾:<br>&nbsp; 为何我在定义钩子过程函数时只能定义成int (_stdcall *)() 型的而不是<br>&nbsp;LRESULT CALLBACK MouseProc(int Code,WPARAM,LPARAM);型的?<br>&nbsp;
 
&nbsp;上一问题通过函数强制转换可以解决,但在定义共享数据段时又遇到了问题。我的定义方法如下:<br>在主函数头部:<br>#pragma data_seg("mydata")<br>HWND hHook=NULL;<br>&nbsp; &nbsp; &nbsp;.<br>&nbsp; &nbsp; &nbsp;.<br>&nbsp; &nbsp; &nbsp;.<br>#pragma data_seg()<br>在.def文件中定义如下:<br>SECTIONS mydata READ WRITE SHARED<br>结果在编译时出现如下提示错误:<br>Section mydata defined in .def file is empty<br>看来CBuilder 好象不支持data_seg的定义方式。<br>不知哪位高手可以赐教在CBuilder中定义共享数据段的方法?<br>在下万分感激!!!!
 
我只知在Delphi上怎么作。
 
&nbsp; 附加功能 &nbsp; 将问题提前 &nbsp; &nbsp;
 
const char *hook_map="sample hook memflag";<br>const char *hook_mux="Hook MUtex";<br>HANDLE memfile,HookMutex;<br>LPVOID lpMapAddress;<br>DWORD dwnumber;<br>HHOOK hNextHookProc;<br>HOOKPROC TMP;<br>LRESULT CALLBACK &nbsp;KeyboardProc(int code, WPARAM wParam, LPARAM lParam)<br>&nbsp;{ &nbsp;const<br>&nbsp; _KeyPressMask = 0x80000000;<br>&nbsp; &nbsp; &nbsp;int Result=0;<br>&nbsp; &nbsp; if (code&lt;0)<br>&nbsp; &nbsp; &nbsp;Result = CallNextHookEx(hNextHookProc, code, wParam, lParam);<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; { if((lParam &amp; _KeyPressMask)== 0 )<br>&nbsp; &nbsp; &nbsp;//ShowMessage("OK");<br>&nbsp; &nbsp; &nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; &nbsp; dwnumber = *((LPDWORD) lpMapAddress);<br>&nbsp; &nbsp; &nbsp; &nbsp; ++dwnumber;<br>&nbsp; &nbsp; &nbsp; &nbsp; *((LPDWORD) lpMapAddress) = dwnumber;<br>&nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp;return Result;<br>}<br>bool _stdcall EnableHotKeyHook()<br>{ &nbsp;TMP=(HOOKPROC)KeyboardProc;<br>&nbsp; bool Result=false;<br>&nbsp; if (hNextHookProc==0)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp;hNextHookProc = SetWindowsHookEx(WH_KEYBOARD,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TMP,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HInstance,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0);<br>&nbsp; &nbsp; &nbsp;Result=(hNextHookProc!=0);<br>&nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;HookMutex=CreateMutex(NULL,true,hook_mux);<br>&nbsp; &nbsp; &nbsp;memfile=OpenFileMapping(FILE_MAP_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;false,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hook_map);<br>&nbsp; &nbsp; &nbsp;if (memfile==NULL)<br>&nbsp; &nbsp; &nbsp;memfile=CreateFileMapping( (HANDLE)0xFFFFFFFF,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PAGE_READWRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sizeof(LPVOID),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hook_map);<br><br>&nbsp; &nbsp; &nbsp; lpMapAddress=MapViewOfFile(memfile,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_MAP_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0);<br>&nbsp; &nbsp; &nbsp; ReleaseMutex(HookMutex);<br>&nbsp; &nbsp; &nbsp; CloseHandle(HookMutex);<br>&nbsp; &nbsp; &nbsp; *((LPDWORD) lpMapAddress)=0;<br>&nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; }<br>&nbsp;return Result;<br>}<br>bool _stdcall DisableHotKeyHook()<br>{ &nbsp; bool Result=false;<br>if (hNextHookProc!=0)<br>&nbsp; {<br>&nbsp; &nbsp; UnhookWindowsHookEx(hNextHookProc);<br>&nbsp; &nbsp; hNextHookProc = 0;<br>&nbsp; &nbsp; Result=(hNextHookProc == 0);<br>&nbsp; &nbsp; if (memfile!=NULL)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; UnmapViewOfFile(lpMapAddress);<br>&nbsp; &nbsp; CloseHandle(memfile);<br>&nbsp; &nbsp; }<br>&nbsp; }<br>&nbsp; return Result;<br>}<br>DWORD _stdcall keynumber()<br>{<br>DWORD number;<br>number=*((LPDWORD) lpMapAddress);<br>return number;<br>}<br>数据共享应用内存映射文件来完成,以上代码hook好使,但数据共享不管用,不知为何?
 
接受答案了.
 
后退
顶部