求内存映射文件的例子(100分)

  • 主题发起人 主题发起人 clyin
  • 开始时间 开始时间
C

clyin

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