Use #pragma data_seg to create a new data segment and define shared data in the following format:
#pragma data_seg ("Shareddata")
HWND sharedwnd=null;//Shared data
#pragma data_seg () ----------------------------------------------------------------------------------------------------- 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 data that is not initialized. BSS segment, resulting in shared behavior between multiple processes failing.
3, what you call the right result is an illusion. If you write this in a DLL:
#pragma data_seg ("MyData")
int g_value;//Note that the global isn't initialized.
#pragma data_seg ()
A DLL provides two interface functions:
int GetValue () { return g_value; }
void SetValue (int n) { g_value = n; }
then start two processes A and b,a and b all call this DLL, if a calls the SetValue (5); b then call int m = GetValue (); Then the value of M is not necessarily 5, but an undefined value. Because the global data in the DLL is private and cannot be shared for each process that invokes it. If you initialize the G_value, the G_value will surely be placed in the MyData section. In other words, if a calls the SetValue (5); b then call int m = GetValue (); Then the value of M must be 5! This enables data communication across processes! ---------------------------------------------------------------------------------------------------- Sometimes we may want an application to be launched only once , like a single-piece mode (singleton), the implementation of the method may have a variety of, here is said to use the #pragma data_seg to implement the method is very simple and convenient. The application's entry file is preceded by the #pragma data_seg ("Flag_data") int app_count = 0; #pragma data_seg () #pragma COMMENT (linker, "/SECTION:FLAG_DATA,RWS") And then the program starts the place plus if (app_count>0)//If the count is greater than 0, exit the application. { MessageBox (NULL, "an application has been started", "Warning", MB_OK); printf ("no%d application", App_count); return FALSE; } app_count++; Windows builds a wall around the address space of a WIN32 program. Typically, the data in the address space of a program is private and is not visible to other programs. However, multiple execution entities executing Strprog indicate that strlib sharing data between all the executing entities of a program is no problem. When you add or delete a string in a Strprog window, the change is immediately reflected in the other window. Between all routines, Strlib shares two variables: a character array and an integer that records the number of valid strings that have been stored. Strlib stores these two variables in a special memory section of the share: #pragma data_seg ("shared") int itotal = 0; WCHAR szstrings [max_strings][max_length + 1] = {'/0 '}; #pragma data_seg ()
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 marking section. It is important to initialize variables specifically, otherwise the compiler will place them in normal uninitialized data segments instead of in shared. The linker must know that there is a "shared" shared data segment. In the "project settings" dialog box, select the "link" page volume label. When "strlib" is selected, the "project options" field (both in the release and debug settings), contains the following link description: /section:shared,rws
The letter RWs indicates that the segment has read, write, and shared properties. Alternatively, you can specify the link option directly with the DLL's source code, as we did in STRLIB.C: #pragma COMMENT (linker, "/SECTION:SHARED,RWS")
Shared memory segments allow Itotal variables and szstrings string arrays to be shared among all routines in Strlib. Because Max_strings equals 256 and max_length equals 63, the length of the shared memory segment is 32,772 bytes-itotal The variable requires 4 bytes, and each of the 256 pointers requires 128 bytes. |