--文章资料来源: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--结束