内存映射 读和写

Posted tobetterlife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了内存映射 读和写相关的知识,希望对你有一定的参考价值。

//
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.

以上是关于内存映射 读和写的主要内容,如果未能解决你的问题,请参考以下文章

读和写

14java实现poi操作excel,包括读和写日期格式,并且设置字体样式

POI完成Excel文件的读和写

C# File文件类的使用 “读和写”的操作 (详细)

NIO 中的读和写

Python open 读和写