Hook的问题(200分)

  • 主题发起人 主题发起人 xinhe
  • 开始时间 开始时间
X

xinhe

Unregistered / Unconfirmed
GUEST, unregistred user!
<br>&nbsp;很奇怪的一个问题,以前竟然没注意到。随便设一个Hook DLL,在EXE中调用。<br>&nbsp;如果这个程序是从IE中启动的(打开“我的电脑”,选到程序的那个目录,双点EXE<br>&nbsp;文件,或用文件管理器选择双点EXE),程序启动并设置好Hook函数后,去关闭启动<br>&nbsp;程序的IE窗口时会出错,并不是每次都出,但一般每三四次就会出一次,提示““该<br>&nbsp;程序招待了非法操作,即将关闭”和“Cannot run multiple instances of a DLL<br>&nbsp;under WIN32s”<br><br>&nbsp;以下是我用来测试的DLL,其实就是随便写的一个最简单的Hook而已。<br><br>#define DLLEXPORT extern "C" __declspec(dllexport)<br><br>LRESULT CALLBACK MyHookProc(int nCode,WPARAM wParam,LPARAM lParam);<br>HHOOK ghHook=NULL;<br>HINSTANCE ghDllInst=NULL;<br><br>#pragma argsused<br>int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)<br>{<br>&nbsp; &nbsp; ghDllInst=hinst;<br>&nbsp; &nbsp; return 1;<br>}<br>//---------------------------------------------------------------------------<br>DLLEXPORT BOOL WINAPI UnInstallHook(void)<br>{<br>&nbsp; &nbsp; return UnhookWindowsHookEx(ghHook);<br>}<br><br>DLLEXPORT BOOL WINAPI InstallHook(HINSTANCE hInst)<br>{<br>&nbsp; &nbsp; ghHook = SetWindowsHookEx( WH_MOUSE,(HOOKPROC)MyMenuHookProc,ghDllInst,NULL);<br>&nbsp; &nbsp; if(ghHook==NULL)return FALSE;<br>&nbsp; &nbsp; else return TRUE;<br>}<br><br>LRESULT CALLBACK MyHookProc(int nCode,WPARAM wParam,LPARAM lParam)<br>{<br>&nbsp; &nbsp; if(nCode&lt;0)return CallNextHookEx(ghHook, nCode, wParam, lParam);<br>&nbsp; &nbsp; MessageBeep(-1);<br>&nbsp; &nbsp; return CallNextHookEx(ghHook, nCode, wParam, lParam);<br>}<br><br>应用程序就只是放了一个按钮,调用了一下InstallHook函数,这里就不写了。<br>程序是用BCB5写的。<br>
 
//SetWindowsHookEx( WH_MOUSE,(HOOKPROC)MyMenuHookProc,ghDllInst,NULL);<br>写成下面怎麽样?<br>SetWindowsHookEx( WH_MOUSE,(HOOKPROC)MyMenuHookProc,hInstance,NULL);<br><br>ghDllInst没有定义在象VC那样的共享数据段里.<br>如:<br>#pragma data_seg("CommonMem")<br>HINSTANCE ghDllInst=NULL;<br>#pragma data_seg()<br><br>
 
同意,如果还是不行的话就试下<br>SetWindowsHookEx( WH_MOUSE,(HOOKPROC)MyMenuHookProc,NULL,::GetCurrentThreadId())
 
<br>&nbsp;您的hInstance参数是从哪里来的?<br>&nbsp;ghDllInst没办法定义在共享数据段里(BCB里没找到办法),而且又不能用<br>&nbsp;FileMapping。怎么办呢?<br><br>
 
用.DEF文件,加入工程(BCB)
 
忘了大小写,应是 HInstance
 
你的dll回运行在其他进程,所以 ghHook 在其他进程是未初始化的值<br>应该使用 CreateFileMapping 将 ghHook 保存在进程间共享的地方
 
<br>&nbsp;按o*o、lww的方法试过,还是出同样的错误。我会再试试Pipi的方法。<br><br>&nbsp;另外hustmouse是说的共享数据段的方法吗?.DEF文件里我知道怎么做,但我实在不知道<br>&nbsp;在BCB里如何定义数据段。按Borland的FAQ所说的方法试过,根本没有他所说的那个<br>&nbsp;#pragma option 选项。而且似乎用这种共享数据段的方法不适用于NT/2000,我原来用<br>&nbsp;VC写过DLL,用共享数据段,在95/98里运行正常,但在NT/2000下却无法正常运行。<br><br>&nbsp;试试各位的回答,有时间的话帮我试试看好吗?解决的话一定重谢(以后慢慢谢,刚知<br>&nbsp;道这个宝地,200分全用上了 :P)。就是一个最普通的Mouse Hook,无论如何想不通竟会<br>&nbsp;出错。我的运行环境:Win98简体中文版。试过两台机器都会出错。(不会是有病毒吧?)<br><br>&nbsp;再有那句:<br>&nbsp;ghHook = SetWindowsHookEx( WH_MOUSE,(HOOKPROC)MyMenuHookProc,ghDllInst,NULL);<br>&nbsp;应为:<br>&nbsp;ghHook = SetWindowsHookEx( WH_MOUSE,(HOOKPROC)MyHookProc,ghDllInst,NULL);<br>
 
