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. Word 写论文的一些教训和经验

    参考文献 写正文时就在引用位置添加参考文献的title,写完后,在百度学术或谷歌学术中搜索参考文献获取GB/T 7714的参考格式,在参考文献的章节为文献编号,在引用位置插入交叉引用. 插图 可以先在 ...

  2. 打开exe并传参

    shellexecute(Application.Handle,'open',PWideChar('E:\控件\TMS.Scripter.Studio.Pro..6.0.2.0.Delphi.BCB. ...

  3. Spark的任务提交和执行流程概述

    1.概述 为了更好地理解调度,我们先看一下集群模式的Spark程序运行架构图,如上所示: 2.Spark中的基本概念 1.Application:表示你的程序 2.Driver:表示main函数,创建 ...

  4. DevOps - 实施原则

    章节 DevOps – 为什么 DevOps – 与传统方式区别 DevOps – 优势 DevOps – 不适用 DevOps – 生命周期 DevOps – 与敏捷方法区别 DevOps – 实施 ...

  5. Docker常用命令,Docker安装Nginx、Redis、Jenkins、tomcat、MySQL

    常用命令 拉取镜像:docker pull xxx启动镜像:docker run --name xxx 8080:8080 -d xxx查看容器:docker ps xxx 停止容器:docker s ...

  6. Java容器--Queue

    ConcurrentLinkedQueue 参考 https://www.cnblogs.com/leesf456/p/5539142.html ConcurerntLinkedQueue一个基于链接 ...

  7. Python基础笔记:使用dict和set

    dict 就和 C语言中的 map 的作用一样.查找非常快,以空间换时间! dict的使用: >>> d={'Mike':66,'Bob':77,'John':88} #定义一个di ...

  8. python转换ascii码

    字符转数字 ord("A") 数字转字符 chr(65)

  9. Spring入门之二-------SpringIoC之实例化Bean以及注入Bean

    一.实例化Bean 1. 通过默认构造方法实创建Bean public class Bean1 { public Bean1() { System.out.println(this.getClass( ...

  10. SpringBoot的Banner横幅

    SpringBoot的Banner横幅即在SpringBoot应用程序启动过程中,日志输出的如下内容: 如果想替换此部分内容的话,可以在classpath根路径下建立一个文件,命名为:banner.t ...