Solved

Memory Mapping

Posted on 1997-03-13
1
497 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, DigitalPersona Fingerprint reader and MySQL 6 267
Firemonkey webbrowser scrollbars ? 1 39
RESTRequest Parameter 4 30
Tvertscrollbox like a whatsapp layout 5 27
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…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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