File memory mapping function description and application by jefong 2005/0407
The following functions are applied to file memory maps: 1) CreateFileMapping 2) FlushViewoffile 3) MapViewOffile 4) MapViewOffileEx 5) MapViewOffilevlm 6) OpenFileMapping 7) UnmapViewoffile 8) UnmapViewOffilevlm
Function Detailed Description: 1. CreateFilemapping creates a famous or unnamed file image for the specified file; Handle CreateFilemapping (Handle Hfile, // Map File Handle LPSecurity_Attributes LPFilemAppingAttributes, // Security Descriptor DWORD FLPROTECT, // Map Object Protect DWORD DWMAXIMUMSIZEHIGH, // Object Maximum Length 32-bit DWMaxImumsizerow, // The maximum length of the maximum length of 32-bit lpctstr lpname // file memory mapping object name;
Note: HFILE: The handle of the mapping file must be consistent with the FLPROTECT parameter; if this parameter value is 0xffffff, the size of the map object must be specified in the DWMaxImumSizeHigh, and the DWMaxImumsizeLow parameter. And will create a file mapping object in the operating system virtual memory page replacement file instead of using a disk file, and the size of this mapping object must be given. File mapping objects are shared by copies, genetic or names. LPFilemappingAttributes: Security Descriptor pointer determines whether the end handle can be inherited, if it is null, then the child process cannot inherit. In Winnt, if it is null, the file mapping object gets a default security descriptor. FLPROTECT: For the obtained file attempts to specify the protection mode, it can be set to the following value: read-only properties, and the HFile corresponding to the file must be opened as generic_read. Page_readwrite: Readable / writable properties, and the file corresponding to the HFile must be opened as generic_read and generic_write. Page_WriteCopy: After copying the writable area, the file corresponding to the HFile must be opened as generic_read and generic_write. DWMaxImumsizehigh, dwmaximumsizerow: If the two parameters are 0, the maximum length of the file mapping object is equal to the length of the file specified by the HFile. LPNAME: The name of the file mapping object, if this name already exists, then process the mapping object according to FLPROTECT. If this parameter is empty, create an unnamed file mapping object. If the name of this parameter is the same as the name of the system event, the function performs failed, getLastError returns ERROR_INVALID_HANDLE;
Return Value: The function call successfully returns the handle of the file mapping object. If the file mapping object already exists, return the handle of the original mapping object, GetLastError returns ERROR_ALREADY_EXISTS. Function failure returns NULL.
Second, FlushViewOffile writes the modified content or all of the file map view to the disk file Bool FlushViewoffile (LPCVOID LPBASEADDRESS, / / Modify the start address DWORD DWNUMBEROFBYTESTOFLUSH / / The number of bytes); the function is successfully returned zero. Three, MapViewOfFile in the address space of the calling process of mapping a document view LPVOID MapViewOfFile (HANDLE hFileMappingObject, file mapping object has been created // handle DWORD dwDesiredAccess, // access mode DWORD dwFileOffsetHigh, high 32 DWORD dwFileOffsetLow // file offset, // The low 32-bit DWORD DWNUMBEROFBYTOMAP // map of the file offset is the size of the view. Note: HFileMappingObject: The file mapping object handle returned by CREATEFILEMAPPING or OPENFILEMAPING. Dwdesired Access: The access mode of the map is related to the protection mode flprotect of the creation file mapping object, which can be set to the following: file_map_write: a file view of a readable write property is created, the protection mode is Page_Readwrite file_map_read: A read-only property File view is created, the protection mode is Page_ReadWrite or Page_ReadOnly file_map_all_access: When the file_map_write mode is the same file_map_copy: When you are Page_WriteCopy, get a view file, when you write the view file, the page is automatically exchanged, and you do Will damage the original data. DwnumberofbyTOMAP: The size of the map file is mapped, and the entire file is mapped. Return Value: If successfully returns the starting address of the returned map, if the failed returns NULL.
Four, MapViewOfFileEx in the address space of the calling process of mapping a document view and map view allows the calling process to specify a particular memory address LPVOID MapViewOfFileEx (HANDLE hFileMappingObject, handle DWORD dwDesiredAccess // file mapping object, // access mode DWORD dwFileOffsetHigh, / / Document offset High 32-bit DWRD DWFILEOFFSETLOW, // File Offset Low 32-bit DWORD DWORD DWROFBYTOMAP, // Map View Size LPVOID LPBASEADDRESS / / Specify the act of the mapping view "Note: That is the same as the MapViewOffile usage, but If the specified memory address space is not enough, the function will fail. Five, OpenFileMapping open a named file mapping object HANDLE OpenFileMapping (DWORD dwDesiredAccess, // access mode BOOL bInheritHandle, // inherit flag LPCTSTR lpName // file-mapping object name pointer); Note: dwDesiredAccess: access mode and access the MapViewOfFile The model is the same. BinheritHandle: Inheritance logo, can be inherited by a new process, if true, you can inherit your handle by a new process. Return Value: Successfully returns a named file mapping object, failed to return NULL. 6. UnmapViewoffile Delete File Mapping View Bool UnmapViewOffile (LPCVOID LPBASEADDRESS // Map View Start Address); Note: LPBaseAddress: Mapping View Start Address, generated by the MapViewOffile function MapViewOffileEx. Return Value: If the call is successfully returned to non-zero, and the dirty page within all specified addresses will be written to the hard disk. Call fails to return zero.
example:/*********************************************** ****** // * * // * This example creates a file mapping view and operates the file content through this mapping * // * view. * // * by jefong 05/04/08 * // * * // **************************************** ****************************** / # include