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