读内存映射文件(100分)

  • 主题发起人 主题发起人 qiqihome
  • 开始时间 开始时间
Q

qiqihome

Unregistered / Unconfirmed
GUEST, unregistred user!
如果读取一个内存映射文件,从位置i,读n个字节,再存入数组中?
 
http://www.csdn.net/dev/delphi/Samples/mmapfile.zip
By David Spies.
如何使用Win32内存映象文件,实现多个程序数据交换
 
procedure TForm1.Button2Click(Sender: TObject);
var
hFile, hMap : THandle;
pData : Pointer;
lpReOpenBuff: TOFStruct;
Buff : array[0..255] of Char;
Const
OFFSET_NUM = 10
//从位置i-1
begin
hFile := OpenFile('c:/1.txt', lpReOpenBuff, OF_READ);
if hFile <> HFILE_ERROR then
begin
hMap := CreateFileMapping(hFile, nil, PAGE_READONLY, 0, 0, nil);
if hMap <> 0 then
begin
pData := MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
CopyMemory(@Buff, Pointer(LongInt(pData) + OFFSET_NUM), 255);
ShowMessage(Buff);
UnmapViewOfFile(pData);
CloseHandle(hMap);
end;
CloseHandle(hFile);
end;
end;
 
如何将一个image型字段的数据写入到内存映射文件呢,又怎样读出
 
procedure TForm1.Button3Click(Sender: TObject);
var
hMap : THandle;
pMap : Pointer;
BlobStream : TBlobStream;
begin
BlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Graphic')), bmRead);
BlobStream.Position := 0;
hMap := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
0, BlobStream.Size, 'MAP_NAME');
if hMap <> 0 then
begin
pMap := MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if pMap <> nil then
begin
BlobStream.ReadBuffer(pMap^, BlobStream.Size);
UnmapViewOfFile(pMap);
end;
CloseHandle(hMap);
end;
BlobStream.Destroy;
end;
 
接受答案了.
 
后退
顶部