CreateFileMapping, MapViewOfFile, UnmapViewOfFile function usage and examples

Source: Internet
Author: User
Tags rar terminates

Memory-Mapped API function CreateFileMapping Create a well-known shared memory:
HANDLE CreateFileMapping (
HANDLE hfile,//mapping file handle,
Set to 0xFFFFFFFF to create an object that is shared between processes
Lpsecurity_attributes lpfilemappingattributes,//Security properties
DWORD Flprotect,//protection mode
DWORD Dwmaximumsizehigh,//Size of Object
DWORD Dwmaximumsizelow,
LPCTSTR lpname//must be named for the mapping file
);

Similar to virtual memory, the protection can be page_readonly or page_readwrite. If multiple processes have write access to the same shared memory, you must maintain synchronization between each other. The mapping file can also specify the PAGE_WRITECOPY flag to ensure that its original data is not compromised, while allowing other processes to manipulate the copy of the data as necessary.

After creating the file mapping object, use the address space that you can call the MapViewOfFile function to map to this process.

The following description creates a well-known mapping file named Mysharedmem with a length of 4096 bytes:
HANDLE hmysharedmapfile=createfilemapping ((HANDLE) 0xFFFFFFFF),
null,page_readwrite,0,0x1000, "Mysharedmem");
and map the buffer view:
LPSTR pszmysharedmapview= (LPSTR) mapviewoffile (Hmysharedmapfile,
file_map_read| file_map_write,0,0,0);

Other processes access the shared object, need to get the object name and call the OpenFileMapping function.
HANDLE hmysharedmapfile=openfilemapping (File_map_write,
FALSE, "Mysharedmem");

Once the other process gets the handle to the mapped object, you can call the MapViewOfFile function to map the view of the object as you would create the process. Users can use this object view for data read and write operations to achieve the purpose of data communication.

When the user process ends using shared memory, call the UnmapViewOfFile function to cancel the view in its address space:
if (! UnmapViewOfFile (Pszmysharedmapview))
{

AfxMessageBox ("Could not unmap view of file");

}

The following is an example program: void Createfilemappingex ()
{
DWORD Timebegin =:: timeGetTime ();
HANDLE fp = CreateFile (TEXT ("E://jyzhj2.rar"),//Here Enter the file you want to copy src
Generic_read | Generic_write,
File_share_read,
Null
Open_existing,
File_flag_sequential_scan,
NULL);

if (fp = NULL)
{
cout<< "Error" <<endl;
Return
}

DWORD Dwbytesinblock = GetFileSize (fp,null); File length

Create a file map kernel object with handles stored in hfilemapping
HANDLE hfilemapping = createfilemapping (FP,
Null
Page_readwrite,
0,//(DWORD) (Dwbytesinblock >> 16),
dwbytesinblock,//(DWORD) (Dwbytesinblock & 0x0000ffff),
NULL);

int dwerror = GetLastError ();

Releasing a file kernel object
CloseHandle (FP);

Offset address
__int64 qwfileoffset = 0;

Mapping file data to the address space of a process
LPVOID pbfile = (LPVOID) mapviewoffile (hfilemapping,
File_map_all_access,
(DWORD) (Qwfileoffset >> 32),
(DWORD) (Qwfileoffset & 0xFFFFFFFF),
Dwbytesinblock);

HANDLE WP = CreateFile (TEXT ("e://Paladin 5.rar"),//Here Enter the file you want to paste DST
Generic_read | Generic_write,
File_share_write,
Null
Create_always,
File_flag_sequential_scan | File_flag_write_through,
NULL);

HANDLE hFileMapping2 = createfilemapping (WP,
Null
Page_readwrite,
0,//(DWORD) (Dwbytesinblock >> 16),
dwbytesinblock,//(DWORD) (Dwbytesinblock & 0x0000ffff),
NULL);


CloseHandle (WP);

LPVOID pbFile2 = (LPVOID) mapviewoffile (HFileMapping2,
File_map_all_access,
(DWORD) (Qwfileoffset >> 32),
(DWORD) (Qwfileoffset & 0xFFFFFFFF),
Dwbytesinblock);

memcpy (Pbfile2,pbfile,dwbytesinblock);

UnmapViewOfFile (PbFile2);
UnmapViewOfFile (Pbfile);

CloseHandle (HFILEMAPPING2);
CloseHandle (hfilemapping);
DWORD timeend =:: timeGetTime ();
cout<< "CreateFileMapping and MapViewOfFile program run time is" <<timeend-timebegin<<endl;
When the test creates and opens a file map, it always gets an "invalid handle" error, and after looking at MSDN, it finds out that the function is not known, and translates the relevant explanations

HANDLE CreateFileMapping (
HANDLE hfile,//physical file handle
Lpsecurity_attributes lpattributes,//Security settings
DWORD Flprotect,//Protection settings
DWORD Dwmaximumsizehigh,//High file size
DWORD Dwmaximumsizelow,//low file size
LPCTSTR lpname//Shared memory name
);

1) Physical file handle
Any physical file handle that you can get, if you need to create a physical file-independent memory map, it's okay to set it to 0xFFFFFFFF (INVALID_HANDLE_VALUE).

