用内存影射文件,这是我抄的一个例子:
主要是其中的几个API 函数
var
Form1: TForm1;
Data: Pointer; // holds a pointer to the memory mapped file
hMapping: THandle; // holds a handle to the memory mapped file object
implementation
function OpenMappedFile(FileName: string; var FileSize: DWORD): Boolean;
var
hFile: THandle; // a handle to the opened file
HighSize: DWORD; // the high order double word of the file size
begin
{initialize the result of the function in case of errors}
Result := FALSE;
{if no filename was specified, exit}
if Length(FileName)= 0 then Exit;
{open the file for reading and writing. indicate a
sequential scan access for better optimization}
hFile := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ, nil, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, 0);
{if the file was not opened, exit;}
if hFile = INVALID_HANDLE_VALUE then Exit;
{retrieve the size of the file}
FileSize := GetFileSize(hFile, @HighSize);
{create a read/write mapping of the opened file}
hMapping:=CreateFileMapping(hFile, nil, PAGE_READWRITE, 0, 0,
'Delphi File Mapping Example');
{if the file mapping failed, exit}
if (hMapping = 0) then
begin
CloseHandle(hFile);
Exit;
end;
{close the file handle, as we no longer need it}
CloseHandle(hFile);
{map a view of the file}
Data := MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);
{if a view of the file was not created, exit}
if (Data=nil) then Exit;
{to insure that the file's data can be displayed directly as
a string, set the very last byte in the file data to a null terminator}
PChar(Data)[FileSize] := #0;
{the file was successfully opened and mapped}
Result := TRUE;
end;
function OpenPreviousMappedFile(var FileSize: Integer): Boolean;
begin
{initialize the result of the function incase of errors}
Result := FALSE;
{open an existing file mapping}
hMapping := OpenFileMapping(FILE_MAP_WRITE, FALSE,
'Delphi File Mapping Example');
{if there was an error opening the existing file mapping, exit}
if hMapping=0 then Exit;
{map a view of the file}
Data := MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);
{if a view of the file was not created, exit}
if (Data=nil) then Exit;
{retrieve the length of the data (which can be represented as a null
terminated string due to adding the null terminator to the end when
the file was opened}
FileSize := StrLen(PChar(Data));
{indicate that the file was successfully opened and mapped}
Result := TRUE;
end;
procedure DisplayMappedFile(FileName: string; Size: Integer);
var
Index: Integer; // general loop counter
DataPtr: PChar; // a pointer to the mapped file data
HexString: PChar; // a pointer to a concatenated hexadecimal string
begin
{display the name of the mapped file}
Form1.StatusBar1.SimpleText := FileName;
{allocate memory for the hexadecimal representation of the file,
and initialize it to zeros}
GetMem(HexString,Size * 3);
ZeroMemory(HexString, Size*3);
{set the pointer to the beginning of the mapped file data}
DataPtr := Data;
{begin looping through the data}
Index:=0;
while (Index < Size) do
begin
{display the value of each byte in the file as a hexadecimal number}
StrCat(HexString, PChar(IntToHex(Byte(DataPtr[Index]),2)+ ' '));
Inc(Index);
end;
{display the hexadecimal representation of the data and the ASCII
representation of the data}
SetWindowText(Form1.Memo1.Handle,HexString);
SetWindowText(Form1.Memo2.Handle,PChar(Data));
{free the memory for the hexadecimal string}
FreeMem(HexString, Size * 3);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Size: Integer; // holds the size of the memory mapped file
begin
{open an existing file...}
if OpenDialog1.Execute then
begin
{...map the file...}
OpenMappedFile(OpenDialog1.FileName, Size);
{...and display the memory mapped file}
DisplayMappedFile(OpenDialog1.FileName, Size);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
Size: Integer; // holds the size of the memory mapped file
begin
{open a previously mapped file...}
if OpenPreviousMappedFile(Size) then
{...and display it}
DisplayMappedFile('Existing mapped file', Size);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{write any changes to the file}
FlushViewOfFile(Data, 0);
{unmap the view of the file}
if not UnMapViewOfFile(Data) then ShowMessage('Can not unmap file');
{close the mapped file handle}
if not CloseHandle(hMapping) then ShowMessage('Can not Close File');
end;