Examples of memory file ing applications 』

Source: Internet
Author: User

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 Description: "See the end of this page 』

Example:
/*************************************** **************/
/**/
/* In this example, the file ing view is created and mapped */
/* View to operate Composition Content. */
/**/
/*************************************** *************/
# 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;
}

 

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 the ing object
DWORD dwmaximumsizehigh, // The maximum length of an object is 32-bit.
DWORD dwmaximumsizelow, // The minimum 32-bit length of the object
lpname of the lpctstr // 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 writes all the modifications in the file ing view back to the disk file.
bool flushviewoffile (
lpcvoid lpbaseaddress, // The starting address of the modified content
DWORD dwnumberofbytestoflush // The 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, // The created file ing object handle
DWORD dwdesiredaccess, // access mode
DWORD dwfileoffsethigh, // 32-bit high file offset
DWORD dwfileoffsetlow, // 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 mode
file_map_copy: When the protection mode is page_writecopy, A View File is generated. When you write a view file, the page is automatically exchanged, and the changes you make will not damage the original data.
dwnumberofbytestomap: size of the mapped file. If it is 0, the entire file is mapped.
return value:
If the ing view start address is returned successfully, null is returned if the ing view fails.

4. mapviewoffileex maps a File View to the address space of the calling process, the calling process can also 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, // high 32-bit file offset
DWORD dwfileoffsetlow, // low 32-bit file offset
DWORD dwnumberofbytestomap, // ing View Size
lpvoid lpbaseaddress // specify the actual memory address of the ing view
);
Note:
the usage is the same as that of mapviewoffile, however, if the specified memory address space is insufficient, the function fails to be executed.
5. openfilemapping open a named file ing object
handle openfilemapping (
DWORD dwdesiredaccess, // access mode
bool binherithandle, // inheritance flag
lpctstr lpname // file ing Object Name Pointer
);
Note:
dwdesiredaccess: the access mode is the same as that in mapviewoffile.
binherithandle: Indicates 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 successfully. If the conversion 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.