Windows inter-process communication (shared memory)

Source: Internet
Author: User

A process is usually defined as an instance of a running program. It consists of two parts:

One is the kernel object used by the operating system to manage processes. The kernel object is also used by the system to store process statistics.

The other is the address space, which contains the code and data of all executable modules or DLL modules. It also contains dynamically allocated space. Such as thread stack and heap allocation space. Each process is assigned its own virtual address space. When a thread in the process is running, the thread can access the memory of the process that only belongs to it. Memory of other processes is hidden and cannot be accessed by running threads.

In order to communicate between two processes, we can achieve this through the following methods:

2. Use shared memory (shared memory)

A. Set a shared memory area

Handle createfilemapping (handle, lpsecurity_attributes, DWORD, lpcstr)

Generate a file-mapping core object

Lpvoid mapviewoffile (

Handle hfilemappingobject,

DWORD dwdesiredacess,

DWORD dwfileoffsethigh,

DWORD dwfileoffsetlow,

DWORD dwnumberofbytestomap

);

Get the shared memory pointer

B. Find out the shared memory

It is determined that this memory should be presented in the form of peer to peer

Each process must have the same capability to generate shared memory and initialize it. Each process

Createfilemapping () should be called, and then getlasterror () should be called. If

The error code is error_already_exists, so the process can assume that the shared memory region has been opened and initialized by other processes. Otherwise, the process can reasonably think that it is ranked first, then, initialize the shared memory.

Still use the Client/Server Architecture

Only the server process should generate and initialize the shared memory. All processes should use

Handle openfilemapping (DWORD dwdesiredaccess,

Bool binherithandle,

Lptstr lpname );

Call mapviewoffile () to obtain the shared memory pointer.

C. synchronous processing (mutex)

D. Cleaning up bool unmapviewoffile (lpcvoid lpbaseaddress );

Closehandle ()

I think this is the easiest way to create a memory share:

Code Implementation

Create a shared file in a process

Handle lhsharememory = createfilemapping (invalid_handle_value, null, page_readwrite, 0,1024, "penghao"); If (lhsharememory! = NULL) {// afxmessagebox ("Shared data is successfully opened! ");} Char * strbuffer; strbuffer = (char *) mapviewoffile (lhsharememory, file_map_write, 1024,); // obtain the file address strcpy (strbuffer," Update "); // write data to the file

Open shared files and access data in another process

ShellExecute (null, "open", "processmfc.exe", sw_show); // load the Process Handle handlefile; handlefile = openfilemapping (file_map_all_access, false, "penghao"); // open the shared file if (handlefile! = NULL) {afxmessagebox ("Shared data opened successfully");} Char * STR = NULL; STR = (char *) mapviewoffile (handlefile, file_map_all_access, 1024 ); // obtain the shared file address and obtain the data if (strcmp (STR, "Update") = 0) {afxmessagebox (STR);} unmapviewoffile (STR ); closehandle (handlefile); Return true; // return true unless you set the focus to a control}

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.