L
llzx
Unregistered / Unconfirmed
GUEST, unregistred user!
用DLL定义一个全局句柄,实现Form间的动态数据传输<br>为此我编了几个Demo,但是不能实现该功能,且Form关闭时,内存不能正确释放<br>望各位大侠指教<br>1.DLL<br>library shareDll;<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,sharemem,<br> StdCtrls, ExtCtrls;<br>var<br> hMem: THandle;<br> num : integer;<br><br>function GetGlobalMem: THandle;<br>begin<br> Result := hMem;<br>end;<br><br>exports<br>GetGlobalMem Index 1 resident;<br><br>begin<br> hMem := GlobalAlloc(gmem_MOVEABLE and gmem_DDEShare,num);<br> if hMem = 0 then<br> MessageDlg('Could not allocate memory',mtWarning,[mbOK],0);<br>end.<br><br>2.Server端<br>implementation<br><br>function GetGlobalMem:THandle;far;external 'shareDll.dll';<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>hMem: THandle;<br>pMem: PChar;<br>S : string;<br>begin<br> hMem := GetGlobalMem; <br> if hMem <> 0 then<br> begin<br> pMem := GlobalLock(hMem); <br> if pMem <> nil then<br> begin<br> StrPCopy(pMem,Memo1.text); <br> GlobalUnlock(hMem); <br> end<br> else<br> MessageDlg('Couldnot Lock memory block',mtWarning,[mbOK],0);<br> end;<br> s:=strpas(pmem);<br>// showmessage(s);<br><br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> close;<br>end;<br><br>3.client端<br>implementation<br>function GetGlobalMem:THandle;far;external 'shareDll.dll';<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> hMem: THandle;<br> pMem: PChar;<br>begin<br> hMem := GetGlobalMem; <br> if hMem <> 0 then<br> begin<br> pMem := GlobalLock(hMem);<br> if pMem <> nil then<br> begin<br> Memo1.text := StrPas(pMem); <br> GlobalUnlock(hMem); <br> end<br> else<br> MessageDlg('Couldnot Lock memory block',mtWarning,[mbOK],0);<br> end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br> hMem: THandle;<br> pMem: PChar;<br>begin<br> hMem := GetGlobalMem; <br> Freelibrary(hMem);<br> close;<br>end;<br><br> 问题:1.程序运行无任何错误,且Server,client都可以得到全局Handle<br>但是Client的Memo 不能得到正确数值<br>2。当client Formclose 时,总有Error 产生,不知道是什么<br><br>因为分不够多了,请斑竹及大侠多多包涵