Objective
A process is a program that is loaded into memory and ready to execute, each with a private virtual address space, consisting of code, data, and other resources. The process of a 32-bit system assigns a virtual address space of 4G. The memory address range is 0x00000000~0xffffffff. This memory address space is independent of each process, that is, it is not possible to access the address space of other processes in one process.
For example, process A's memory holds a data, assuming the address of this data is 0x33333333. This is the time, even if the memory address of process B is 0x33333333 data is read out. This data is not the same as the address data in process a. Even the 0x33333333 address of process B has no data at all.
If you want to implement data sharing across multiple processes, this involves inter-process communication issues. Of course, there are a number of ways that interprocess communication can be implemented. For example, dynamic link library, Dynamic data exchange, file mapping, pipeline, clipboard and so on. This article focuses on how to implement interprocess communication using dynamic link libraries.
Implementation method
A dynamic-link library DLL is a file that contains a series of functions and data that the application can call its functions.
In general, when an application calls a function inside a DLL, the operating system maps the file image of the DLL to the address space of the process, at which point the DLL is just some code and data attached to the address process space for the threads in the process, and when multiple applications load the same DLL , the DLL has only one in memory. At this point, if one of the processes calls the DLL's function, it gets the arguments passed to him in the thread stack of the current process and uses the line Cheng to store the variables he needs. Any object created by a DLL function is either a calling thread or a calling process owned, and the DLL does not have any objects, that is, if a function in the DLL calls VirtualAlloc, the system will reserve the address from the calling process's address space. In this scenario, the data sharing of the dynamic-link library cannot be implemented. If you want to achieve data sharing, let's take a look at the following process virtual space composition diagram.
+---------------------------------+ 4 GB
| |
| Kernel Space |
| |
| |
+---------------------------------+ 3 GB
| |
| User Sharing Space |
| |
| |
+---------------------------------+ 2 GB
| |
| User Private Space |
| |
| |
+---------------------------------+ 0 GB
As you can see from this diagram, the call to the DLL above is 0GB-2GB virtual space. If you want to realize sharing, you can use 2GB-3GB space. In other words, save the data in this space. Here we need to use #pragma data_seg () to define a shared, named Data segment in the DLL. We can demonstrate this by following this example.
Demo Example
Start by creating a shared DLL project, which is used to save shared data.
Implementation steps
1. Create a new Win32 Project, named SharedDLL
2. Program Type selection DLL
3. Engineering structure
4.shareddll.h file, mainly stating that two functions are used to save shared data and to obtain shared data.
[CPP]View PlainCopy
- <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >//The following ifdef block is the standard-of-the-creating macros which make exporting
- From a DLL simpler. All files within this DLL is compiled with the Shareddll_exports
- Symbol defined on the command line. This symbol should not being defined on any project
- That uses the this DLL. This is the other project whose source files include the This file see
- Shareddll_api functions as being imported from a DLL, whereas this DLL sees symbols
- Defined with this macro as being exported.
- #ifdef Shareddll_exports
- #define SHAREDDLL_API __declspec (dllexport)
- #else
- #define SHAREDDLL_API __declspec (dllimport)
- #endif
- This class is exported from the SharedDll.dll
- Class Shareddll_api Cshareddll {
- Public
- Cshareddll (void);
- //Todo:add your methods here.
- };
- Set string to shared DLL
- Shareddll_api void setvaluestring (LPCSTR str);
- Get string from shared DLL
- Shareddll_api LPSTR getvaluestring ();
- </span>
5.shareddll.cpp file with #pragma data_seg preprocessing instructions for setting up shared data segments.
[CPP]View PlainCopy
- <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" A.
- Sharing Data Statement start
- #pragma data_seg ("Shareddata")
- Be careful to initialize the following variables, otherwise you will not be able to implement data sharing among multiple processes
- Char m_strstring[256]="";
- End of shared data statement
- #pragma data_seg ()
- The ability to define shared data segments in a connector is readable and writable to share
- #pragma COMMENT (linker, "/SECTION:SHAREDDATA,RWS")
- Access to shared Data interface
- Shareddll_api LPSTR getvaluestring ()
- {
- return m_strstring;
- }
- Save interface for shared data
- Shareddll_api void setvaluestring (LPCSTR str)
- {
- strcpy (M_STRSTRING,STR);
- }
- ......
- </span>
So far, the project to share the DLL is done. Next, do two MFC applications to save data to a shared DLL, or to get data from a shared DLL.
The first program: Create a new MCF dialog program, the screen is as follows. The edit box is used to enter data information, and the [Submit] button is used to submit data to the shared DLL. Of course the project must be imported into the SharedDLL Lib package.
The implementation code is as follows:
[CPP]View PlainCopy
- <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" A.
- Click event for the [Submit] button
- void Cformadlg::onbnclickedok ()
- {
- //Get input data information from the edit box control
- cedit* e = (cedit*) This->getdlgitem (IDC_EDIT1);
- CString str;
- E->getwindowtext (str);
- //Call SharedDLL.dll's setvaluestring interface to save data to the shared DLL
- Setvaluestring ((lpctstr) str);
- }
- ......
- </span>
Second program: As above, create a new MCF dialog program, the screen is as follows. The edit box is used to display data information, and the [Display] button is used to obtain the data stored in the shared DLL. This project also needs to import the SharedDLL lib package.
The code is as follows:
[CPP]View PlainCopy
- <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" A.
- Click events for the [Display] button
- void Cformbdlg::onbnclickedok ()
- {
- //Call SharedDLL.dll's getvaluestring interface to get shared data
- CString str = (CString) getvaluestring ();
- //outputs the obtained data to the edit box in the FormB form
- cedit* e = (cedit*) This->getdlgitem (IDC_EDIT1);
- E->setwindowtext (str);
- }
- </span>
So all the projects are finished, and then look at the effect of the operation.
First, we copy the SharedDll.dll and FormA.exe and FormB.exe to the same directory, allowing them to access each other.
Then run FormA.exe and FormB.exe two programs.
Enter the data information into the edit box in the Forma window and press the Submit button.
Then click the Display button in the FORMB window
As can be seen from the above example, the communication between process forma and process FORMB can be achieved through the dynamic link library.
However, there are a few points to note if you use the dynamic link library for interprocess communication.
1. interprocess communication for a dynamic-link library can be applied to local interprocess communication and not to communication across the network.
2. interprocess communication in a dynamic-link library is an issue where real-time notifications cannot be implemented. If you want to implement real-time notifications at the same time, you can work with some synchronization methods including: Event, Mutex, Semaphore semaphore, critical area resource, etc.
3. To use interprocess communication with a dynamic link library, both processes must be required to import the dynamic link library.
Concrete how to use, whether can use also need concrete situation concrete treatment.
http://blog.csdn.net/mickeyty/article/details/51721860
interprocess communication-shared memory in a dynamic-link library (address segment space using the DLL's 2~3g)