• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi中共享内存学习

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

--文章资料来源:https://blog.csdn.net/weixin_30426957/article/details/96224027?utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242

 

--------Unit---开始

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Memo1: TMemo;
Memo2: TMemo;
Button5: TButton;
Button6: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
PShareMem = ^TShareMem;
TShareMem = Record
id:string[10];
name:string[20];
age:Integer;
end;
var
Form1: TForm1;
shareMemName:string=\'A_Mapping_Test_0001\'; //共享内存名
fileHandle : THandle;//内存映射文件句柄
pUserInfoShareMem : PShareMem;//指向共享内存的指针

RshareMemName:string=\'A_Mapping_Test_0001\'; //共享内存名
RfileHandle : THandle;//内存映射文件句柄
RpUserInfoShareMem : PShareMem;//指向共享内存的指针
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
fileHandle:=CreateFileMapping($FFFFFFFF,nil,PAGE_READWRITE ,0, SizeOf(TShareMem), PChar(shareMemName));
pUserInfoShareMem:=MapViewOfFile(fileHandle,FILE_MAP_ALL_ACCESS,0,0,sizeof(TShareMem));
if pUserInfoShareMem <> nil then
begin
Memo1.Lines.Add(\'已成功建立映射关系!\');
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
pUserInfoShareMem.id:=\'8888\';
pUserInfoShareMem.name:=\'Terry\';
pUserInfoShareMem.age:=25;
Memo1.Lines.Add(\'已向共享内存中写入用户信息!\');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnmapViewOfFile(pUserInfoShareMem);
Memo1.Lines.Add(\'已成功解除映射关系!\');
CloseHandle(fileHandle);
Memo1.Lines.Add(\'已成功关闭内存映射文件!\');
end;

procedure TForm1.Button4Click(Sender: TObject);
var
userInfoStr:string;
begin
RfileHandle:=OpenFileMapping(FILE_MAP_ALL_ACCESS,false,pchar(RshareMemName));
if RFileHandle <> 0 then
begin
Memo2.Lines.Add(\'已成功打开内存映射文件!\') end;

RpUserInfoShareMem:= MapViewOfFile(RFileHandle,windows.FILE_MAP_ALL_ACCESS,0,0,sizeof(TShareMem));
if RpUserInfoShareMem <> nil then
begin
Memo2.Lines.Add(\'已成功建立映射关系!\'); end;
if RpUserInfoShareMem <> nil then
begin
userInfoStr:=\'共享内存中获取的用户信息如下:\'+#13#10;
userInfoStr:=userInfoStr+\'用户Id号:\'+RpUserInfoShareMem.id+#13#10;
userInfoStr:=userInfoStr+\'用户姓名:\'+RpUserInfoShareMem.name+#13#10;
userInfoStr:=userInfoStr+\'用户年龄:\'+IntToStr(RpUserInfoShareMem.age);
Memo2.Lines.Add(userInfoStr); end;
end;

procedure TForm1.Button5Click(Sender: TObject);
var
userInfoStr:string;
begin
if pUserInfoShareMem <> nil then
begin
userInfoStr:=\'共享内存中获取的用户信息如下:\'+#13#10;
userInfoStr:=userInfoStr+\'用户Id号:\'+pUserInfoShareMem.id+#13#10;
userInfoStr:=userInfoStr+\'用户姓名:\'+pUserInfoShareMem.name+#13#10;
userInfoStr:=userInfoStr+\'用户年龄:\'+IntToStr(pUserInfoShareMem.age);
Memo2.Lines.Add(userInfoStr); end;
end;


procedure TForm1.Button6Click(Sender: TObject);
begin
if RpUserInfoShareMem<> nil then
UnmapViewOfFile(RpUserInfoShareMem);
Memo2.Lines.Add(\'已成功解除映射关系!\');


if RfileHandle<> 0 then
CloseHandle(RfileHandle);
Memo2.Lines.Add(\'已成功关闭内存映射文件!\');
end;

end.

--------Unit---结束

---------Form--开始

object Form1: TForm1
Left = 717
Top = 334
Width = 363
Height = 424
Caption = \'Form1\'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = \'MS Sans Serif\'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 8
Top = 8
Width = 75
Height = 25
Caption = \'建立\'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 192
Top = 48
Width = 75
Height = 25
Caption = \'释放\'
TabOrder = 1
OnClick = Button2Click
end
object Button3: TButton
Left = 8
Top = 40
Width = 75
Height = 25
Caption = \'写入\'
TabOrder = 2
OnClick = Button3Click
end
object Button4: TButton
Left = 16
Top = 144
Width = 75
Height = 25
Caption = \'读取\'
TabOrder = 3
OnClick = Button4Click
end
object Memo1: TMemo
Left = 112
Top = 0
Width = 161
Height = 49
ImeName = \'中文(简体) - 搜狗拼音输入法\'
ScrollBars = ssBoth
TabOrder = 4
end
object Memo2: TMemo
Left = 88
Top = 144
Width = 185
Height = 89
ImeName = \'中文(简体) - 搜狗拼音输入法\'
ScrollBars = ssBoth
TabOrder = 5
end
object Button5: TButton
Left = 104
Top = 48
Width = 75
Height = 25
Caption = \'读取\'
TabOrder = 6
OnClick = Button5Click
end
object Button6: TButton
Left = 128
Top = 240
Width = 75
Height = 25
Caption = \'释放\'
TabOrder = 7
OnClick = Button6Click
end
end

 

----------Form--结束


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
delphi7clientdataset详解发布时间:2022-07-18
下一篇:
DelphiI/Oerror103错误发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap