两个exe共享内存数据
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共享内存数据的更多相关文章
- delphi 实现两个exe文件共享内存映像的代码
创建内存映像的程序 ------------------------------------------------------------------------------------------ ...
- dll hook 共享内存数据
unit UnitDll; interface uses Windows; * ; // 文件映射到内存的大小 const HOOK_MEM_FILENAME = 'MEM_FILE'; // 映像文 ...
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 VC中进程与进程之间共享内存 .net环境下跨进程、高频率读写数据 使用C#开发Android应用之WebApp 分布式事务之消息补偿解决方案
C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing). ...
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转
原文:C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped 转 节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing ...
- C# .Net 多进程同步 通信 共享内存 内存映射文件 Memory Mapped
节点通信存在两种模型:共享内存(Shared memory)和消息传递(Messages passing). 内存映射文件对于托管世界的开发人员来说似乎很陌生,但它确实已经是很远古的技术了,而且在操作 ...
- C# 进程间共享内存通信方式
从别处看到一篇文章做进程间通信很好使,唯一的问题是,需要注意using的用法,Using有个用法3, using 语句允许程序员指定使用资源的对象应当何时释放资源.using 语句中使用的对象必须实现 ...
- Windows共享内存示例
共享内存主要是通过映射机制实现的. Windows 下进程的地址空间在逻辑上是相互隔离的,但在物理上却是重叠的.所谓的重叠是指同一块内存区域可能被多个进程同时使用.当调用 CreateFileMapp ...
- 共享内存+互斥量实现linux进程间通信 分类: Linux C/C++ 2015-03-26 17:14 67人阅读 评论(0) 收藏
一.共享内存简介 共享内存是进程间通信中高效方便的方式之一.共享内存允许两个或更多进程访问同一块内存,就如同 malloc() 函数向不同进程返回了指向同一个物理内存区域的指针,两个进程可以对一块共享 ...
- C# 进程间通信(共享内存)
原文:C# 进程间通信(共享内存) 进程间通信的方式有很多,常用的方式有: 1.共享内存(内存映射文件,共享内存DLL). 2.命名管道和匿名管道. 3.发送消息 本文是记录共享内存的方式进行进程间通 ...
随机推荐
- DOM:通过文本框向下拉列表中添加内容
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...
- Unity Reflection Probe使用入门
贴官方API的说法: 反射探头: 一个反射探头很像一个相机,捕获了周围所有方向的球形视图.然后将捕获的图像存储为Cubemap,可由具有反射材料的对象使用.在给定场景中可以使用多个反射探测器,可以将对 ...
- 中兴将用“加减乘除”建立理想 5G 网络
6 月 28 日,MWC 2019 上海展期间,中兴通讯执行董事.总裁徐子阳发表演讲表示,面对 5G 建网大势,要看破大势,不破不立.为此中兴将用“加减乘除”建立理想 5G 网络. 何为“加减乘除 ...
- centos-6更新yum源(163)
更新前请先备份原来的repo文件,养成好习惯 cd /etc/yum.repos.d/ mv CentOS-Base.repo CentOS-Base.repo.bak 到http://mirrors ...
- bzoj 1369: [Baltic2003]Gem
确实是神2333333333,一开始以为是01染色sb题,然而被打脸... (蒟蒻不乱说,网上各种神犇的题解,还有图!!) #include <bits/stdc++.h> #define ...
- 升级安装go1.13.5
运行文件时报错 verifying github.com/mattn/go-isatty@v0.0.10-0.20190818123653-bf9a1dea1961/go.mod: github.co ...
- 四十四、在SAP中冻结第一行表头
一.表格数据量大了,如果需要界面滚动,则看不到第一行的表头文本 二.代码如下: 二.效果如下,任意滚动,表头还是被冻结可以看到
- ES6 之 对象属性的可枚举性和遍历
1.Object.getOwnPropertyDescriptor() 解释:获取对对象属性的描述对象. let obj = { foo: 123 }; console.log(Object.getO ...
- oracle11g数据库的安装
先在 Oracle官网上下载11g oracle Database 11g 第 2 版 (11.2.0.1.0) 标准版.标准版 1 以及企业版,适用于 Microsoft Windows (x6 ...
- Java compare方法和compareTo方法
Java Comparator接口排序用法,详细介绍可以阅读这个链接的内容:https://www.cnblogs.com/shizhijie/p/7657049.html 对于 public int ...