如果你之一要用CreateFileMapping,那么我有个自己写的例子:
发送方:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
MapName = 'mophy map';
type
TMapInfo = record
info1: string[255];
info2: string[255];
end;
type
TMainForm = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
hMap: THandle;
pInfo: Pointer;
MapSize: Integer = SizeOf(TMapInfo);
implementation
{$R *.dfm}
procedure TMainForm.FormCreate(Sender: TObject);
begin
hMap := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE or SEC_COMMIT,
0, MapSize, MapName);
end;
procedure TMainForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
CloseHandle(hMap);
end;
procedure TMainForm.Button1Click(Sender: TObject);
var
MapInfo: TMapInfo;
begin
MapInfo.info1 := Edit1.Text;
MapInfo.info2 := Edit2.Text;
pInfo := MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, MapSize);
Move(MapInfo, pInfo^, MapSize);
UnmapViewOfFile(pInfo);
end;
end.
接受方:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
//MapSize = 1024;
MapName = 'mophy map';
type
TMapInfo = record
info1: string[255];
info2: string[255];
end;
type
TMainForm = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
hMap: THandle;
pInfo: Pointer;
MapSize: Integer = SizeOf(TMapInfo);
implementation
{$R *.dfm}
procedure TMainForm.Button1Click(Sender: TObject);
var
MapInfo: TMapInfo;
begin
hMap := OpenFileMapping(FILE_MAP_WRITE, True, MapName);
pInfo := MapViewOfFile(hMap, FILE_MAP_WRITE, 0, 0, MapSize);
MapInfo := TMapInfo(pInfo^);
Edit1.Text := MapInfo.info1;
Edit2.Text := MapInfo.info2;
UnmapViewOfFile(pInfo);
CloseHandle(hMap);
end;
end.