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. 030、Java中的求模计算

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  2. Spark 资料整理

    http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ http://blog.csdn.net/aliv ...

  3. UVA - 12083 Guardian of Decency (二分匹配)

    题意:有N个人,已知身高.性别.音乐.运动.要求选出尽可能多的人,使这些人两两之间至少满足下列四个条件之一. 1.身高差>40  2.性别相同  3.音乐不同  4.运动相同 分析: 1.很显然 ...

  4. CentOS 6.8 32位 安装mysql8

    1.清理掉之前安装过的mysql rpm -qa | grep mysql mysql-libs-5.1.52-1.el6_0.1.x86_64 yum remove mysql-libs-5.1.5 ...

  5. raspberry pi-php-exec

    遇到的问题是在树莓派上搭建的web服务器,想通过网页操控树莓派的gpio,网页是通过php实现的,通过php的exec函数调用写好的程序实现对gpio的操作,但是赖何没有效果,分析也知道是权限问题,最 ...

  6. sping MVC 定时任务的设置

    项目中用到了定时任务,写一篇随笔记录一下. 首先在Spring的配置文件ApplicationContext.xml文件的beans标签中添加 xmlns:task="http://www. ...

  7. WinForm读写App.config配置文件

    一.配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序.配置文件的根节点是conf ...

  8. c++程序—三目运算符

    #include<iostream> using namespace std; #include<string> int main() { //三目运算符 ; ; ; c = ...

  9. [GXYCTF2019]BabyUpload

    0x00 知识点 文件类型绕过: Content-Type: image/jpeg apache环境下上传.hatcess 首先上传一个.htaccess内容如下的文件 :SetHandler app ...

  10. SpringBoot+SpringSecurity之如何forword到登录页面

    当我们在项目中引入了SpringSecurity框架进行身份校验的时候,如果某个请求需要用户身份认证,那么SpringSecurity会将用户redirect到登录页面.但是有些时候我们希望是forw ...