两个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.发送消息 本文是记录共享内存的方式进行进程间通 ...
随机推荐
- css的响应式布局和动画
把响应式布局和动画放在一起写是因为他们有个共同点@符号 先讲讲响应式布局@media 响应式布局==曾经==非常的流行,这种布局方式可以做出一也兼容一切设备的页面,但是当页面的功能越来越多,css文件 ...
- 图片上传--base64
<?php defined('BASEPATH') OR exit('No direct script access allowed'); include_once (APPPATH . &qu ...
- 单调栈应用--将一个数删除n各数字之后的最大\最小值
E. Playing with numbers time limit per test 2.0 s memory limit per test 64 MB input standard input o ...
- error LNK2019: 无法解析的外部符号……
在VS中开发程序的时候遇到一个问题,应该算是比较常见,所以记录下. 在编译程序的时候遇到一个错误,大致提示如下: "error LNK2019: 无法解析的外部符号--" 遇到这个 ...
- python 输出99乘法表
for i in range(1,10): for j in range(1,i+1): print("%s*%s=%2s"%(i,j,i*j),end=" " ...
- 五十、在SAP程序中应用其他单元,INCLUDE的用法
一.在SAP程序中写入以下代码 二.双击引用的单元,会弹出以下窗口 三.点击是 四.点击保存 五.保存在本地 六.此文件被包含进来 七.我们把在GET_DATA和SHOW_DATA写到INCLUDE里 ...
- 058-PHP中goto语句的使用
<?php for($i=1;$i<=5;$i++){ //使用for循环循环输出1~5 if($i==3) goto ECH; //当$i为3时候跳出for循环 echo "$ ...
- Essay写作常见错误精选
Essay写作常见错误精选.Essay写作有许多不为人注意的小细节,如果申请人在这些细节上不注意,往往会犯一些很典型的错误.和小编一起来看看留学Essay写作常见错误解析. 1)直接把申请学校A的Es ...
- 腾讯云服务器上搭建Jenkins配置邮箱通知
1,Jenkins 点击 系统管理 2,点击系统管理 3,配置系统管理员邮件地址 5,配置 Extended E-main Notification,(用户名不需要邮箱后缀“@163.com”, SS ...
- 代理模式(Proxy Pattern)C#版本的
引用地址 https://www.cnblogs.com/zhili/p/ProxyPattern.html --------------------------------------------- ...