Posted on 1998-08-07
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?
Question by:ukasan
  • 5
  • 3

Author Comment

ID: 1361713
Edited text of question

Expert Comment

ID: 1361714
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.


Author Comment

ID: 1361715
Yes, some source code would be apreciated!

Accepted Solution

pjdb earned 100 total points
ID: 1361716
    Charged:Boolean;  //Any stuff you want


procedure DoFileMapBegin;
{establish the shared memory -> begin of program}
  SMemoryMapFile='MemoryMapFile'+ #0;
  SNoFileMapping='Impossible to map the file';
  hMapFile:=CreateFileMapping($FFFFFFFF, nil, Page_ReadWrite, 0, SizeOf(boolean), SMemoryMapFile);
  if hMapFile=0 then
    raise Exception.Create(SNoFileMapping);
  SharedMemory:=MapViewOfFile(hMapFile, File_Map_Write, 0, 0, sizeof(boolean));
  if SharedMemory=nil then
    raise Exception.Create(SNoFileMapping);
end;      {DoFileMapBegin}

procedure DoFileMapEnd;
{Unmap the shared memory -> End of program}
end;      {DoFileMapEnd}

in the programs, just use SharedMemory as a pointeur to a structure.

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline


Author Comment

ID: 1361717
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


Expert Comment

ID: 1361718
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.


Author Comment

ID: 1361719
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:

Author Comment

ID: 1361720
I finally figured out what i was doing wrong and have now corrected it!! Thanks for your help!

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now