我实在不行了,关于HOOK ( 积分: 200 )

  • 主题发起人 主题发起人 ralfa
  • 开始时间 开始时间
R

ralfa

Unregistered / Unconfirmed
GUEST, unregistred user!
我查了好多资料和在这里翻查了好久的帖子,但是还是无法做的出来<br><br>我要做的其实以前好多帖子都提出过了的&nbsp;,但是都没确切的代码可以提供参考<br>就是&nbsp;如果我需要监视&nbsp;系统窗口的&nbsp;创建/焦点变更/关闭&nbsp;一类事件&nbsp;该如何写呢<br>下面是我写的测试DLL&nbsp;目的是取得其他激活的窗口的句柄,但是好象失败了,来来去去都是在同一返回值;<br>library&nbsp;ProjectDllTest;<br>uses<br>&nbsp;&nbsp;SysUtils,<br>&nbsp;&nbsp;Windows,<br>&nbsp;&nbsp;Classes,<br>&nbsp;&nbsp;IniFiles,<br>&nbsp;&nbsp;Messages;<br><br>{$R&nbsp;*.res}<br><br>var<br>&nbsp;&nbsp;&nbsp;CBTHandle:&nbsp;HHOOK;//hook<br>&nbsp;&nbsp;masin:integer;//回传变量<br>&nbsp;&nbsp;master_masin:integer;<br><br>&nbsp;&nbsp;//------------------<br>function&nbsp;sets(masters:integer)&nbsp;:integer;stdcall;export;<br>begin<br>master_masin:=masters;<br>end;<br>&nbsp;function&nbsp;gets():integer;stdcall;export;<br>&nbsp;&nbsp;begin<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result:=masin;<br><br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//---------------------------&nbsp;CBT&nbsp;Hook&nbsp;test&nbsp;---------------------------<br>//&nbsp;CBT(computer-based&nbsp;training&nbsp;)全局钩子测试函数<br>function&nbsp;CBTProc(nCode:&nbsp;Integer;wParam:&nbsp;WPARAM;&nbsp;lParam:&nbsp;LPARAM):&nbsp;LRESULT;&nbsp;stdcall;<br>var&nbsp;now_masin:integer;<br>begin<br>&nbsp;&nbsp;if(nCode&nbsp;=&nbsp;HCBT_ACTIVATE)&nbsp;then<br>&nbsp;&nbsp;begin<br> now_masin:=GetForegroundWindow;<br> if(now_masin&lt;&gt;master_masin)&nbsp;then<br> begin<br> masin:=now_masin;<br> end;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;CallNextHookEx(CBTHandle,nCode,wParam,lParam);<br>end;<br><br>//&nbsp;设置CBT全局钩子<br>function&nbsp;EnableCBTHook():&nbsp;Boolean;&nbsp;stdcall;&nbsp;export;<br>begin<br><br>&nbsp;&nbsp;cbtHandle&nbsp;:=&nbsp;SetWindowsHookEx(WH_cbt,@cbtProc,hInstance,0);<br>&nbsp;&nbsp;&nbsp;if(&nbsp;cbtHandle&nbsp;=&nbsp;0)&nbsp;then&nbsp;Result&nbsp;:=&nbsp;False<br>&nbsp;&nbsp;else&nbsp;Result&nbsp;:=&nbsp;True;<br>end;<br><br>//取消&nbsp;CBT全局钩子<br>function&nbsp;DisableCBTHook():&nbsp;Boolean;&nbsp;stdcall;&nbsp;export;<br>begin<br>&nbsp;&nbsp;&nbsp;if(&nbsp;cbtHandle&nbsp;&lt;&gt;&nbsp;0)&nbsp;then&nbsp;UnhookWindowsHookEx(cbtHandle);<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;True;<br>end;<br><br>exports<br>&nbsp;&nbsp;EnableCBTHook,DisableCBTHook&nbsp;,gets,sets;<br>end.
 
补充下&nbsp;难道MOUSEHOOK&nbsp;KEYBOADHOOK和&nbsp;CBT,SHELL那些HOOK写法不同的么?<br><br>我看见铺天盖地都是MOUSE和KB的例子&nbsp;我也试过试可以的,但是按照MOUSE和KB的样子写CBT和SHELL却万万不行&nbsp;但是偏偏又找不到CBT和SHELL的&nbsp;完整例子&nbsp;真郁闷死人了
 