If you need to associate with a physical file, make sure that your physical file is created with an access pattern that matches the "protection settings", such as a physical file read-only and a memory map that requires reading and writing. It is recommended that your physical files be created using exclusive means.

If you use Invalid_handle_value, you also need to set the size of the memory space that you want to request, regardless of whether the physical file handle parameter is valid, so that createfilemapping can create a memory space that is independent of the size of the physical file.  Even more than the actual file size, if your physical file is valid, and the size parameter is 0, then return to you is a physical file size of the same memory space address range. Return to your file map address space can be copied, integrated or named to get the initial content of 0.

2) Protection settings
is a security setting, but it is generally set to NULL, using the default security configuration. If the restrictions are required under Win2K, this is for those application processes that share the memory file mapping to the entire network and can be considered for limitations.

3 high-level file size
Brethren, I think our machines are now 32-bit, it is impossible to get more than 32-bit process can address the private 32-bit space, usually set 0 bar, I do not want to try to set it more than 0 of the situation.
4 Low File size
This can still be set, but in order for other shared users to know about the file map you are applying for, when I use it, I add a structured description to the head of the address space I get, record the size of the memory map, name, etc., so that the actual application space is larger than the input of the header information structure size, I think such a BSTR approach should be more reasonable.

5) Shared Memory name
This is my test today when the bane of the wall, because in order to the memory of mutually exclusive access, I set a mutex handle, and the name I choose and name shared memory, under the same name, because they use the common namespace caused the error, hehe.

7) The corresponding error of GetLastError when calling CreateFileMapping
Error_file_invalid If you attempt to create a 0-length file map, you should have this report
Error_invalid_handle If you find your named memory space and existing memory mappings, mutexes, semaphores, and a critical area with the same name, you're in trouble.
Error_already_exists indicates that the memory space name already exists

8) related services or the naming of the platform reservation
Terminal Services:
The name can contain "global/" or "local/" prefixes in the global or session-name space for the primary file mapping. Other parts can contain any character other than (/), and you can refer to the Kernel Object Name spaces.

Windows later or:
If Terminal Services does not have the special meaning of running "global/" and "local/" prefixes, it is ignored

Create a File View

    to map the data in a file to the virtual memory of the process, you must create a view of the file. The
    mapviewoffile and Mapviewoffileex functions use the file mapping object handle returned by CreateFileMapping to create a view of the file in the virtual address space of the process. Or a part of the file. If these functions specify a permission flag that is inconsistent with the permission flags in CreateFileMapping, a failure is performed. The
    mapviewoffile function returns a pointer to a file view. With the address pointer declared in MapViewOfFile, the program can read from the file and write data to the file. Writing data to a file view causes the file map object to change. A file that actually writes data to disk is handled by the system. Data is not immediately written to disk, and many of the file's input and output are cached to improve system performance. The program can call the Flushviewoffile function to cross this way, forcing the system to write data to the disk immediately. The
    Mapviewoffileex function works exactly the same as the MapViewOfFile function, except that it can take advantage of the lpvbase parameters of the Mapviewoffileex function. To specify the base address of the file view in the process virtual address space. If there is not enough space at the specified address, the call fails. The
    1, lpvbase parameter must be an integer multiple of the smallest unit of system memory, otherwise the call will fail. To get the smallest unit of system memory, using the GetSystemInfo function, he writes the information to the members of the SYSTEM_INFO structure.
    programs can create multiple file views from the same file mapping object. File views can be of different sizes, but they must be less than the file mapping object. The Dwoffsethigh and Dwoffsetlow parameters of the MapViewOfFile function must be integral times the smallest unit of system memory.


File mapping issues
Memory-mapped files are not simple file I/O operations, but actually use the core programming technology of Windows-memory management. Therefore, if you want to have a deeper understanding of memory-mapped files, you must have a clear understanding of the memory management mechanism of the Windows operating system, memory management related knowledge is very complex, beyond the scope of this article, no longer repeat, interested readers can refer to other related books. The following is a general method for using memory-mapped files:

