如何利用内存映射机制在两个进程中传递图像(30分)

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

barryani

Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序中利用映射系统的页面文件的方法实行在两个进程中<br>通信的时候如果只传递文字,数字的时候就成功,但如果传递<br>TBITMAP就发生错误,大概代码如下:<br>library ShareLib;<br>uses<br>&nbsp; ShareMem,Windows,SysUtils,Graphics,Types,Dialogs,<br>&nbsp; Classes;<br>const<br>&nbsp; MapName: Pchar ='ShareFile';<br><br>Type<br>&nbsp; PGlobalData = ^TGlobalData;<br>&nbsp; TGlobalData = record<br>&nbsp; Information:String[50];<br>&nbsp; Number:Integer;<br>&nbsp; Mybitmap:TBitmap;<br>&nbsp; end;<br>var<br>&nbsp; GlobalData:PGlobalData;<br>&nbsp; MapHandle:THandle;<br>procedure &nbsp;GetGlobalData(var MyGlobalData:PGlobalData);stdcall;<br>begin<br>&nbsp; &nbsp; MyGlobalData:=GlobalData;<br>end;<br><br>procedure &nbsp;OpenShareData;<br>var<br>&nbsp; size:Integer;<br>begin<br>&nbsp; size:=sizeof(TGlobalData);<br>&nbsp; MapHandle:=CreateFileMapping(DWORD(-1),NIL,PAGE_READWRITE,0,size,MapName);<br>&nbsp; if MapHandle=0 then<br>&nbsp; RaiseLastWin32Error;<br>&nbsp; GlobalData:=MapViewOfFile(MapHandle,FILE_MAP_ALL_ACCESS,0,0,size);<br>&nbsp; GlobalData^.Information:='This is a Test';<br>&nbsp; GlobalData^.Number:=1;<br>&nbsp; GlobalData^.Mybitmap.LoadFromFile('f:/chip.bmp');<br>&nbsp; if GlobalData=nil then<br>&nbsp; begin<br>&nbsp; &nbsp; CloseHandle(MapHandle);<br>&nbsp; &nbsp; RaiseLastWin32Error;<br>&nbsp; end;<br>end;<br><br>procedure &nbsp;CloseShareData;<br>begin<br>&nbsp; UnmapViewOfFile(GlobalData);<br>&nbsp; CloseHandle(MapHandle);<br>end;<br><br>procedure DllEntryPoint(dwReason:DWORD);<br>begin<br>&nbsp; case dwReason of<br>&nbsp; &nbsp; DLL_PROCESS_ATTACH:OpenShareData;<br>&nbsp; &nbsp; DLL_PROCESS_DETACH:CloseShareData;<br>&nbsp; &nbsp; DLL_THREAD_ATTACH:;<br>&nbsp; &nbsp; DLL_THREAD_DETACH:;<br>&nbsp; end;<br>end;<br><br>exports<br>&nbsp; GetGlobalData;<br>{$R *.res}<br><br>begin<br>&nbsp; DllProc:=@DllEntryPoint;<br>&nbsp; DllEntryPoint(DLL_PROCESS_ATTACH);<br>end.<br>请帮帮小弟,谢谢!!
 
让我考虑一下
 
TBitmap 好像实际上是一个指针,两个进程确实共享了这个指针,但是另一个进程中这个<br>指针指向的地址并不是 TBitmap。你可以自己分配一块内存,然后写入所有点的颜色。然<br>后共享这块内存。
 
To mikedeakins<br>写入所有点的颜色速度会不会有问题呢?
 
我试过了,加一个GlobalData^.Mybitmap:=Tbitmap.create就可以了
 
但另一进程用Image1.Picture.Bitmap.Assign(GlobalData^.Mybitmap);就发生了错误<br>怎么办?
 
不要用assign,用:=就可以<br>image1.Picture.Bitmap:=GlobalData^..Mybitmap;<br>
 
后退
顶部