<br>&nbsp;Pipi的方法也试了,还是一样的错误,真要昏过去了。代码如下:<br><br>DLLEXPORT BOOL WINAPI UnInstallHook(void)<br>{<br>&nbsp; &nbsp; HANDLE hMapFile;<br><br>&nbsp; &nbsp; hMapFile=OpenFileMapping(FILE_MAP_ALL_ACCESS,TRUE,"abcde");<br>&nbsp; &nbsp; if(hMapFile==NULL)return FALSE;<br> HHOOK *pHook=(HHOOK*)MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,sizeof(HHOOK));<br>&nbsp; &nbsp; if(pHook==NULL){<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(hMapFile);<br>&nbsp; &nbsp; &nbsp; &nbsp; return FALSE;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; HHOOK MouseHook=*pHook;<br><br>&nbsp; &nbsp; UnmapViewOfFile(pHook);<br>&nbsp; &nbsp; CloseHandle(hMapFile);<br><br>&nbsp; &nbsp; return UnhookWindowsHookEx(MouseHook);<br>}<br><br>DLLEXPORT BOOL WINAPI InstallHook(HINSTANCE hInst)<br>{<br>&nbsp; &nbsp; HANDLE hMapFile;<br><br>&nbsp; &nbsp; hMapFile=OpenFileMapping(FILE_MAP_ALL_ACCESS,TRUE,"abcde");<br>&nbsp; &nbsp; if(hMapFile==NULL)return FALSE;<br>&nbsp; &nbsp; HHOOK *pHook=(HHOOK*)MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,sizeof(HHOOK));<br>&nbsp; &nbsp; if(pHook==NULL){<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(hMapFile);<br>&nbsp; &nbsp; &nbsp; &nbsp; return FALSE;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; *pHook = SetWindowsHookEx( WH_MOUSE,(HOOKPROC)MyHookProc,ghDllInst,NULL);<br><br>&nbsp; &nbsp; UnmapViewOfFile(pHook);<br>&nbsp; &nbsp; CloseHandle(hMapFile);<br>&nbsp; &nbsp; if(*pHook)return TRUE;<br>&nbsp; &nbsp; else return FALSE;<br>}<br><br>LRESULT CALLBACK MyHookProc(int nCode,WPARAM wParam,LPARAM lParam)<br>{<br>&nbsp; &nbsp; HANDLE hMapFile;<br><br>&nbsp; &nbsp; hMapFile=OpenFileMapping(FILE_MAP_ALL_ACCESS,TRUE,"abcde");<br>&nbsp; &nbsp; if(hMapFile==NULL)return FALSE;<br>&nbsp; &nbsp; HHOOK *pHook=(HHOOK*)MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,sizeof(HHOOK));<br>&nbsp; &nbsp; if(pHook==NULL){<br>&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(hMapFile);<br>&nbsp; &nbsp; &nbsp; &nbsp; return FALSE;<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; HHOOK MouseHook=*pHook;<br><br>&nbsp; &nbsp; UnmapViewOfFile(pHook);<br>&nbsp; &nbsp; CloseHandle(hMapFile);<br><br>&nbsp; &nbsp; if(nCode&lt;0)CallNextHookEx(MouseHook, nCode, wParam, lParam);<br>&nbsp; &nbsp; MessageBeep(-1);<br>&nbsp; &nbsp; return CallNextHookEx(MouseHook, nCode, wParam, lParam);<br>}<br><br>EXE里设置FileMapping: <br><br>&nbsp; &nbsp; HANDLE hMapFile;<br>&nbsp; &nbsp; hMapFile=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,sizeof(HHOOK),"abcde");<br>&nbsp; &nbsp; if(hMapFile==NULL)return;<br><br>
 
还是用共享数据段吧<br><br>在连接开关中加上 /SECTION:mydata,RWS
 
<br>&nbsp;hi, 哪位帮我试试看,让我知道是不是只有我这里才会出错好吗?随便设一个Mouse的<br>&nbsp;Hook就行。在“我的电脑”里启动程序,启动Hook后关闭IE窗口,三四次后就会出错的。<br><br>&nbsp;试验完毕马上加分了。<br>&nbsp;//bow<br>
 
附加功能 &nbsp; 将问题提前
 
我以前边的一个hook:<br>//---------------------------------------------------------------------------<br>#include &lt;vcl.h&gt;<br>#define IMGDLL<br>#include "usedll.h"<br>#pragma hdrstop<br>//---------------------------------------------------------------------------<br>// &nbsp; Important note about DLL memory management when your DLL uses the<br>// &nbsp; static version of the RunTime Library:<br>//<br>// &nbsp; If your DLL exports any functions that pass String objects (or structs/<br>// &nbsp; classes containing nested Strings) as parameter or function results,<br>// &nbsp; you will need to add the library MEMMGR.LIB to both the DLL project and<br>// &nbsp; any other projects that use the DLL. &nbsp;You will also need to use MEMMGR.LIB<br>// &nbsp; if any other projects which use the DLL will be perfomring new or delete<br>// &nbsp; operations on any non-TObject-derived classes which are exported from the<br>// &nbsp; DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling<br>// &nbsp; EXE's to use the BORLNDMM.DLL as their memory manager. &nbsp;In these cases,<br>// &nbsp; the file BORLNDMM.DLL should be deployed along with your DLL.<br>//<br>// &nbsp; To avoid using BORLNDMM.DLL, pass string information using "char *" or<br>// &nbsp; ShortString parameters.<br>//<br>// &nbsp; If your DLL uses the dynamic version of the RTL, you do not need to<br>// &nbsp; explicitly add MEMMGR.LIB as this will be done implicitly for you<br>//---------------------------------------------------------------------------<br>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>int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)<br>{<br>&nbsp; &nbsp; &nbsp; &nbsp; return 1;<br>}<br>//---------------------------------------------------------------------------<br>以上代码运行时不会出现““该<br>&nbsp;程序招待了非法操作,即将关闭”和“Cannot run multiple instances of a DLL<br>&nbsp;under WIN32s”的错误,但内存单元共享不管用,不止为何,还望各位指教<br>
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
625
import
I
后退
顶部