By default, all data segments are process independent and each process has its own copy. But sometimes the process of sharing a data segment is also necessary, for this VC + + compiler connector provides a special syntax, the following is a typical example, the establishment of a shared data section named shared, the data segment has only one variable G_hwndowner.
The process sharing area///////////////////////////////////////#pragma data_seg (" Shared ") HWND G_hwndowner = null;//call SetWindowsHookEx The main window of the thread #pragma data_seg () #pragma comment (linker,"/SECTION: Shared,rws ")//readable, writable, inter-process sharing. All processes that load this DLL share a portion of memory
Seemingly simple things, but it is often easy for us to make mistakes. Here are a few syntax questions to be aware of:
(1) The name of the data segment is "shared", so when setting this property, make sure that the segment name is exactly the same as "shared" and is case sensitive. Once the two are different, the connector warns of errors.
>link:warning LNK4039: The section "shar2ed" specified with the/SECTION option does not exist. Note that it is a warning error, so the DLL file will continue to compile the connection successfully, except that the shared data segment is not set to share segment.
(2) The variables in the shared segment must be initialized, or the connector will error, and it cannot be set as a shared segment.
1>link:warning LNK4039: The section "shared" specified with the/SECTION option does not exist. However, the DLL file continues to be generated.
(3) The last row of RWS can not have a space before, or the compiler error.
1>main.obj:fatal error LNK1276: Found invalid instruction "RWS"; not beginning with "/". Then stop compiling the connection.
The three kinds of errors, the most serious is (1) and (2), because although the shared segment has not been successfully set, but still compiled successfully, a little attention, it will be very dangerous. for (3) It is not possible to compile successfully, so as long as the understanding of syntax modification can be, there is no potential danger.
??
Syntax issues to be noted in building a process shared data segment in a DLL