First you create or open a file kernel object by using the CreateFile () function, which identifies the file on disk that will be used as a memory-mapped file. After the file image is advertised to the operating system in the physical storage with CreateFile (), only the path to the image file is specified, and the length of the image is not specified. To specify how much physical storage is required for a file map object, you also need to create a file mapping kernel object by using the CreateFileMapping () function to tell the system file size and how to access the file. After the file mapping object has been created, you must also maintain an address space area for the file data and submit the file data as physical storage mapped to the zone. The MapViewOfFile () function is responsible for mapping all or part of a file mapping object to the process address space through system management. At this point, the use and processing of the memory-mapped file is essentially the same as that of the file data that is normally loaded into memory, and when the memory-mapped file is used, the purge and use of the resource are completed through a series of operations. This section is relatively simple and can be done by UnmapViewOfFile () to undo the image of the file data from the address space of the process, and to close the file mapping object and file object created earlier by CloseHandle ().

Memory-mapped file-related functions

When using memory-mapped files, the API functions used are essentially the ones mentioned earlier, which are described below:

HANDLE CreateFile (LPCTSTR lpfilename,
DWORD dwDesiredAccess,
DWORD dwShareMode,
Lpsecurity_attributes lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE htemplatefile);

function CreateFile () is often used to create and open files even in normal file operations, when processing memory-mapped files, the function creates/opens a file kernel object, and returns its handle. When calling this function, you need to set the parameters dwDesiredAccess and dwShareMode based on whether you need to read and write the data and share the files, and the incorrect parameter settings will cause the failure of the corresponding operation.

HANDLE createfilemapping (HANDLE hfile,
Lpsecurity_attributes Lpfilemappingattributes,
DWORD Flprotect,
DWORD Dwmaximumsizehigh,
DWORD Dwmaximumsizelow,
LPCTSTR lpname);

The CreateFileMapping () function creates a file map kernel object that specifies the file handle to be mapped to the process address space through the parameter hfile (the handle is fetched by the return value of the CreateFile () function). Because the physical storage of a memory-mapped file is actually a file stored on disk, instead of allocating memory from the system's page file, the system does not actively reserve the address space for it, nor does it automatically map the file's storage space to the zone, in order for the system to determine what kind of protection properties to take on the page, Needs to be set through parameter Flprotect, protection properties page_readonly, Page_readwrite, and page_writecopy, respectively, indicate that the file mapping object is mapped to read and write file data. When using page_readonly, it is necessary to ensure that the CreateFile () is using the Generic_read parameter, and Page_readwrite requires CreateFile () to use generic_read| Generic_write parameters; As for the attribute page_writecopy, you only need to ensure that CreateFile () takes one of Generic_read and Generic_write. The parameter Dwmaximumsizehigh and Dwmaximumsizelow of the DWORD are also very important, specifying the maximum number of bytes for the file, because the two parameters are 64 bits, so the maximum supported file length is 16EB. Can almost meet the requirements of any large data processing occasions.

LPVOID MapViewOfFile (HANDLE hfilemappingobject,
DWORD dwDesiredAccess,
DWORD Dwfileoffsethigh,
DWORD Dwfileoffsetlow,
DWORD Dwnumberofbytestomap);

The MapViewOfFile () function is responsible for mapping the file data to the address space of the process, and the parameter hfilemappingobject to the file image object handle returned by CreateFileMapping (). The parameter dwdesiredaccess again specifies how the file data is accessed, and also matches the protection property set by the CreateFileMapping () function. While it seems redundant to repeatedly set protection properties, it can enable applications to more effectively control the protection properties of the data. The MapViewOfFile () function allows all or part of the mapping file to be mapped, specifying the offset address of the data file and the length of the map to be mapped. Where the file's offset address is specified by a 64-bit value consisting of a DWORD parameter Dwfileoffsethigh and Dwfileoffsetlow, and must be an integer multiple of the allocation granularity of the operating system, and the allocation granularity is fixed to 64KB for the Windows operating system. Of course, you can also dynamically obtain the allocation granularity of the current operating system by using the following code:

System_info Sinf;
GetSystemInfo (&sinf);
DWORD dwallocationgranularity = sinf.dwallocationgranularity;

The parameter dwnumberofbytestomap specifies the mapping length of the data file, and it should be noted that for the Windows 9x operating system, if MapViewOfFile () cannot find a large enough area to hold the entire file mapping object, Null is returned, but under Windows 2000, MapViewOfFile () only needs to find a sufficiently large area for the necessary views, regardless of the size of the entire file-mapped object.

