unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs,
StdCtrls;
const
WM_DATA=WM_USER+; type
PShareMem=^TShareMem;
TShareMem=record
Data:array[..] of char;
end;
TForm1 = class(TForm)
Button1: TButton;
edt1: TEdit;
//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=;
request_timeout=; procedure openMap;
begin
hmapping :=createFileMapping($FFFFFFFF,nil,PAGE_READWRITE,,SizeOf(TShareMem),
pchar('map pei'));
if hmapping= then
begin
showMessage('创建内存映像文件失败');
Application.Terminate;
end;
//将影像文件映射到进程的地址空间
pshare := PShareMem(mapviewoffile(hmapping,FILE_MAP_ALL_ACCESS,,,));
if pshare=nil then
begin
closehandle(hmapping);
showmessage('显示内存映像文件失败');
application.Terminate;
exit;
end;
end; procedure closeMap; //关闭共享内存映像
begin
if pshare<>nil then
unmapviewoffile(pshare); // 从进程地址空间中释放映像文件
if hmapping<> then
closehandle(hmapping); end; function LockMap:boolean;
begin
result:=true;
hmapmutex:=createMutex(nil,false,pchar('mutex peidw'));
if hmapmutex= 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('简单的内存共享例子');
str:= PChar(edt1.Text);
copyMemory(@(pshare^.Data),str,length(str));
postMessage(findWindow(nil,'Form2'),WM_DATA,,); end; procedure TForm1.FormCreate(Sender: TObject);
begin
openMap;
LockMap;
end; procedure TForm1.FormDestroy(Sender: TObject);
begin
unLockMap;
closeMap;
end; end. unit Unit2; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
const
WM_DATA=WM_USER+;
type
PShareMem=^TShareMem;
TShareMem=record
Data:array[..] of char;
end; TForm2 = class(TForm)
//Memo1: TMemo;
Button1: TButton;
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= then
begin
showMessage('定位内存映像文件块失败');
halt; //异常终止
end;
//将影像文件映射到进程的地址空间
pshare := PShareMem(mapviewoffile(hmapping,FILE_MAP_ALL_ACCESS,,,));
if pshare=nil then
begin
closehandle(hmapping);
showmessage('将映像映射到进程地址空间失败');
application.Terminate;
exit;
end;
fillchar(pshare^,sizeof(TShareMem),);//初始化地址空间
end; procedure TForm2.getShareInfo(var msg: TMessage);
begin
if msg.LParam= then
memo1.Lines.add(pshare^.Data);
end;
end.

两个exe共享内存数据的更多相关文章

  1. delphi 实现两个exe文件共享内存映像的代码

    创建内存映像的程序 ------------------------------------------------------------------------------------------ ...

  2. dll hook 共享内存数据

    unit UnitDll; interface uses Windows; * ; // 文件映射到内存的大小 const HOOK_MEM_FILENAME = 'MEM_FILE'; // 映像文 ...

  3. C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 VC中进程与进程之间共享内存 .net环境下跨进程、高频率读写数据 使用C#开发Android应用之WebApp 分布式事务之消息补偿解决方案

    C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing). ...

  4. C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转

    原文:C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing ...

  5. C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped

    节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing). 内存映射文件对于托管世界的开发人员来说似乎很陌生,但它确实已经是很远古的技术了,而且在操作 ...

  6. C# 进程间共享内存通信方式

    从别处看到一篇文章做进程间通信很好使,唯一的问题是,需要注意using的用法,Using有个用法3, using 语句允许程序员指定使用资源的对象应当何时释放资源.using 语句中使用的对象必须实现 ...

  7. Windows共享内存示例

    共享内存主要是通过映射机制实现的. Windows 下进程的地址空间在逻辑上是相互隔离的,但在物理上却是重叠的.所谓的重叠是指同一块内存区域可能被多个进程同时使用.当调用 CreateFileMapp ...

  8. 共享内存+互斥量实现linux进程间通信 分类: Linux C/C++ 2015-03-26 17:14 67人阅读 评论(0) 收藏

    一.共享内存简介 共享内存是进程间通信中高效方便的方式之一.共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针,两个进程可以对一块共享 ...

  9. C# 进程间通信(共享内存)

    原文:C# 进程间通信(共享内存) 进程间通信的方式有很多,常用的方式有: 1.共享内存(内存映射文件,共享内存DLL). 2.命名管道和匿名管道. 3.发送消息 本文是记录共享内存的方式进行进程间通 ...

随机推荐

  1. 1、MYSQL 数据库的安装与配置

    安装 1.打开官网https://www.mysql.com,选择社区版本   2.如图点击下在安装(本人在下载过程中亲身感觉下载时间非常漫长,需要等待,不知道为啥会有限速,可以参考网上教程用迅雷进行 ...

  2. centos7下安装ansible

    由于centos7预装了python,因此我们可以跳过python的安装环节(记得关闭防火墙) [root@model ~]# [root@model ~]# python --version Pyt ...

  3. 关于安装openfiler

    简介 Openfiler 由rPath Linux驱动,它是一个基于浏览器的免费网络存储管理实用程序,可以在单一框架中提供基于文件的网络连接存储 (NAS) 和基于块的存储区域网 (SAN).Open ...

  4. FreeCAD stp文件基于python脚本操作

    FreeCAD对于3D模型处理这块的东西封装的还是很完善的,所以移植这块的东西还是有必要的! 首先下载FreeCAD编译好的库: https://www.freecadweb.org/wiki/Dow ...

  5. Percona-Toolkit 之 pt-archiver 删除历史数据

    pt-archiver是Percona-Toolkit工具集中的一个组件,是一个主要用于对MySQL表数据进行归档和清除工具.它可以将数据归档到另一张表或者是一个文件中.pt-archiver在清除表 ...

  6. Oracle SQL触发器

    一.触发器 触发器是一个数据库对象,是一个特殊的过程,当特定的时间发生时隐式地执行.比如在一个表中发生插入.更新或删除的时间,或者 CREATE.ALTER 这样的数据定义语句执行时,触发器会隐式执行 ...

  7. 六十四、SAP中的内表的9种定义方式

    一.内表一共有9种定义方式,如下: 二.执行如下 *&--------------------------------------------------------------------- ...

  8. TRUNC()函数——oracle

    使用trunc()函数获取不同的日期: select trunc(sysdate) from dual; --今天的日期 select trunc(sysdate,'dd') from dual; - ...

  9. 线程与进程 queue模块

    queue模块的基本用法 https://www.cnblogs.com/chengd/articles/7778506.html 模块实现了3种类型的队列,区别在于队列中条目检索的顺序不同.在FIF ...

  10. MySQL8.0安装caching_sha2_password问题

    MySQL安装之后无法用工具连接上本地数据库 详情原因可见: https://mysqlserverteam.com/mysql-8-0-4-new-default-authentication-pl ...