Value&nbsp;HCBT_ACTIVATE&nbsp;<br>wParam&nbsp;&nbsp;&nbsp;Specifies&nbsp;the&nbsp;handle&nbsp;to&nbsp;the&nbsp;window&nbsp;about&nbsp;to&nbsp;be&nbsp;activated.<br>lParam&nbsp;&nbsp;&nbsp;Specifies&nbsp;a&nbsp;long&nbsp;pointer&nbsp;to&nbsp;a&nbsp;CBTACTIVATESTRUCT&nbsp;structure&nbsp;containing&nbsp;the&nbsp;handle&nbsp;to&nbsp;the&nbsp;active&nbsp;window&nbsp;and&nbsp;specifies&nbsp;whether&nbsp;the&nbsp;activation&nbsp;is&nbsp;changing&nbsp;because&nbsp;of&nbsp;a&nbsp;mouse&nbsp;click.&nbsp;&nbsp;<br>&nbsp;看msdn,激活窗口的句柄应该取值于wParam
 
我也用WPARAN过&nbsp;但是出来和&nbsp;GetForegroundWindow&nbsp;一样&nbsp;不是0就是一个固定的句柄
 
不过我直接获取事件<br>就是吧&nbsp;&nbsp;if(nCode&nbsp;=&nbsp;HCBT_ACTIVATE)&nbsp;then&nbsp;去掉&nbsp;改获取&nbsp;NCODE&nbsp;的话&nbsp;每次切换都只能获得一次获得&nbsp;4和8&nbsp;就是说&nbsp;我先点其他窗口再点回主程序&nbsp;钩子只能得到一次切换.....而不是2次..是不是我的代码哪里错了?
 
你用gets&nbsp;sets函数来取的DLL里的变量值吗???<br>在调用DLL的程序了你用了这两个函数了吗?<br>如果是这样肯定不行的,内存不一样,你取出来的值是0<br>如果你想和DLL共享变量的话,要用到文件内存映射<br>如果你有需要的话&nbsp;我就贴出来<br><br>我看了看你上面的代码,钩子思想已经没有错了,但是变量的使用上还有问题
 
呵呵,写钩子肯定会遇到这类问题的。<br>我以前写系统钩子的时候也是这样的。<br>做一个数据共享段,在exe和dll之间共享,然后发送消息过去就可以的。
 
哦?是么?那拜托贴来看看&nbsp;谢谢<br>我的确是用了GETS来获得DL的变量&nbsp;用SETS来设置........看来是我漏了什么部分?<br>因为我只能参考MOUSE的HOOK那些代码做参考&nbsp;&nbsp;不见有用倒映射什么的
 
//DLL部分<br>//声明<br>var<br>&nbsp;&nbsp;KeyBoardHook,hMappingFile:&nbsp;THandle;<br>&nbsp;&nbsp;FirstProcess:boolean;{是否是第一个进程}<br>&nbsp;&nbsp;pShMem:pShareMem;&nbsp;//pShareMem是自定义的结构体指针,用来存你需要的变量<br><br>//在初始化DLL的时候加入这些代码,建立共享内存<br>&nbsp;&nbsp;hMappingFile&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);<br>&nbsp;&nbsp;&nbsp;if&nbsp;hMappingFile=0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hMappingFile&nbsp;:=&nbsp;CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMem),MappingFileName);<br>&nbsp;&nbsp;&nbsp;if&nbsp;hMappingFile=0&nbsp;then&nbsp;Exception.Create('不能建立共享内存!');<br>&nbsp;&nbsp;&nbsp;pShMem&nbsp;:=&nbsp;&nbsp;MapViewOfFile(hMappingFile,FILE_MAP_WRITE&nbsp;or&nbsp;FILE_MAP_READ,0,0,0);<br>&nbsp;&nbsp;&nbsp;if&nbsp;pShMem&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMappingFile);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exception.Create('不能映射共享内存!');<br>&nbsp;&nbsp;&nbsp;end;
 
//EXE部分<br><br>private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;hMapObj&nbsp;:&nbsp;THandle;<br>&nbsp;&nbsp;&nbsp;&nbsp;pShMem&nbsp;:&nbsp;PShareMem;<br><br>procedure&nbsp;TForm1.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;hMapObj&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,{获取完全访问映射文件}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPCTSTR(MappingFileName));{映射文件名字}<br>&nbsp;&nbsp;if&nbsp;hMapObj&nbsp;=&nbsp;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('不能定位内存映射文件块!');<br>&nbsp;&nbsp;&nbsp;&nbsp;Halt;<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;pShMem&nbsp;:=&nbsp;MapViewOfFile(hMapObj,FILE_MAP_WRITE,0,0,0);<br>&nbsp;&nbsp;if&nbsp;pShMem&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('映射文件错误'+&nbsp;IntToStr(GetLastError));<br>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMapObj);<br>&nbsp;&nbsp;&nbsp;&nbsp;Halt;<br>&nbsp;&nbsp;end;<br>end;
 
