We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now



ukasan asked
Medium Priority
Last Modified: 2010-04-04
I have a 32bit dll that takes a key combination from an executable and uses it as a hot key! When another program is activated the dll 'forgets' the key combination, unless I re-activate the application that initally called the dll. I understand that each instance of the dll has it's own memory area under 32bit windows. Is there any way that the dll can have global variables like the ones under 16bit. ie dll has only one memory area? If so how can I impliment this in Delphi 3?
Watch Question


Edited text of question

use file mapping (MapViewOfFile) to allocate a memory area acessible from many programs (it can either be the same DLL or many DLL/EXE). Is you want some code, just say it, I will search for them in my projects.



Yes, some source code would be apreciated!
Unlock this solution and get a sample of our free trial.
(No credit card required)


I tried the code you send. but I still have the problem of 'remembering the Pointer SharedMemory! Let me explain what i am trying to do!
 I have an exe which calls my dll, and gives it the virtual key combination (The hotkey), the dll then sets up a system wide hook, when a key is pressed from any application it calls my dll, but my dll cannot 'remember' the key combination to lookout for! Whilst i was browsing through the windows 'Win32 Developers Reference' I came across the following, but I do not know how to impiment this in delphi, I wonder if you could help:-

How to Share Data Between Different Mappings of a DLL

PSS ID Number: Q125677

Authored 01-Feb-1995                  Last modified 25-May-1995

The information in this article applies to:

 - Microsoft Win32 Software Development Kit (SDK)


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 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.


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 occured.

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. Each

section name begins with a period. The section of interest in this article
is the data section. These sections can have one of 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 (.sdata) in a DLL.

Sample Code

#pragma data_seg(".sdata")
int iSharedVar = 0;
#pragma data_seg()

The first line directs the compiler to place all the data declared in this
section into the .sdata data segment. Therefore, the iSharedVar variable is
stored in the MYSEC 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 carefule 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:.sdata, RWS in your link line. Here's an example
SECTIONS section:



In the case of a typical hook DLL, the HHOOK, HINSTDLL, and other variables
can go into the shared data section.

Additional reference words: 4.00 95
KBCategory: kbprg
KBSubcategory: BseMisc

I'm very supri the code did not work, i'm using it (i've copy and paste) in a program using the SetWindowHookEx function to remap the keyboard. The EXE load the configuration to a shared array in the memory and DLL use it to know which key code to send back.
Did you copy and paste the code in the EXE and the DLL?
did you call the DoFileMapBegin and the loading and DOFileMapEnd at the unloading of the EXE and of the DLL?
If you still have trouble, i can mail you an example.



I didn't realise that you had to put it in the exe as well, i tried this but i still get an access violation error, and causes windows 95 to crash! Could you mail me an example so i can figure out what i am doing wrong!
my Email is: UKASAN@Hotmail.com


I finally figured out what i was doing wrong and have now corrected it!! Thanks for your help!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.