The information in this article applies to:
SUMMARYUnder 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.
Sample Code
The first line directs the compiler to place all the data declared in this
section into the .shared data segment. Therefore, the iSharedVar variable
is stored in the .shared segment. By default, data is not shared. Note that
you must initialize all data in the named section. The data_seg pragma
applies only to initialized data. The third line, #pragma data_seg(),
resets allocation to the default data section.
If 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:
Alternatively, some compilers allow you to set the linker switch in your
code so that if your file is ever copied to another project, the linker
switch goes with it. To do this, include the following line in your code
preferably near the #pragma data_seg(".shared") line:
Be careful not to include any extraneous spaces inside the quotation marks
because this may cause the linker to misinterpret the directive. In the
case of a typical hook DLL, the HHOOK, HINSTDLL, and other variables can go
into the shared data section.
REFERENCESFor additional information, please see the following article in the Microsoft Knowledge Base: Q100634 HOWTO: Specify Shared and Nonshared Data in a DLL Additional query words:
Keywords : kbDLL kbIPC kbKernBase kbWinOS2000 kbDSupport kbGrpKernBase |
Last Reviewed: January 5, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |