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}