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文件添加版本信息呢? 首 ...
随机推荐
- JVM性能、多线程排查常用命令
最近遇到很一个很棘手的多线程问题,跟踪了几天终于解决了,在此记录跟踪过程的常用命令,后期有空再做具体的事件总结.软件的开发一定要有监控,一定要有监控,一定要有监控,重要的事情说三遍.没有监控的软件就是 ...
- CRM 2016 刷新 Iframe
在CRM中刷新IFame: /// <summary>刷新Iframe的内容,用于表单上刷新iframe里的内容</summary> var iframe = Xrm.Page ...
- Round544div3E(1133E)
一.题目链接 https://codeforces.com/problemset/problem/1133/E 二.思路 显然要使用dp,因为中间有部分人不会选取. 令$dp[i][j]$表示在前$i ...
- (转)C#动态webservice调用接口
原文地址:http://www.cnblogs.com/zouhao/p/6236785.html ,此处仅测试了使用Http post请求调用webservice 调用类: using Syst ...
- SQL Server 导入超大脚本
另外使用window server 版操作系统,执行脚本文件比普通版操作系统大大提升大小限制. 在执行SQL脚本的时候要是出现了这些情况我咋办呢? 步入正轨 应用场景:服务器升级,比如原来是2003的 ...
- dubbo-文档
Srping版Dubbo集成中文地址:https://dubbo.gitbooks.io/dubbo-user-book/content/preface/background.html SpringB ...
- JQ获取选中select 标签的值
Jq://#ses为select 标签的Id$("#ses option:selected").val(); $("#ses option:selected") ...
- hadoop启动问题分析
hadoop的安装和启动很简单直接解压进行安装 配置文件就好了,但是启动问题就很多,总结下无非以下两点: 第一点:无论你是群起还是单起;都首要格式化 bin/hdfs namenode -form ...
- win10更改hosts文件
管理员身份运行notepad,打开hosts文件即可.
- 1.正则re
正则 :规则表达式 一般在匹配非结构化的数据时用的比较多,结构化的数据一般用xpath,bs4.但具体使用起来都是视情况而定,相对而言.正则规则平时涉及最多也就是匹配邮箱,电话,及特殊字符串.规则相对 ...