内存映射文件,无法共享,请帮忙(50分)

  • 主题发起人 主题发起人 wp231957
  • 开始时间 开始时间
W

wp231957

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
maphandle:thandle;
implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var
lsize:integer;
pview:pointer;
begin
lsize:=4*1024;
maphandle:=createfilemapping(dword($ffffffff),nil,PAGE_READWRITE,0,lsize,pchar('smapname'));
if maphandle=0 then exit;
if getlasterror()=error_already_exists then begin
showmessage('MAPÒѾ­´æÔÚ');
closehandle(maphandle);
exit;
end;
pview:=mapviewoffile(maphandle,FILE_MAP_READ OR FILE_MAP_WRITE,0,0,4096);
if pview=nil then exit;
pchar(pview):=pchar(edit1.text);  //计划把EDIT.TEXT写如共享区
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
closehandle(maphandle);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
hfilemap:thandle;
pview:pointer;
begin
hfilemap:=openfilemapping(FILE_MAP_READ OR FILE_MAP_WRITE,false,pchar('smapname'));
if hfilemap=0 then begin
showmessage('not found map');
exit;
end;
pview:=mapviewoffile(hfilemap,FILE_MAP_READ OR FILE_MAP_WRITE,0,0,4096);
if pview=nil then exit;
edit1.Text :=pchar(pview);   //从共享区读数据,可是没实现
end;

end.
 
好象知道了
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Label1: TLabel;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
maphandle:thandle;
hfilemap:thandle;
implementation

{$R *.dfm}
//创建MAP
procedure TForm1.Button2Click(Sender: TObject);
var
lsize:integer;
pview:pointer;
begin
lsize:=4*1024;
maphandle:=createfilemapping(dword($ffffffff),nil,PAGE_READWRITE,0,lsize,pchar('smapname'));
if maphandle=0 then exit;
if getlasterror()=error_already_exists then begin
showmessage('MAP已经存在');
closehandle(maphandle);
exit;
end;
pview:=mapviewoffile(maphandle,FILE_MAP_READ OR FILE_MAP_WRITE,0,0,0);
if pview=nil then exit;
strcopy(pchar(pview),pchar(edit1.text));
unmapviewoffile(pview);
end;
//关闭MAP
procedure TForm1.Button3Click(Sender: TObject);
begin
if closehandle(maphandle) and closehandle(hfilemap) then showmessage('关闭MAP成功');
end;
//打开MAP并获取数据
procedure TForm1.Button1Click(Sender: TObject);
var
pview:pointer;
begin
hfilemap:=openfilemapping(FILE_MAP_READ OR FILE_MAP_WRITE,false,pchar('smapname'));
if hfilemap=0 then begin
showmessage('not found map');
exit;
end;
pview:=mapviewoffile(hfilemap,FILE_MAP_READ OR FILE_MAP_WRITE,0,0,0);
if pview=nil then exit;
edit1.Text:=pchar(pview);
unmapviewoffile(pview);
end;

end.
//还有一个小问题,我无法进行第2次创建MAP,总是提示我MAP已经存在,可我明明把它关了啊
 
创建映像,最后有一个参数名,不能两个都是相同的三
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
hMapFile: THandle;
MapFilePointer: Pointer;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
hMapFile := CreateFileMapping ($FFFFFFFF, nil, page_ReadWrite, 0,10000,'ctaxp'); // 文件名
if hMapFile <> 0 then MapFilePointer := MapViewOfFile(hMapFile,File_Map_All_Access,0, 0, 0);
StrCopy (PChar (MapFilePointer),PChar (edit1.Text));//写入内存
end;

procedure TForm1.Button2Click(Sender: TObject);
var ss:string;
begin
hMapFile := CreateFileMapping ($FFFFFFFF, nil, page_ReadWrite, 0,10000,'ctaxp'); // 文件名
if hMapFile <> 0 then MapFilePointer := MapViewOfFile(hMapFile,File_Map_All_Access,0, 0, 0);
ss:=PChar(MapFilePointer);
edit2.Text :=ss;
end;

end.
 

Similar threads

I
回复
0
查看
584
import
I
I
回复
0
查看
608
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部