delphi 实现两个exe文件共享内存映像的代码
创建内存映像的程序
--------------------------------------------------------------------------------------------
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.
delphi 实现两个exe文件共享内存映像的代码的更多相关文章
- 两个exe共享内存数据
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Java进程通信之映像文件共享内存
Java进程通信之映像文件共享内存 1. 共享内存 vs 进程通信 对UNIX系统来说,共享内存分为一般共享内存和映像文件共享内存两种.但windows实际上只有影像文件共享内存一种. 而说到进程通信 ...
- 【VS开发】文件共享内存2
在32位的Windows系统中,每一个进程都有权访问他自己的4GB(232=4294967296)平面地址空间,没有段,没有选择符,没有near和far指针,没有near和far函数调用,也没有内存模 ...
- 使用PageHeap.EXE或GFlags.EXE检查内存越界错误 (转)
2011-05-27 20:19 290人阅读 评论(0) 收藏 举报 microsoftdebuggingstructureoutputimagefile 必先利其器之一:使用PageHeap.EX ...
- 使用PageHeap.EXE或GFlags.EXE检查内存越界错误
必先利其器之一:使用PageHeap.EXE或GFlags.EXE检查内存越界错误 Article last modified on 2002-6-3 ------------------------ ...
- Linux的内存映像导出接口—kcore
发表于 2012-4-10 15:00 /proc/kcore文件提供了整个机器的内存映像,和vmcore不同的是,它提供了一个运行时的内存映像,为此和vmcore一样,内核提供了一个类似的但是稍 ...
- 解决服务器上 w3wp.exe 和 sqlservr.exe 的内存占用率居高不下的方案
SQL Server是如何使用内存 最大的开销一般是用于数据缓存,如果内存足够,它会把用过的数据和觉得你会用到的数据统统扔到内存中,直到内存不足的时候,才把命中率低的数据给清掉.所以一般我们在看sta ...
- System.IO之内存映射文件共享内存
内存映射文件是利用虚拟内存把文件映射到进程的地址空间中去,在此之后进程操作文件,就 像操作进程空间里的地址一样了,比如使用c语言的memcpy等内存操作的函数.这种方法能够很好的应用在需要频繁处理一个 ...
- 如何用DELPHI编程修改外部EXE文件的版本信
右击里面有修改 点开直接修改就可以了吧. DELPHI 里程序的版本信息怎么是灰色的,无法更改 耐心读以下说明,应该能解决你的问题,如果不能解决,请Hi我~ 如何给自己的dll文件添加版本信息呢? 首 ...
随机推荐
- 第11章 拾遗4:IPv6(3)_配置IPv6路由
5. 配置IPv6路由 5.1 配置IPv6静态路由 (1)在路由器上配置静态路由(以R1路由器为例) //静态路由 R1#config t R1(config)#ipv6 unicast-routi ...
- 第10章 网络安全(5)_访问控制列表ACL
6. 访问控制列表ACL 6.1 标准访问控制列表 (1)标准ACL ①标准ACL是基于IP数据包的源IP地址作为转发或是拒绝的条件.即,所有的条件都是基于源IP地址的. ②基本不允许或拒绝整个协议组 ...
- CentOS7 安装 hbase1.3.3
1. 集群规划 ip地址 机器名 角色 192.168.1.101 palo101 hadoop namenode, hadoop datanode, yarn nodeManager, zookee ...
- 释放linux的buff/cache
有个linux的服务器,2G内存的,今天登上去一看,内存竟然被占得满满的. ssh上去执行了free. free -m total used free shared buff/cache availa ...
- for 练习
一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 var height = 0.00001; for (var i = 1; true; i++) { height *= ...
- Hadoop分布式文件系统HDFS
HDFS的探究: HDFS HDFS是 Hadoop Distribute File System的缩写,是谷歌GFS分布式文件系统的开源实现,Apache Hadoop的一个子项目,HDFS基于流数 ...
- Redis的集群模式
集群 即使使用哨兵,此时的Redis集群的每个数据库依然存有集群中的所有数据,从而导致集群的总数据存储量受限于可用存储内存最小的数据库节点,形成木桶效应.由于Redis中的所有数据都是基于内存存储,这 ...
- npm指南
什么是 NPM npm 之于 Node ,就像 pip 之于 Python , gem 之于 Ruby , pear 之于 PHP . npm 是 Node 官方提供的包管理工具,他已经成了 Node ...
- Docker使用札记 - 常用命令
1. 删除untagged的镜像(TAG列为<none>) docker images -f "dangling=true" -q|xargs docker rmi
- Mybatis-Plus3.0入门手册
Mybatis-Plus3.0入门手册 ref: https://blog.csdn.net/moshowgame/article/details/81008485 Mybatis-Plus简介 ...