After completing the processing of the file mapped to the process address space area, the release of the file data image needs to be completed through the function UnmapViewOfFile (), which is declared as follows:

BOOL unmapviewoffile (lpcvoid lpbaseaddress);

The unique parameter lpbaseaddress specifies the base address of the return range and must be set to the return value of MapViewOfFile (). After you have used the function mapviewoffile (), you must have a corresponding unmapviewoffile () call, otherwise the reserved area will not be released until the process terminates. In addition, the CreateFile () and createfilemapping () functions have previously created file kernel objects and file-mapped kernel objects that need to be released by CloseHandle () before the process terminates, otherwise there will be a problem with resource leaks.

In addition to the previous necessary API functions, some other auxiliary functions are used when using memory-mapped files. For example, when using a memory-mapped file, the system caches the file's data pages for speed, and does not immediately update the file's disk image when processing the file map view. To solve this problem, consider using the Flushviewoffile () function, which forces the system to rewrite some or all of the modified data to the disk image, ensuring that all data updates are saved to disk in a timely manner.

Shared Memory Object Method (MapViewOfFile)
Shared memory object methods Typically, a memory-mapped file supported by the paging file is used as a technique for sharing memory between user processes. However, you can use the same technology to share memory between user processes and device drivers. There are two ways of using this technique.

In the first method, the driver can create a named memory object (called a "zone object") by using openfilemapping and then calling the MapViewOfFile function to obtain a pointer to a zone or all shared memory. And one or more user applications can open the same object. You can define how a process manipulates memory by specifying protection properties to the Zone object.

In the second method, an application can create a named memory object in user mode with CreateFileMapping. The driver can open the same memory object by using Zwopensection and calling Zwmapviewofsection to get a pointer to it. This memory address is always accessed in kernel mode with an exception handler.

Because the object is always mapped in the process's user address space (less than 0x80000000, whether the object is created in kernel mode or in user mode), the address is valid only when the address is accessed in the context of the process. Each time a mapviewoffile or zwmapviewofsection is invoked on the same memory object, a different memory address is returned (even for the same process). This is not recommended (especially for low-level device drivers), as described earlier, because the address range is limited to the process of mapping objects and cannot be accessed in DPC or ISR. In addition, the API for creating memory objects in kernel mode is not documented in DDK.

However, to use this address on an improved IRQL (such as DPC or ISR), you must identify and lock the buffer page and obtain the system virtual address mmgetsystemaddressformdl (as described in the IOCTL method earlier in this article).

This approach is easier only if you want to share memory between two (or more) user processes and one (or more) device drivers. Otherwise, using IOCTL technology to share memory between user processes and device drivers is simpler and more efficient.

Memory-Mapped file technology
1. Use and basic operation
For memory sharing operations between different processes, you can map a physical file into memory and use the address space assigned to or open named shared memory to implement resource sharing access

2. Related process
1 create a new named shared memory
First use CreateFile or createfileformapping to obtain a physical file handle for mapping. The file handle is then combined with createfilemapping to get a named shared memory mapped file handle.
//createfilemapping creates a well-known or nameless file image for the specified file;
HANDLE createfilemapping (
  HANDLE hfile,              //mapping file handle
  Lpsecurity_attributes Lpfilemappingattributes,//Security descriptor Pointers
  DWORD flprotect,           //Protection of mapped objects
  DWORD dwmaximumsizehigh,  //High 32-bit maximum length of object
  DWORD dwmaximumsizelow,   //object maximum length of low 32-bit
  LPCTSTR lpname             //File memory-mapped object name
);

Attention:
Hfile: The handle of the mapping file, which must be in the same mode as the Flprotect parameter specifies, if the value of this parameter is 0xFFFFFFFF, You must specify the size of the mapping object in the Dwmaximumsizehigh and Dwmaximumsizelow parameters. The file map object is created in the operating system virtual memory page replacement file, rather than using disk files, and the size of the mapped object must be given. File map objects are shared by copy, heredity, or name.
Lpfilemappingattributes: A security descriptor pointer that determines whether the return handle can be inherited by the quilt process and, if it is null, the child process cannot inherit. In Winnt, if it is null, then the file map object gets a default security descriptor.
Flprotect: Attempting to specify protection mode for the resulting file can be set to the following values:
Page_readonly: Read-only property, and hfile corresponding file must be opened in generic_read form.
Page_readwrite: Readable writable properties, and hfile corresponding files must be opened in Generic_read and Generic_write form.
Page_writecopy: After copying the writable area, and hfile the corresponding file must be opened in Generic_read and Generic_write form.
Dwmaximumsizehigh,dwmaximumsizelow: If these two parameters are 0, the maximum length of the file map object is equal to the file length specified by hfile.
Lpname: The name of the file map object, and if the name already exists, the mapping object is processed according to the flprotect specified. If this parameter is empty, a file mapping object with no name is created. If the name of this parameter is the same as the name of the system event, the function fails, GetLastError returns Error_invalid_handle;

