# Pragma data_seg ("flag_data")
int count = 0;
# Pragma data_seg ()
# Pragma comment (linker, "/ SECTION: flag_data, RWS")
This method can only be used when there is no def file, if the exported file by def, then set the necessary settings in the document can not be def in the code set.
flag_data READ WRITE SHARED
In the main file, use # pragma data_seg establish a
A new data segment and define the shared data, the specific format:
# Pragma data_seg ("shareddata") / / name can be
/ / Define their own, but must be consistent with the following.
HWND sharedwnd = NULL; / / shared data
# Pragma data_seg ()
Only the definition of a data segment can not achieve the purpose of sharing data, but also tells the compiler the properties of the paragraph, there are two ways to achieve that purpose (the effect is the same), a method is. DEF file, add the following statement: SETCTIONS shareddata READ WRITE SHARED Another method is to set the link option in the project (Project Setting -> Link) add the following statement: / SECTION: shareddata, rws
The first point: What is the shared data segment? Why use shared data segment? ? It is used for? ?
In the Win16 environment, DLL's global data for each of the process of loading it is the same; in Win32 environment, the situation has changed, DLL function in the code to create any objects (including variables) owned by calling thread or process it all. When the process of loading the DLL, the operating system automatically maps the DLL into the process address private space, that is, the process's virtual address space, but also copy the DLL's global data a copy to the process space. Means that each process has the same DLL's global data, they have the same name, but its value is not necessarily the same, and is non-interference in the.
Therefore, in the Win32 environment in order to share data across multiple processes, it must make the necessary settings. Dll in all access the same shared memory between processes is through memory-mapped file technology to achieve. Also need to share data in these isolated, placed in a separate paragraph in the data, and to the paragraph property to share. The initial value must be assigned to these variables, or the compiler will not assign initial values of variables in a called uninitialized data section.
# Pragma data_seg directive sets the shared data segment. For example:
# Pragma data_seg ("SharedDataName") HHOOK hHook = NULL; / / must be initialized while the definition of !!!!# pragma data_seg ()
In the # pragma data_seg ("SharedDataName") and # pragma data_seg () among all the variables will be access to all processes of the Dll to see and share. Coupled with a directive # pragma comment (linker, "/ section:. SharedDataName, rws"), [Note: The data section name is case sensitive] data section then the data can be shared among all instances of DLL. All of these data operations for the same instance instead of the address space of each process has a copy.
When the process is called implicit or explicit function of a dynamic library, the system should be mapped to the dynamic libraries to this process's virtual address space (hereinafter referred to as "address space"). This makes the DLL as part of the process to run as this process, the use of the process stack. (The technology known as code Injection technology, is widely used in the virus, hacker fields! Ha ha ^ _ ^)
The second point: to share data in a specific period of time using a number of issues need to pay attention!
Win32 DLLs are mapped into the address space of the calling process. By default, each process using a DLL has its own instance of all the DLLs global and static variables. (Note: Even if the global variables and static variables are not shared! ) If your DLL needs to share data with other instances of it loaded by other applications, you can use either of the following approaches:
· Create named data sections using the data_seg pragma.
· Use memory mapped files. See the Win32 documentation about memory mapped files.
Here is an example of using the data_seg pragma:
# Pragma data_seg (". Myseg")
int i = 0;
char a  = "hello world";
# Pragma data_seg ()
data_seg can be used to create a new named section (. myseg in this example). The most typical usage is to call the data segment. shared for clarity. You then must specify the correct sharing attributes for this new named data section in your. def file or with the linker option / SECTION:. MYSEC, RWS. (This parameter can either use the compiler pragma directive to specify that the IDE can also be specified in the VC!)
There are restrictions to consider before using a shared data segment:
· Any variables in a shared data segment must be statically initialized. In the above example, i is initialized to 0 and a is 32 characters initialized to hello world.
· All shared variables are placed in the compiled DLL in the specified data segment. Very large arrays can result in very large DLLs. This is true of all initialized global variables.
· Never store process-specific information in a shared data segment. Most Win32 data structures or values (such as HANDLEs) are really valid only within the context of a single process.
· Each process gets its own address space. It is very important that pointers are never stored in a variable contained in a shared data segment. A pointer might be perfectly valid in one application but not in another.
· It is possible that the DLL itself could get loaded at a different address in the virtual address spaces of each process. It is not safe to have pointers to functions in the DLL or to other shared variables.