进程B返回错误代码应该是表明指定的文件句柄已经存在, 这是正常返回<br><br><br><br>Windows还提供了File_Mapping内存共享技术用于实现程序间数据共享。使用与上例窗口相似的两个程序来说明 <br>在MapWrite程序中: <br>Private <br>HMapFile: Thanale; <br>MapFilePointer: Pointer; <br>procedure TForm1.FormCreate(Sender: TObject); <br>begin <br>//使用API函数来建立映象文件 <br>hMapFile:=CreateFileMapping( <br>$FFFFFFFF, //指定共享内存 <br>nil, <br>Page_ReadWrite, //共享方式 <br>0, <br>1000, //共享内存大小 <br>′MyMappedFile′); //映象文件的名字 <br>if hMapFile<>0 then //如果映象文件建立成功 <br>//MapViewOfFile函数返回一个指向共享内存块的在该程序内存空间中有效的指针 <br>MapFilePointer:=MapView OfFile(hMapFile,File-Map-All-Access,0,0,0) <br>else <br>ShowMessage(′Can′'t Create MapFile′); <br>if MapFilePointer=nil then <br>ShowMessage(′MapFilePointer=nil′); <br>end; <br>procedure TForm1.Edit1Change(Sender: TObject); <br>begin <br>StrCopy(PChar(MapFilePointer),PChar(Edit1.Text));//将文本框中的字符串copy到共享内存中 <br>end; <br>在MapRead程序中使用API函数OpenFileMapping打开MapWrite程序中已建立的映象文件 <br>procedure TForm1.FormCreate(Sender: TObject); <br>begin <br>hMapFile:=OpenFileMapping(FILE_MAP_READ,true,′MyMappedFile′); //取得′MyMappedFile′映象文件的句柄 <br>if hMapFile<>0 then <br>MapFilePointer:=MapViewOfFile(hMapFile,File_Map_All_Access,0,0,0) <br>else <br>begin <br>ShowMessage(′Can not open Mapp File′); <br>Timer1.Enabled:=false; <br>end; <br>end; <br>//定时从共享内存中读取数据并显示出来 <br>procedure TForm1.Timer1Timer(Sender: TObject); <br>var <br>ss: String; <br>begin <br>ss:=PChar(MapFilePointer); <br>Edit1.Text:=ss; <br>end; <br>当然最后我们要释放共享内存句柄 <br>UnMapViewOfFile(MapFilePointer); <br>CloseHandle(hMapFile);