• 内存映射 读和写


    //
    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.
    书搞进脑袋 创新 创造; 积极
  • 相关阅读:
    一次事务过程与数据库管理过程
    如何连接oracle数据库及故障解决办法
    存储过程从入门到熟练(多个存储过程完整实例及调用方法)
    ASP.NET:通过反射填充泛型集合List的静态方法
    UTC datetime values in SQL Server 2000
    Datable 添加到Dataset 并且重新命名
    sp_help sp_helptext 存储过程的返回结果导出到表
    【转】目录 aspnet_client是什么?
    Javascript to generate excel 2
    Javascript verify integer or float value from textbox
  • 原文地址:https://www.cnblogs.com/tobetterlife/p/12161991.html
Copyright © 2020-2023  润新知