//其中,mappingfilename是共享内存的名称<br>const&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;MappingFileName&nbsp;=&nbsp;'myFileName';<br><br>//自定义结构体<br>type<br>&nbsp;&nbsp;&nbsp;&nbsp;PPointer&nbsp;=&nbsp;^Pointer;<br>&nbsp;&nbsp;&nbsp;&nbsp;TShareMem&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//加入你用到的变量,比如<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;masin:integer;//回传变量<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;master_masin:integer;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;PShareMem&nbsp;=&nbsp;^TShareMem;
 
这样&nbsp;你就可以再EXE和DLL里共同使用一个变量了
 
我试试谢谢
 
可以了,我看别的例子&nbsp;直接对变量操作&nbsp;<br><br>//========================<br>library&nbsp;dll;<br>uses<br>&nbsp;&nbsp;ShareMem,<br>&nbsp;&nbsp;SysUtils,<br>&nbsp;&nbsp;Windows,<br>&nbsp;&nbsp;Classes,<br>&nbsp;&nbsp;IniFiles,<br>&nbsp;&nbsp;Messages;<br><br>{$R&nbsp;*.res}<br>&nbsp;const&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;MappingFileName&nbsp;=&nbsp;'myFileName';<br><br>//自定义结构体<br>type<br>&nbsp;&nbsp;&nbsp;&nbsp;PPointer&nbsp;=&nbsp;^Pointer;<br>&nbsp;&nbsp;&nbsp;&nbsp;TShareMem&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//加入你用到的变量,比如<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;masin:integer;//回传变量<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;master_masin:integer;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;PShareMem&nbsp;=&nbsp;^TShareMem;<br><br>var<br>&nbsp;&nbsp;&nbsp;CBTHandle:&nbsp;HHOOK;//hook<br>&nbsp;&nbsp;masin:integer;//回传变量<br>&nbsp;&nbsp;master_masin:integer;<br>&nbsp;&nbsp;hMappingFile:&nbsp;THandle;<br>&nbsp;&nbsp;FirstProcess:boolean;{是否是第一个进程}<br>&nbsp;&nbsp;pShMem:pShareMem;&nbsp;//pShareMem是自定义的结构体指针,用来存你需要的变量<br><br><br>&nbsp;&nbsp;//------------------<br>function&nbsp;sets(masters:integer):integer;stdcall;export;<br>begin<br>//master_masin:=masters;<br>//result:=1;<br>end;<br>&nbsp;function&nbsp;gets():integer;stdcall;export;<br>&nbsp;&nbsp;begin<br><br>&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;result:=masin;<br><br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//---------------------------&nbsp;CBT&nbsp;Hook&nbsp;test&nbsp;---------------------------<br>//&nbsp;CBT(computer-based&nbsp;training&nbsp;)全局钩子测试函数<br>function&nbsp;CBTProc(nCode:&nbsp;Integer;wParam:&nbsp;WPARAM;&nbsp;lParam:&nbsp;LPARAM):&nbsp;LRESULT;&nbsp;stdcall;<br>var&nbsp;now_masin:integer;<br>begin<br>&nbsp;&nbsp;if(nCode&nbsp;=&nbsp;HCBT_ACTIVATE)&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;now_masin:=wParam;<br>&nbsp;if(now_masin&lt;&gt;pShMem^.master_masin)&nbsp;then<br>&nbsp;begin<br>&nbsp;&nbsp;&nbsp;pShMem^.masin:=now_masin;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;CallNextHookEx(CBTHandle,nCode,wParam,lParam);<br>end;<br><br>//&nbsp;设置CBT全局钩子<br>function&nbsp;EnableCBTHook():&nbsp;Boolean;&nbsp;stdcall;&nbsp;export;<br>begin<br><br>&nbsp;&nbsp;cbtHandle&nbsp;:=&nbsp;SetWindowsHookEx(WH_cbt,@cbtProc,hInstance,0);<br>&nbsp;&nbsp;&nbsp;if(&nbsp;cbtHandle&nbsp;=&nbsp;0)&nbsp;then&nbsp;Result&nbsp;:=&nbsp;False<br>&nbsp;&nbsp;else&nbsp;Result&nbsp;:=&nbsp;True;<br>end;<br><br>//取消&nbsp;CBT全局钩子<br>function&nbsp;DisableCBTHook():&nbsp;Boolean;&nbsp;stdcall;&nbsp;export;<br>begin<br>&nbsp;&nbsp;&nbsp;if(&nbsp;cbtHandle&nbsp;&lt;&gt;&nbsp;0)&nbsp;then&nbsp;UnhookWindowsHookEx(cbtHandle);<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;True;<br>end;<br><br>exports<br>&nbsp;&nbsp;EnableCBTHook,DisableCBTHook&nbsp;,gets,sets;<br><br>begin<br>&nbsp;&nbsp;hMappingFile&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,False,MappingFileName);<br>&nbsp;&nbsp;&nbsp;if&nbsp;hMappingFile=0&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hMappingFile&nbsp;:=&nbsp;CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMem),MappingFileName);<br>&nbsp;&nbsp;&nbsp;if&nbsp;hMappingFile=0&nbsp;then&nbsp;Exception.Create('不能建立共享内存!');<br>&nbsp;&nbsp;&nbsp;pShMem&nbsp;:=&nbsp;&nbsp;MapViewOfFile(hMappingFile,FILE_MAP_WRITE&nbsp;or&nbsp;FILE_MAP_READ,0,0,0);<br>&nbsp;&nbsp;&nbsp;if&nbsp;pShMem&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMappingFile);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exception.Create('不能映射共享内存!');<br>&nbsp;&nbsp;&nbsp;end;&nbsp;&nbsp;<br><br>&nbsp;&nbsp;end.
 
