zoukankan      html  css  js  c++  java
  • 利用内存映射文件在两个进程间共享数据 转

    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;

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

  • 相关阅读:
    SpringCloud 学习之概述
    定位慢查询
    中止线程
    笨办法41学会说面向对象【pyinstaller安装使用
    pip安装
    笨办法40模块, 类和对象class
    笨办法39字典dict
    笨办法38列表操作
    笨办法35分支和函数
    笨办法34访问列表元素(列表方法)
  • 原文地址:https://www.cnblogs.com/FuYan/p/3811788.html
Copyright © 2011-2022 走看看