Notes for using shared DLL, especially the C ++ class

Source: Internet
Author: User

How can I share data in my own DLL with an application or other DLL? Win32 DLL maps to the address space of the calling process. By default, each DLL-used process has its own global and static DLL variables. If the DLL needs to share data with other instances loaded by other applications, you can use one of the following methods: Use data_seg to create a named data section. Use a memory ing file. See the Win32 documentation on memory ing files. The following is an example of using data_seg hybrid injection: copy the Code # pragma data_seg (". myseg ") int I = 0; char a [32] n =" Hello World "; # pragma data_seg () data_seg can be used to create a new naming section (in this example, it is. myseg ). For clarity, the most typical use is to call the data segment. shared. Then, you must specify the correct sharing attribute for the new named data section in the. Def file or using the linker option/section:. mysec. RWS. Before using shared data segments, consider the following restrictions: all variables in the shared data segment must be initialized statically. In the preceding example, I is initialized to 0, while a is a 32 character initialized to "Hello World. All shared variables are placed in the specified data segment of the compiled DLL. A large array can generate a large DLL. This applies to all initialized global variables. Never store process-specific information in shared data segments. Most Win32 Data structures or values (such as handle) are valid only in the context of a single process. Each process obtains its own address space. It is important not to store pointers in the variables contained in the shared data segment. Pointers may be completely valid in an application, but they are not valid in another application. The DLL itself may be loaded to different addresses in the virtual address space of each process. It is insecure to have pointers to functions in the DLL or to other shared variables. Note that the last three points apply to memory ing files and shared data segments. The memory ing file is better than the shared data section because the starting position of the memory ing file is known. Developers can perform pointer-like behavior by using the offset from the starting position of the shared memory node from all data in the shared memory. To make this operation quick and easy, we strongly recommend that you use the _ Based pointer. But remember: In each process, the Base (or the starting position of the memory ing file) may be different. Therefore, the base variable storing the _ Based pointer cannot be in the shared memory itself. These restrictions have important meanings for C ++ classes. Classes with virtual functions always contain function pointers. Classes with virtual functions should never be stored in shared data segments or in Memory ing files. This is especially important for MFC classes or classes inherited from MFC. Static data members are implemented in the form of global variables. This means that each process has its own copy of its own static data member. Classes with static data members should not be shared. For C ++ classes, the initialization requirements for shared data segments cause a specific problem. If the shared data segment contains content similar to ctest counter (0);, the counter object is initialized in the process when each process loads the DLL, therefore, the data of the object may be cleared every time. This is very different from the internal data type (initialized by the linker during DLL creation. Due to these limitations, Microsoft does not recommend sharing C ++ objects between processes. Generally, if you want to use C ++ to share data between processes, write a class that uses a memory ing file internally to share data, but do not share the class instance itself. You may need to be especially careful when developing such a category, but it enables application developers to have full control over the side effects of data sharing. For more information about creating the named data section, see in http://support.microsoft.com/default.aspx? Ln = ZH-CN: "How to share data between different Mappings of a DLL" (how to share data between different Mappings of DLL) (q125677 ). "Specifying shared and nonshared data in a DLL" (specify the shared data and non-shared data in the DLL) (q100634 ). "Sharing all data in a DLL" (share all data in the DLL) (q109619 ). "Memory in shared code sections is not shared between SS Terminal Server sessions" (the memory in the shared code section is not shared between Terminal Server sessions) (q251045)

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.