1. private
        hMapFile: THandle;
        MapFilePointer: Pointer;
      public
        { Public declarations }
      end;
    var
      Form1: TForm1;
    implementation
    {$R *.DFM}
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      hMapFile := CreateFileMapping (
        $FFFFFFFF, // 特殊内存映射句柄
        nil, page_ReadWrite, 0,10000,
        'DdhDemoMappedFile'); // 文件名
      if hMapFile <> 0 then
        MapFilePointer := MapViewOfFile (
          hMapFile, // 上面映象文件的句柄
          File_Map_All_Access,
          0, 0, 0) // 访问整个映象文件
      else
        ShowMessage ('hMapFile = 0');
      if MapFilePointer = nil then
        ShowMessage ('MapFilePointer = nil');
    end;
    procedure TForm1.BtnWriteClick(Sender: TObject);
    begin
      StrCopy (PChar (MapFilePointer),
        PChar (EditWrite.Text));//把内容写入共享内存
    end;
    procedure TForm1.BtnReadClick(Sender: TObject);
    var
      S: string;
    begin
      S := PChar (MapFilePointer);//从共享内存读出内容
      EditRead.Text := S;
    end;

     

用这种方法,不但可以在不同的程序之间共享数据,还可以
在同一程序的不同实例间共享数据。为了及时通知其它进程
共享数据的变化,可以自定义一条用户消息,通过发消息来
实现。

posted on 2014-06-27 13:18  倾天  阅读(364)  评论(0编辑  收藏  举报