Return value: A function call successfully returns a handle to a file-mapped object and returns the handle of the original mapped object if the file mapping object already exists, GetLastError returns ERROR_ALREADY_EXISTS. function execution failed to return null.

2) Open named shared memory
Use the OpenFileMapping function if you need to share a named shared memory-mapped file that already exists.
OpenFileMapping Open a named File map object
HANDLE openfilemapping (
DWORD dwdesiredaccess,//access mode
BOOL bInheritHandle,//Inheritance flag
LPCTSTR lpname//File map object name pointer
);
Attention:
dwDesiredAccess: The access mode is the same as the access mode in MapViewOfFile.
bInheritHandle: The inheritance flag, whether it can be used by a new process, and if true, can be inherited by a new process handle.
return value:
A named file mapping object was successfully returned, and failed to return null.

3 Get the address space pointer
The memory-mapped file reading and writing is different from the general file read and write, is directly facing the address space you request, for this need to use MapViewOfFile to get the relevant address LPVOID type of pointer. If file writes are required, you can assign values directly to memory addresses through type conversions, such as:
memcpy (lpaddress, lpbuf, ...)
There is a natural need to prevent memory overflow.
If it is a read operation, adjust the order of the parameters.

MapViewOfFile maps a file view in the address space of the calling process
LPVoid mapviewoffile (
  HANDLE hfilemappingobject, // Created file map object handle
  DWORD dwdesiredaccess,     //Access Mode
  DWORD Dwfileoffsethigh ,    //File offset high 32-bit
  DWORD dwfileoffsetlow,     //File offset low 32-bit
  DWORD dwnumberofbytestomap //Map view size
);
Note:
Hfilemappingobject: The file map object handle returned by CreateFileMapping or openfilemapping.
dwDesiredAccess: The access mode of the mapped view, which is related to the protection mode flprotect that creates the file map object, can be set to the following values:
 file_map_write: A file view of a read-write property is created, Protected mode is Page_readwrite
 file_map_read: A file view of a read-only property is created with a protected mode of Page_readwrite or page_readonly
 file_map_ All_access: The same as the File_map_write mode
 file_map_copy: When the protection mode is page_writecopy, a view file is obtained and the page is automatically exchanged when you write the image file. And the modifications you make will not damage the original data.
Dwnumberofbytestomap: The size of the mapped file portion, or 0, to map the entire file.
Return Value:
Returns the starting address of the mapped view if it succeeds, if NULL is returned by the failure.

4) Mapviewoffileex maps a file view in the address space of the calling process and allows the calling process to specify a special memory address for the mapping view
LPVOID Mapviewoffileex (
HANDLE hfilemappingobject,//File mapping object handle
DWORD dwdesiredaccess,//access mode
DWORD Dwfileoffsethigh,//file offset high 32 bits
DWORD Dwfileoffsetlow,//file offset low 32 bit
DWORD Dwnumberofbytestomap,//Map the size of the view
LPVOID lpbaseaddress//Specify the actually memory address of the mapped view
);
Attention:
The same as the MapViewOfFile usage, but the function fails if the specified memory address space is not large enough.


5 copy memory to the mapped physical file
The Flushmapviewoffile function dumps the contents of the memory onto the physical disk
Flushviewoffile the modified content in the file map view or all of it back to the disk file
BOOL Flushviewoffile (
Lpcvoid lpbaseaddress,//modify the starting address of the content
DWORD Dwnumberofbytestoflush//Modified number of bytes
);
function execution successfully returned Non-zero.

6 Uninstall the memory-mapped file address pointer
The Unmapviewofffile function is to uninstall
UnmapViewOfFile map view of deleted files
BOOL UnmapViewOfFile (
Lpcvoid lpbaseaddress//Map view start address
);
Attention:
Lpbaseaddress: Maps The starting address of the view, which is generated by the MapViewOfFile function Mapviewoffileex.
return value:
If the call successfully returns Non-zero, the dirty pages in all the specified addresses are written to the hard disk. Call failed to return zero.

7 Close the memory-mapped file
It's too easy, CloseHandle.

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.