但是有个小问题&nbsp;为什么关闭程序的时候老出错呢<br>跳几个错误窗口<br>可以帮忙看看么<br><br>EXE的部分<br><br>unit&nbsp;test;<br><br>interface<br><br>uses<br>&nbsp;sharemem,&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls;<br><br>const<br>&nbsp;&nbsp;&nbsp;&nbsp;MappingFileName:pchar=&nbsp;'myFileName';<br><br>//自定义结构体<br>type<br>&nbsp;&nbsp;&nbsp;&nbsp;PPointer&nbsp;=^Pointer;<br>&nbsp;&nbsp;&nbsp;&nbsp;TShareMem&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//加入你用到的变量,比如<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;masin:integer;//回传变量<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;master_masin:integer;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;PShareMem&nbsp;=^TShareMem;<br>&nbsp;type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button2:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo1:&nbsp;TMemo;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button2Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormCreate(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hMapObj&nbsp;:&nbsp;THandle;<br>&nbsp;&nbsp;&nbsp;&nbsp;pShMem&nbsp;:&nbsp;PShareMem;<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br>function&nbsp;EnableCBTHook:&nbsp;Boolean;&nbsp;stdcall;&nbsp;external&nbsp;'dll.dll';<br><br>implementation<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>var&nbsp;x:integer;<br>begin<br>x:=self.Handle;<br>pShMem^.master_masin:=x;<br>EnableCBTHook;<br>end;<br>procedure&nbsp;TForm1.Button2Click(Sender:&nbsp;TObject);<br>begin<br>memo1.Text:=&nbsp;inttostr(pShMem^.masin)+'&nbsp;&nbsp;&nbsp;&nbsp;'+&nbsp;inttostr(self.Handle);<br>end;<br><br>procedure&nbsp;TForm1.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;hMapObj&nbsp;:=&nbsp;OpenFileMapping(FILE_MAP_WRITE,{获取完全访问映射文件}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPCTSTR(MappingFileName));{映射文件名字}<br>&nbsp;&nbsp;if&nbsp;hMapObj&nbsp;=&nbsp;0&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('不能定位内存映射文件块!');<br>&nbsp;&nbsp;&nbsp;&nbsp;Halt;<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;pShMem&nbsp;:=&nbsp;MapViewOfFile(hMapObj,FILE_MAP_WRITE,0,0,0);<br>&nbsp;&nbsp;if&nbsp;pShMem&nbsp;=&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('映射文件错误'+&nbsp;IntToStr(GetLastError));<br>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMapObj);<br>&nbsp;&nbsp;&nbsp;&nbsp;Halt;<br>&nbsp;&nbsp;end;<br>end;<br>end.
 
好象老是引起IE出错的?
 
其中有个错误窗口试显示&nbsp;RUNTIME&nbsp;ERROR&nbsp;217&nbsp;AT&nbsp;00413C7C
 
忘了给取消映射的代码了&nbsp;&nbsp;不好意思<br>在窗体退出的时候,或者在DLL&nbsp;&nbsp;finalization部分&nbsp;&nbsp;加入<br>&nbsp;&nbsp;&nbsp;UnMapViewOfFile(pShMem);&nbsp;{取消映射视图}<br>&nbsp;&nbsp;&nbsp;CloseHandle(hMappingFile);&nbsp;{关闭映射文件句柄}
 
EXE中的是不是不对呢&nbsp;&nbsp;CloseHandle(hMapObj);<br><br>我直接改成&nbsp;&nbsp;pShMem&nbsp;:=&nbsp;MapViewOfFile(hMapObj,FILE_MAP_WRITE,0,0,0);<br>&nbsp;&nbsp;if&nbsp;pShMem&nbsp;&lt;&gt;&nbsp;nil&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('映射文件错误'+&nbsp;IntToStr(GetLastError));<br>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hMapObj);<br>&nbsp;&nbsp;&nbsp;&nbsp;Halt;<br>&nbsp;&nbsp;end;<br>end;&nbsp;&nbsp;<br>&nbsp;一样出错的
 
后退
顶部