Solved

dll's

Posted on 1998-08-07
8
362 Views
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?
0
Comment
Question by:ukasan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:ukasan
ID: 1361713
Edited text of question
0
 
LVL 1

Expert Comment

by:pjdb
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.

JDB
0
 

Author Comment

by:ukasan
ID: 1361715
Yes, some source code would be apreciated!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
pjdb earned 100 total points
ID: 1361716
Type
  PMySharedMemory=^TMySharedMemory;
  TMySharedMemory=Record
    Charged:Boolean;  //Any stuff you want
  End;

SharedMemory:PMySharedMemory;

procedure DoFileMapBegin;
{establish the shared memory -> begin of program}
const
  SMemoryMapFile='MemoryMapFile'+ #0;
  SNoFileMapping='Impossible to map the file';
var
  hMapFile:THandle;
begin
  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
  begin
    CloseHandle(hMapFile);
    raise Exception.Create(SNoFileMapping);
  end;
  CloseHandle(hMapFile);
end;      {DoFileMapBegin}

procedure DoFileMapEnd;
{Unmap the shared memory -> End of program}
begin
  UnMapViewOfFile(SharedMemory);
end;      {DoFileMapEnd}

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

JDB
0
 

Author Comment

by:ukasan
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)


SUMMARY

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.


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

   SECTIONS

   .sdata   READ WRITE SHARED

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


0
 
LVL 1

Expert Comment

by:pjdb
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.

JDB
0
 

Author Comment

by:ukasan
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: UKASAN@Hotmail.com
0
 

Author Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

623 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