DLL Shared data segment

Source: Internet
Author: User

Variable definitions, different instance shared by this variable #pragma data_seg ("shared") static Hhook  hhk=null;//mouse hook handle static hinstance hinst=null; The instance handle of this DLL (Hook.dll) #pragma data_seg () #pragma comment (linker, "/SECTION:SHARED,RWS")///above the variable share Oh!


1. #pragma data_seg () is generally used in DLLs. In other words, a shared, named data segment is defined in a DLL.
Most crucially, the global variables in this data segment can be shared by multiple processes. Otherwise, the global variables in the DLL cannot be shared between multiple processes.
2. The shared data must be initialized, otherwise the Microsoft compiler will put the uninitialized data into the normal uninitialized data segment instead of the shared one, causing the sharing behavior between multiple processes to fail.


The first #pragma narrative establishes the data segment, which is named Gkfx here. You can name the paragraph any one you like. All initialized variables after the #pragma narrative here are placed in the shared data segment.
The second #pragma describes the end of the data segment.
The linker must know that there is a shared data segment. You can specify the link options directly with the DLL source code, such as:
#pragma COMMENT (linker, "/SECTION:SHARED,RWS")//The letter RWS indicates that the segment has read, write, and shared properties


Introduced:

Http://baike.baidu.com/link?url=PK_ Xehtkaddtat8dsywxhoxiberkw9dbl5a0xpecmwbuunjq57zqzt95-i5umqj3skl30o4uea5cjfohadc6aq
In the WIN16 environment, The global data for the DLL is the same for each process that loads it, and in the WIN32 environment, the situation changes, and any object (including variables) created by the code in the DLL function is owned by the thread or process that called it. When a process loads a DLL, the operating system automatically maps the DLL address to the process's private space, the virtual address space of the process, and copies a copy of the DLL's global data to that process space. That is, each process owns the same DLL's global data, their names are the same, but their values are not necessarily the same, and are not interfering with each other.
Therefore, in a Win32 environment, if you want to share data across multiple processes, you must make the necessary settings. The shared memory between the processes accessing the same DLL is implemented through the memory map file technology. You can also separate the data that needs to be shared, place it in a separate data segment, and set the segment's properties to be shared. These variables must be assigned an initial value, or the compiler will place a variable with no initial values in a data segment called uninitialized.
#pragma data_seg preprocessing directives are used to set shared data segments, for example:
#pragma data_seg ("Shareddataname")
Hhook Hhook=null;
#pragma data_seg ()
#pragma comment (linker, "/section:. Shareddataname,rws ")
Description:

All variables between the #pragma data_seg ("shareddataname") and #pragma data_seg () will be seen and shared by all the processes accessing the DLL.
#pragma COMMENT (linker, "/section:. Shareddataname,rws ") sets the data in this data section to be shared between instances of all DLLs. All operations on this data are for the same instance, not one in each process's address space.
When a process implicitly or explicitly invokes a function in a dynamic library, the system maps the dynamic library to the virtual address space of the process (hereinafter referred to as the "address space"). This makes the DLL a part of the process, executing as the process, using the stack of the process.


Here's a practical application that uses shared data to count the number of times an application is started and handle it accordingly:

At the entrance of the application://Control application can only start once #pragma data_seg ("flag_data") int count=0; #pragma data_seg () #pragma comment (linker, "/ Section:flag_data,rws ") if (Count > 1) {MessageBox (" an application has been started "," Warning ", MB_OK); return flase;} count++;


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

DLL Shared data segment

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.