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;

     

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

相关文章:

  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2021-06-19
  • 2021-06-22
猜你喜欢
  • 2022-12-23
  • 2021-12-27
  • 2021-09-10
  • 2022-12-23
  • 2022-01-17
  • 2022-02-12
相关资源
相似解决方案