内存映象文件的问题!谢谢!(0分)

  • 主题发起人 主题发起人 xiuguo
  • 开始时间 开始时间
X

xiuguo

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在建立一个内存映象文件之后,可以通过建立映象视图多次读取,为什么不行?
具体程序如下,为什么Button1只能执行一次?
var
hMapping:Thandle;
FFileHandle: THandle;
//Handle to the open file. 指向打开文件的句柄
FMapHandle: THandle;
//Handle to a file-mapping object 指向映射文件的对象
FFileSize: Integer;
//Variable to hold the file size. 保存文件大小的尺寸
FData: PByte;
//Pointer to the file's data when mapped. 指针指向文件的数据当映射的时候
PData: PChar;
Procedure TForm4.FormCreate(Sender: TObject);
begin
if not FileExists(FName) then
//FName指向一个文件名
raise Exception.Create('Filedo
es not exist.')
else
FFileHandle := FileOpen(FName, fmOpenReadWrite);//以可读写状态的方式打开
If FFileHandle = INVALID_HANDLE_VALUE then
raise Exception.Create('Failed to open or create file');
//文件没有打开
Try
FFileSize := GetFileSize(FFileHandle, Nil);
FMapHandle := CreateFileMapping(FFileHandle, Nil, PAGE_READWRITE, 0, FFileSize, nil);
If FMapHandle = 0 then
Raise Exception.Create('Failed to create file mapping');
Finally
CloseHandle(FFileHandle);
end;
end;

procedure TForm4.Button1Click(Sender: TObject);
var ten,one,num,i:integer;
selectstring,tstr:string;
begin
try
FData := MapViewOfFile(FMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, FFileSize);
if FData = Nil then
raise Exception.Create('Failed to map view of file');
finally
CloseHandle(FMapHandle);
end;

try
PData := PChar(FData);
inc(PData, FFileSize);
PData^ := #0;
selectstring:=trim(PChar(FData));
i:=1;
tstr:='';
while selectstring<>' 'do
begin
tstr:=tstr+selectstring;
i:=i+1;
end;
Edit1.Text:=tstr;
finally
UnmapViewOfFile(FData);
end;
end;

 
有结果,告诉我一声
 
非常关心
 
接受答案了.
 
后退
顶部