Windows process Communication--shared memory

Source: Internet
Author: User

The way to enjoy memory is to map a physical memory to the respective virtual address space of the different processes so that each process can read the same data to enable process communication. It is the most efficient method of data exchange because it is used for communication through memory operations.

The presence of Windows in a share is implemented using filemapping, which is achieved mainly through the following steps, depending on the implementation method:

1. Call CreateFileMapping to create a memory file mapping object;

HANDLE createfilemapping (  HANDLE hfile,              //HANDLE to file to map  lpsecurity_attributes Lpfilemappingattributes,                             //optional security attributes  DWORD flprotect,           //protection for mapping object  DWORD Dwmaximumsizehigh,   //High-order-bits of object size  DWORD Dwmaximumsizelow,    //Low-order 32 Bits of object size  LPCTSTR lpname             //Name of file-mapping object);

This API function creates a kernel object for a memory-mapped file that maps files to memory. Like virtual memory, a memory-mapped file can be used to preserve an area of an address space and commit the physical memory
to the region. The difference between them is that the physical memory comes from a file that is already on disk, not the system's page file.

hfile lpfilemappingattributes: The parameter is a pointer to the SECURITY_ATTRIBUTES structure of the file-mapped kernel object, usually passing a value of N U l l;Flprotect : Security settings for memory-mapped files (page_readonly open mappings as read-only, page_readwrite open mappings in a readable, writable way, page_writecopy for write operations)Dwmaximumsizehigh : The maximum length of the file map is 32 bits high. dwmaximumsizelow: The maximum length of the file map is 32 bits lower. If both this parameter and the Dwmaximumsizehigh are zero, the actual length of the disk file is used. lpname: Specifies the name of the file mapping object, which can be used by other processes to invoke openfilemapping to open the Filemapping object.
If the creation succeeds, returns a handle to the created memory-mapped file, and returns its handle if it already exists, but the error code returned by the call to GetLastError () is: 183 (error_already_exists), or null if the creation fails;

2. Call MapViewOfFile to map to the virtual address of the current process;

If the call to CreateFileMapping succeeds, the MapViewOfFile function is called to map the memory-mapped file to the virtual address of the process;

LPVOID mapviewoffile (  HANDLE hfilemappingobject,  //File-mapping object to map into                               /address space  DWORD dwdesiredaccess,      //access mode  DWORD Dwfileoffsethigh,     //high-order bits of file offset  DWORD Dwfileoffsetlow,      //Low-order-bits of file offset  DWORD dwnumberofbytestomap  //number of bytes to map);
Hfilemappingobject:createfilemapping () returns a handle to the file image object.
dwDesiredAccess: The way to access the file data of the mapped object, and also to match the protection properties set by the CreateFileMapping () function.
Dwfileoffsethigh: Represents the high 32 bits of the file map start offset.
Dwfileoffsetlow: The low 32 bits representing the start offset of the file map.
Dwnumberofbytestomap: The number of bytes to map in the file. The 0 representation maps the entire file-map object.

3. Open the corresponding memory-mapped object in the receive process

In the data receiving process, first call the openfilemapping () function to open a named file map kernel object, get the corresponding file map kernel object handle hfilemapping; If open successfully, call MapViewOfFile () A view of the function map object that maps the file map kernel object hfilemapping to the current application's process address for read operations. (Of course, if you use CreateFileMapping, you can also get the corresponding handle)

HANDLE openfilemapping (  DWORD dwdesiredaccess,  //access mode  BOOL binherithandle,    //Inherit flag  LPCTSTR lpname          //Pointer to name of file-mapping object);
dwDesiredAccess: dwdesiredaccess parameters of the same mapviewoffile function
bInheritHandle: This parameter is true if the handle returned by this function can be inherited by a new process initiated by the current process.
Lpname: Specifies the name of the file map object to open.

4. Read and write memory-mapped files

Once the MapViewOfFile call succeeds, it can read and write memory like the memory area of the address space of this process.

Read operation:

Open success, a view of the mapped object, a pointer to the shared memory, showing the data inside
pbuffer = MapViewOfFile (hmap, file_map_all_access, 0, 0, 0);
if (pbuffer)
{
cout << ((Cthostftdcdepthmarketdatafield *) pbuffer)->lastprice << Endl;
cout << "read shared memory data:" << ((Cthostftdcdepthmarketdatafield *) pbuffer)->lastprice << Endl;
}
Else
{
cout << "Failed to open shared data! "<< Endl;
}

Write operation:

A view of the mapped object that gets a pointer to the shared memory, sets the data inside
pbuffer = MapViewOfFile (hmap, file_map_all_access, 0, 0, 0);
if (pbuffer)
{
memcpy (cthostftdcdepthmarketdatafield*) pbuffer, Depthmarketdata, sizeof (Cthostftdcdepthmarketdatafield));
cout << "Write Shared memory data:" << ((Cthostftdcdepthmarketdatafield *) pbuffer)->lastprice << Endl;
}
Else
{
cout << "Failed to open shared data! "<< Endl;
}

5. Clean up Kernel objects

After use, the mapping of the address space of the process is canceled and the memory-mapped object is freed.

    Cancels the mapping of the address space of this process;       UnmapViewOfFile (PLOCALMEM);            Plocalmem=null;       Close file map kernel file      closehandle (hfilemapping);

Windows process Communication--shared memory

Related Article

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.