怎样用globalAlloc实现程序间数据共享? (50分)

  • 主题发起人 主题发起人 savenight
  • 开始时间 开始时间
S

savenight

Unregistered / Unconfirmed
GUEST, unregistred user!
如题目。[:(]<br>觉得很简单,但...<br>1.做一个DLL,<br>
代码:
library pDataDll;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; windows,<br>&nbsp; dialogs;<br>type<br>&nbsp;data=array[0..38] of integer;<br>var<br>&nbsp;hMem:thandle;<br>&nbsp;Pdata:^data;<br>function GetGlobalMem:Thandle;export;<br>begin<br>&nbsp;result:=hMem;<br>end;<br><br>{$R *.res}<br>exports<br>&nbsp;GetglobalMem;<br>&nbsp; <br>begin<br>hMem:=GlobalAlloc(gmem_MOVEABLE and gmem_DDEShare,sizeof(data));<br>if hMem=0 then<br>messagedlg('could not allocate memory',mtwarning,[mbok],0);<br>end.
<br>2.新建一个工程1,然后调用DLL的GetGlobalMem向工程1里写入数据。<br>3再新建一个工程2,从这一个工程2中读出数据。[red]但结果却是为乱码[/red]。
 
看不太懂你写的<br>换个写法:<br>Library Project2tr;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; windows,<br>&nbsp; Dialogs;<br><br>{$R *.RES}<br><br>var<br>hMem: THandle;<br>function GetGlobalMem(num:integer): THandle; stdcall;<br>begin<br>&nbsp; &nbsp;hMem := GlobalAlloc(gmem_MOVEABLE and gmem_DDEShare,num);<br>&nbsp; &nbsp;if hMem = 0 then<br>&nbsp; &nbsp;MessageDlg('Could not allocate memory',mtWarning,[mbOK],0);<br>&nbsp; &nbsp;Result := hMem;<br>end;<br><br>exports<br>&nbsp;GetGlobalMem;<br>begin<br>end.<br>
 
没有听说可以这样做,还是我记错了,<br>DLL中的数据在各个进程中不是共享的(在32位Windows中),<br>因此这种写法好像没法达到你想要的结果。<br>
 
接受答案了.
 
可以用shareMem在可执行程序和dll之间共享长字符串。
 
后退
顶部