博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
delphi 实现两个exe文件共享内存映像的代码
阅读量:3585 次
发布时间:2019-05-20

本文共 3442 字,大约阅读时间需要 11 分钟。

创建内存映像的程序

--------------------------------------------------------------------------------------------

unit Unit1;

interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
const
  WM_DATA=WM_USER+1025;
type
  PShareMem=^TShareMem;
  TShareMem=record
    Data:array[0..255] of char;
  end;
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  pshare: PShareMem;
implementation
{$R *.dfm}
var
  hmapping:THandle;
  hmapmutex:THandle;
const
  mapfilesize=1000;
  request_timeout=1000;
procedure openMap;
begin
  hmapping :=createFileMapping($FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(TShareMem),
  pchar('map pei'));
  if hmapping=0 then
  begin
    showMessage('创建内存映像文件失败');
    Application.Terminate;
  end;
  //将影像文件映射到进程的地址空间
  pshare := PShareMem(mapviewoffile(hmapping,FILE_MAP_ALL_ACCESS,0,0,0));
  if pshare=nil then
  begin
    closehandle(hmapping);
    showmessage('显示内存映像文件失败');
    application.Terminate;
    exit;
  end;
end;
procedure closeMap;    //关闭共享内存映像
begin
  if pshare<>nil then
    unmapviewoffile(pshare);      // 从进程地址空间中释放映像文件
  if hmapping<>0 then
     closehandle(hmapping);
end;
function LockMap:boolean;
begin
  result:=true;
  hmapmutex:=createMutex(nil,false,pchar('mutex peidw'));
  if hmapmutex=0 then
  begin
    showmessage('创建互斥对象失败');
    result:=false;
  end
  else
  begin
     if waitforsingleObject(hmapmutex,request_timeout)=WAIT_FAILED then
     begin
        showmessage('对互斥对象加锁失败');
        result:=false;
     end;
  end;   //if end
end;
procedure unLockMap;//释放互斥对象
begin
  releaseMutex(hmapmutex);
  closeHandle(hmapmutex);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
  str:pchar;
begin
  str:=pchar('简单的内存共享例子');
  copyMemory(@(pshare^.Data),str,length(str));
  postMessage(findWindow(nil,'MyForm'),WM_DATA,1,1);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
  openMap;
  LockMap;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
    unLockMap;
    closeMap;
end;

end.

读取内存映像程序

-----------------------------------------------------------------------------

unit Unit2;

interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
const
  WM_DATA=WM_USER+1025;
type
  PShareMem=^TShareMem;
  TShareMem=record
    Data:array[0..255] of char;
  end;
  TForm2 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure getShareInfo(var msg:TMessage); message WM_DATA;
  end;
var
  Form2: TForm2;
  pshare: PShareMem;
  hmapping:THandle;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
  closehandle(hmapping);
  close;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
  hmapping :=openFileMapping(File_MAP_WRITE,false,pchar('map pei'));
  if hmapping=0 then
  begin
    showMessage('定位内存映像文件块失败');
    halt; //异常终止
  end;
 //将影像文件映射到进程的地址空间
  pshare := PShareMem(mapviewoffile(hmapping,FILE_MAP_ALL_ACCESS,0,0,0));
  if pshare=nil then
  begin
    closehandle(hmapping);
    showmessage('将映像映射到进程地址空间失败');
    application.Terminate;
    exit;
  end;
  fillchar(pshare^,sizeof(TShareMem),0);//初始化地址空间
end;
procedure  TForm2.getShareInfo(var msg: TMessage);
begin
  if msg.LParam=1 then
    memo1.text:=pshare^.Data;
end;
end.

转载地址:http://jmvwn.baihongyu.com/

你可能感兴趣的文章
Spring组件注册、Bean生命周期、自动装配相关知识
查看>>
Spring基于注解的IOC初始化过程(较长慎入)
查看>>
《Arduino》开发 之 基于 u8g2 库 的 OLED 菜单界面
查看>>
python学习之旅
查看>>
python-00-小白,你该怎样学编程?
查看>>
py-mongdb语法
查看>>
计算机网络 | 无盘工作站的建立
查看>>
Redis基础知识
查看>>
面向对象设计的五大基本原则
查看>>
以项目为导向,助您入门python之搭建开发环境anaconda+pycharm
查看>>
以项目为导向,助您入门python之网络爬虫-爬取京东商品plus价格低于原价5折的商品(一)
查看>>
以项目为导向,助您入门python之网络爬虫-爬取京东商品plus价格低于原价5折的商品(二)
查看>>
mysql安装和配置ODBC驱动,然后tableau链接MySQL数据库
查看>>
以项目为导向,助您入门python之网络爬虫-爬取京东商品plus价格低于原价5折的商品(三)
查看>>
物联网之智能灯开发-前言
查看>>
物联网之智能灯-Django(一)
查看>>
使用计算机视觉技术进行工业品质检测
查看>>
Python小白入门分享
查看>>
突破编程瓶颈《数据结构与算法》初识
查看>>
SystemError: execution of module h5py.utils raised unreported exception
查看>>