interprocess communication-shared memory in a dynamic-link library (address segment space using the DLL's 2~3g)

Source: Internet
Author: User
Tags semaphore

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
  1. <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >//The following ifdef block is the standard-of-the-creating macros which make exporting
  2. From a DLL simpler. All files within this DLL is compiled with the Shareddll_exports
  3. Symbol defined on the command line. This symbol should not being defined on any project
  4. That uses the this DLL. This is the other project whose source files include the This file see
  5. Shareddll_api functions as being imported from a DLL, whereas this DLL sees symbols
  6. Defined with this macro as being exported.
  7. #ifdef Shareddll_exports
  8. #define SHAREDDLL_API __declspec (dllexport)
  9. #else
  10. #define SHAREDDLL_API __declspec (dllimport)
  11. #endif
  12. This class is exported from the SharedDll.dll
  13. Class Shareddll_api Cshareddll {
  14. Public
  15. Cshareddll (void);
  16. //Todo:add your methods here.
  17. };
  18. Set string to shared DLL
  19. Shareddll_api void setvaluestring (LPCSTR str);
  20. Get string from shared DLL
  21. Shareddll_api LPSTR getvaluestring ();
  22. </span>

5.shareddll.cpp file with #pragma data_seg preprocessing instructions for setting up shared data segments.

[CPP]View PlainCopy
  1. <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" A.
  2. Sharing Data Statement start
  3. #pragma data_seg ("Shareddata")
  4. Be careful to initialize the following variables, otherwise you will not be able to implement data sharing among multiple processes
  5. Char m_strstring[256]="";
  6. End of shared data statement
  7. #pragma data_seg ()
  8. The ability to define shared data segments in a connector is readable and writable to share
  9. #pragma COMMENT (linker, "/SECTION:SHAREDDATA,RWS")
  10. Access to shared Data interface
  11. Shareddll_api LPSTR getvaluestring ()
  12. {
  13. return m_strstring;
  14. }
  15. Save interface for shared data
  16. Shareddll_api void setvaluestring (LPCSTR str)
  17. {
  18. strcpy (M_STRSTRING,STR);
  19. }
  20. ......
  21. </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
  1. <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" A.
  2. Click event for the [Submit] button
  3. void Cformadlg::onbnclickedok ()
  4. {
  5. //Get input data information from the edit box control
  6. cedit* e = (cedit*) This->getdlgitem (IDC_EDIT1);
  7. CString str;
  8. E->getwindowtext (str);
  9. //Call SharedDLL.dll's setvaluestring interface to save data to the shared DLL
  10. Setvaluestring ((lpctstr) str);
  11. }
  12. ......
  13. </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
  1. <span style="FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" A.
  2. Click events for the [Display] button
  3. void Cformbdlg::onbnclickedok ()
  4. {
  5. //Call SharedDLL.dll's getvaluestring interface to get shared data
  6. CString str = (CString) getvaluestring ();
  7. //outputs the obtained data to the edit box in the FormB form
  8. cedit* e = (cedit*) This->getdlgitem (IDC_EDIT1);
  9. E->setwindowtext (str);
  10. }
  11. </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)

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.