码迷,mamicode.com
首页 > 其他好文 > 详细

内存映射 读和写

时间:2020-01-07 16:29:02      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:als   sizeof   nmap   应用   ping   control   ctr   phi   line   

//
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    mmo1: TMemo;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    btn5: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
  private
    { Private declarations }
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  PShareMem = ^TShareMem;

  TShareMem = Record
    id: string[10];
    name: string[20];
    age: Integer;
  end;

var
  shareMemName: string=zl; // 共享内存名
  fileHandle: THandle; // 内存映射文件句柄
  pUserInfoShareMem: PShareMem; // 指向共享内存的指针
  userInfoStr:string;

  // 1)创建“内存映射文件”
procedure TForm1.btn1Click(Sender: TObject);
begin
   fileHandle:=OpenFileMapping(FILE_MAP_ALL_ACCESS,false,pchar(shareMemName));
  if FileHandle <> 0 then
  begin
    Self.mmo1.Lines.Add(已成功打开内存映射文件!)
  end;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
 pUserInfoShareMem:= MapViewOfFile(FileHandle,FILE_MAP_ALL_ACCESS,0,0,sizeof(TShareMem));
   if pUserInfoShareMem <> nil then
   begin
     Self.mmo1.Lines.Add(已成功建立映射关系!);
   end;
end;

procedure TForm1.btn3Click(Sender: TObject);
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);
      Self.mmo1.Lines.Add(userInfoStr);
   end;
end;

procedure TForm1.btn4Click(Sender: TObject);
begin
if pUserInfoShareMem<> nil then
     UnmapViewOfFile(pUserInfoShareMem);
  Self.mmo1.Lines.Add(已成功解除映射关系!);
end;

procedure TForm1.btn5Click(Sender: TObject);
begin
 if fileHandle<> 0 then
     CloseHandle(fileHandle);
  Self.mmo1.Lines.Add(已成功关闭内存映射文件!);
end;

end.


//
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    mmo1: TMemo;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    btn5: TButton;
    edtName: TEdit;
    lbl姓名: TLabel;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
    procedure btn5Click(Sender: TObject);
  private
    { Private declarations }
  public

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  PShareMem = ^TShareMem;

  TShareMem = Record
    id: string[10];
    name: string[20];
    age: Integer;
  end;

var
  shareMemName: string=zl; // 共享内存名
  fileHandle: THandle; // 内存映射文件句柄
  pUserInfoShareMem: PShareMem; // 指向共享内存的指针

  // 1)创建“内存映射文件”
procedure TForm1.btn1Click(Sender: TObject);
begin
   //创建“内存映射文件”
   fileHandle:=CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(TShareMem), PChar(shareMemName));
   if fileHandle <> 0 then
   begin
     Self.mmo1.Lines.Add(已成功创建内存映射文件!);
   end;
end;

//建立映射关系
procedure TForm1.btn2Click(Sender: TObject);
begin
  //将“内存映射文件”与“应用程序地址空间”建立映射关系
  pUserInfoShareMem:=MapViewOfFile(fileHandle,FILE_MAP_ALL_ACCESS,0,0,sizeof(TShareMem));
  if pUserInfoShareMem <> nil then
  begin
     Self.mmo1.Lines.Add(已成功建立映射关系!);
  end;
end;

procedure TForm1.btn3Click(Sender: TObject);
begin
   pUserInfoShareMem.id:=8888;
   //pUserInfoShareMem.name:=‘Terry‘;
   pUserInfoShareMem.name:=edtName.Text;
   pUserInfoShareMem.age:=25;
   Self.mmo1.Lines.Add(已向共享内存中写入用户信息!);
end;

procedure TForm1.btn4Click(Sender: TObject);
begin
  //解除“内存映射文件”与“应用程序地址空间”的映射关系
  if pUserInfoShareMem<> nil then
     UnmapViewOfFile(pUserInfoShareMem);
  Self.mmo1.Lines.Add(已成功解除映射关系!);
end;

procedure TForm1.btn5Click(Sender: TObject);
begin
  //关闭内存映射文件
  if fileHandle<> 0 then
     CloseHandle(fileHandle);
  Self.mmo1.Lines.Add(已成功关闭内存映射文件!);
end;

end.

内存映射 读和写

标签:als   sizeof   nmap   应用   ping   control   ctr   phi   line   

原文地址:https://www.cnblogs.com/tobetterlife/p/12161991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!