WinCE - Share a data segment in a DLL
Introduction
A DLL can be loaded by more than one process. For every process the DLL's code segment is shared but each process gets its own data segment by default. Therefore if one process changes the value of the DLL's global variable, the other process can not get the modified value. For sharing some global variables of the DLL among several processes, we can use the shared data segment.
Example Code
// Global and static member variables that are not shared defined here.
...
// Begin the shared data segment
#pragma data_seg("SHARED")
// Define simple variables, the variable must be initialized here, otherwise it will not be shared.
int gSharedTest = 0;
// Do not define classes that require 'deep' copy constructors.
#pragma data_seg()
// End the shared data segment and default back to the normal data segment behavior.
// Tells the linker to generate the shared data segment.
#pragma comment(linker, "/section:SHARED,RWS")
Remarks
1) The upper "SHARED" is the name of the segment, you can use another name. It should be appeared both in the data_seg and comment part.
2) The address of the shared variable must lie in this data segment, so variables allocated from heap can not be shared.
3) The variable must be initialized otherwise it won't be shared.
1 Comments:
看不懂,不会弄点我能看懂呢吗?真是的,什么意思嘛!
Post a Comment
Subscribe to Post Comments [Atom]
<< Home