Article ID: 125677 - Last Review: November 21, 2006 - Revision: 3.2 How To Share Data Between Different Mappings of a DLLThis article was previously published under Q125677 On This PageSUMMARY
Under certain circumstances, 32-bit DLLs might have to share data with
other 32-bit DLLs loaded by a different application or with different
mappings of the same DLL. Because 32-bit DLLs are mapped into the calling
process's address space, which is private, sharing data with other DLLs
mapped into the address spaces of different applications involves creating
shared data section(s) or using memory mapped files. This article discusses
the former -- creating shared data sections by using the #pragma statement.
Typically, system-wide hooks installed in a DLL need to share some common
data among different mappings.
MORE INFORMATION
Each Win32-based application runs in its own private address space. If a 32-
bit application installs a system-wide hook with the hook callback function
in a DLL, this DLL is mapped into the address space of every application
for which the hook event occurred.
Every application that the DLL gets mapped into, gets its own set of variables (data). Often there will be a scenario where hook callback functions mapped into different application or process address spaces need to share some data variables -- such as HHOOK or a Window Handle -- among all mappings of the DLL. Because each application's address space is private, DLLs with hook callback functions mapped into one application's address spaces cannot share data (variables) with other hook callback functions mapped into a different application's address space unless a shared data SECTION exists in the DLL. Every 32-bit DLL (or EXE) is composed of a collection of sections. By convention, each section name begins with a period. (The period is not required.) These sections can have the following attributes: READ, WRITE, SHARED, and EXECUTE. DLLs that need to share data among different mappings can use the #pragma pre-processor command in the DLL source file to create a shared data section that contains the data to be shared. The following sample code shows by example how to define a named-data section (.shared) in a DLL. Sample CodeIf one application makes any changes to variables in the shared data section, all mappings of this DLL will reflect the same changes, so you need to be careful when dealing with shared data in applications or DLLs. You must also tell the linker that the variables in the section you defined are to be shared by modifying your .DEF file to include a SECTIONS section or by specifying /SECTION:.shared,RWS in your link line. Here's an example SECTIONS section: REFERENCES
For additional information, please see the following article in the
Microsoft Knowledge Base:
| Article Translations
|

Back to the top
