Description and application of File Memory ing Functions
By jefong 2005/0407
The following functions are used in File Memory ing:
1) createfilemapping
2) flushviewoffile
3) mapviewoffile
4) mapviewoffileex
5) mapviewoffilevlm
6) openfilemapping
7) unmapviewoffile
8) unmapviewoffilevlm
Function details:
1. createfilemapping creates a famous or unknown file image for the specified file;
Handle createfilemapping (
Handle hfile, // the handle of the ing File
Lpsecurity_attributes lpfilemappingattributes,
// Security descriptor pointer
DWORD flprotect, // protection of ing objects
DWORD dwmaximumsizehigh, // the maximum length of the object is 32 bits
DWORD dwmaximumsizelow, // the maximum length of the object is 32 bits
Lptstr lpname // name of the file memory ing object
);
Note:
Hfile: the handle of the ing file. The open mode of the file must be consistent with that specified by the flprotect parameter. If the value of this parameter is 0 xffffffffff, you must specify the size of the ing object in the dwmaximumsizehigh and dwmaximumsizelow parameters. In addition, the file ing object will be created in the replace file on the Virtual Memory Page of the operating system, instead of using the disk file, and the size of the ing object must be given. File ing objects are shared by copies, inheritance, or names.
Lpfilemappingattributes: Security Descriptor pointer, which determines whether the return handle can be inherited by the quilt process. If it is null, the child process cannot be inherited. In WINNT, if it is null, the file ing object gets a default security descriptor.
Flprotect: attempts to specify the protection mode for the obtained file. It can be set to the following values:
Page_readonly: Read-only attribute, and the file corresponding to hfile must be opened in the form of generic_read.
Page_readwrite: readable and writable attributes, and the file corresponding to hfile must be opened in the form of generic_read and generic_write.
Page_writecopy: this operation is performed after the writable area is copied, and the file corresponding to the hfile must be opened in the form of generic_read and generic_write.
Dwmaximumsizehigh, dwmaximumsizelow: if the two parameters are 0, the maximum length of the file ing object is equal to the file length specified by hfile.
Lpname: name of the file ing object. If this name already exists, the ing object will be processed according to the name specified by flprotect. If this parameter is null, a file ing object without a name is created. If the name of this parameter is the same as that of the System Event, the function fails to be executed. getlasterror returns error_invalid_handle;
Returned value: the handle of the file ing object is returned after the function call is successful. If the file ing object already exists, the handle of the original ing object is returned. getlasterror returns error_already_exists. If the function fails to be executed, null is returned.
2. flushviewoffile write all the modifications in the file ing view back to the disk file.
Bool flushviewoffile (
Lpcvoid lpbaseaddress, // start address of the modified content
DWORD dwnumberofbytestoflush // number of modified bytes
);
If the function is successfully executed, a non-zero value is returned.
3. mapviewoffile maps a File View to the address space of the calling Process
Lpvoid mapviewoffile (
Handle hfilemappingobject, // handle of the created file ing object
DWORD dwdesiredaccess, // Access Mode
DWORD dwfileoffsethigh, // 32-bit file offset height
DWORD dwfileoffsetlow, // low 32-bit file offset
DWORD dwnumberofbytestomap // ing View Size
);
Note:
Hfilemappingobject: handle of the file ing object returned by createfilemapping or openfilemapping.
Dwdesiredaccess: Access Mode of the ing view, which is related to the protection mode flprotect of the created file ing object. It can be set to the following values:
File_map_write: A file view with read/write attributes is created. The protection mode is page_readwrite.
File_map_read: A file view with read-only attributes is created. The protection mode is page_readwrite or page_readonly.
File_map_all_access: Same as file_map_write
File_map_copy: When the protection mode is page_writecopy, A View File is obtained. When you write a view file, the page is automatically exchanged, and your modifications do not damage the original data.
Dwnumberofbytestomap: the size of the part of the ing file. If it is 0, the entire file is mapped.
Return Value:
If yes, return the start address of the ing view. If no, return null.
4. mapviewoffileex maps a File View to the address space of the calling process, and allows the calling process to specify a special memory address for the ing view.
Lpvoid mapviewoffileex (
Handle hfilemappingobject, // handle of the file ing object
DWORD dwdesiredaccess, // Access Mode
DWORD dwfileoffsethigh, // 32-bit file offset height
DWORD dwfileoffsetlow, // low 32-bit file offset
DWORD dwnumberofbytestomap, // map the size of the View
Lpvoid lpbaseaddress // specify the actual memory address of the ing View
);
Note:
Same as mapviewoffile, function execution fails if the specified memory address space is not enough.
5. openfilemapping open a named file ing object
Handle openfilemapping (
DWORD dwdesiredaccess, // Access Mode
Bool binherithandle, // inheritance flag
Lptstr lpname // Object Name Pointer mapped to a file
);
Note:
Dwdesiredaccess: the access mode is the same as that in mapviewoffile.
Binherithandle: the inheritance flag, whether it can be inherited and used by a new process. If it is true, it can be inherited by a new process.
Return Value:
A named file ing object is returned. If the ing object fails, null is returned.
6. unmapviewoffile: Delete the ing view of a file
Bool unmapviewoffile (
Lpcvoid lpbaseaddress // start address of the ing View
);
Note:
Lpbaseaddress: the start address of the ing view, which is generated by mapviewoffileex.
Return Value:
If the call is successful, a non-zero value is returned, and all dirty pages in the specified address are written to the hard disk. Zero is returned if the call fails.
Example:
/*************************************** **************/
/**/
/* In this example, the file ing view is created and mapped */
/* View to operate the file content. */
/* By jefong 05/04/08 */
/**/
/*************************************** *************/
# Include <stdio. h>
# Include <stdlib. h>
# Include <windows. h>
Int main ()
{
Hfile;
Ofstruct opbuf;
Handle hmapfile;
Handle hmapview;
Byte * Recv;
Hfile = openfile (". // map. Test", & opbuf, of_readwrite );
If (hfile = hfile_error)
{
Printf ("Open File failed! /N ");
Return 1;
}
Hmapfile = createfilemapping (handle) hfile, null, page_readwrite, 0, 0, "maptest ");
If (hmapfile = NULL)
{
Printf ("Mapping File failed! /N ");
Return 1;
}
Closehandle (handle) hfile );
Hfile = 0;
Hmapview = mapviewoffile (hmapfile, file_map_write, 0, 0 );
If (hmapview = NULL)
{
Printf ("Mapping View failed! /N ");
Return 1;
}
Recv = (byte *) hmapview;
Printf ("Mapping View's content is: %. 10 s/n", Recv );
Recv [0] ='s ';
Printf ("Mapping View's content is: %. 10 s/n", Recv );
Unmapviewoffile (hmapview );
Return 0;
}