Solved

Memory Mapping

Posted on 1997-03-13
1
492 Views
Last Modified: 2010-04-04
This is a Delphi 2 question:

I'm going to be using memory mapping to ensure that a 'middleman' DLL I am writing will allow access to the same data for all processes accessing it.

Can I set up a memory map that simply contains pointers to the addresses of AllocMems that the DLL wil create. If the DLL uses an AllocMem to create 512KB of data storage and I then store the address of this memory in the memory map, can I then access that memory through the memory map via any of the processes that are using the DLL?

I ask because any process can request memory from the DLL and I want any other process to have access to it. So my thought was to still allocate memory the old way, via AllocMem, but store the addresses of these allocations in the shared memory map to allow all processes access to it.

Background: in D2, a DLL creates an an instance of all it global variables and such for each process connected ot it...if one process asks the DLL to allocate memory, that allocation is totally invisible to all other processes using the DLL. But if we use memory-mapping, we can share data among all the processes. Using memory-mapping for all my data sharing would not work because my allocations are very dynamic...therefore a method of storing the addresses of the allocations would work best.

Thanks.
0
Comment
Question by:mheacock
1 Comment
 
LVL 3

Accepted Solution

by:
sperling earned 120 total points
ID: 1334855
No, you cannot store the return address from AllocMem. This address will only be valid in the context of the process that caused the DLL to allocate memory.

I have done something very similar to what you're about to. The methods I used was as follows.


First, understand CreateFileMapping and MapViewOfFile if you haven't already.


On Initialization, and in DLLProc on process attachment, I create/connect to a named shared memory object using CreateFileMapping($FFFFFFFF ... Let's call it MEMTABLE


The DLL exports
  ShareAlloc
  ShareDealloc
  ShareLock
  ShareUnlock


ShareAlloc allocates an amount of memory as follows:
  If not MEMTBLxxx exists, create MEMTBL001
  If all MEMTBLxxx are full, create next MEMTBLxxx
  Add an entry to MEMTABLE containig the xxx number, an unique handle, starting and ending offsets within MEMTABLE
  Return the handle

ShareLock looks up a passed handle, and returns a pointer to it in addition to increasing a usage count in MEMTABLE

ShareUnlock takes a pointer and reduces usage count for the associated MEMTABLE entry

ShareDealloc fails if the usage count is 1 or greater. If not, the MEMTABLE entry for the passed handle is zeroed, and if the MEMTBLxxx is empty, it is freed.
 
Using this scheme, any app can pass the handle using e.g. SendMessage to another app, which then can lock and read the memory as if it was allocated in the process. Note however that the actual returned pointer for any handle may and will differ from process to process.


MEMTABLE entry:
  BlockNumber
  Offset
  Size
  Usage
  Handle

all INTEGERs

The initial size of the MEMTABLE determines the max number of handles that can be allocated.

You should use critical sections to protect all the Share* functions.

You could also create a replacement TMemoryManager, which handles this in the DLL. I do believe you could manage to create shared objects this way, but I've never tried as it would be a heck of a job.


Erik.
0

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

10 Experts available now in Live!

Get 1:1 Help Now