Article View: borland.public.cppbuilder.nativeapi
Article #14285How to use more than one shared-memory-blocks inside a dll?
From: MR
Date: Wed, 20 Aug 2003 07:05
Date: Wed, 20 Aug 2003 07:05
109 lines
4406 bytes
4406 bytes
How to use more than one shared-memory-blocks inside a dll?
I do successfully use one structure-instance containing shared memory,
i.e. all changes in one instance of the dll is directly visible in all
instances of the dll.
Now I wanted to use a second, different dll, which also has such a
shared memory. I created it the same way and it seems to work fine for
it's own.
The only problem is, that using these both dlls in one application,
these shared blocks overlapp each other completely, i.e. if I change
the first byte in the first dll's shared memory, it is also changed in
the second. Naturally this is definitely not, what I intended: the
data should be shared between all instances of ONE dll, not of all
instances of all dlls.
The implmentation is done with
~~~~~~
#pragma option -zRSHARESEG_##_DLL_PROJECT_NAME
#pragma option -zTSHARECLS_##_DLL_PROJECT_NAME
~~~~~~
where _DLL_PROJECT_NAME is (correctly) defined in the project-file and
contains the name of the dll to be created.
After this the allocation of the shared memory is done with
~~~~~~
bool DLLSharedMem_Handle(void **_DLLSharedMem, size_t
_DLLSharedMem_size, DWORD fdwReason) {
static HANDLE hMapObject=NULL; // handle to file mapping
BOOL fInit, fIgnore;
switch (fdwReason) {
// The DLL is loading due to process
// initialization or a call to LoadLibrary.
case DLL_PROCESS_ATTACH:
// Create a named file mapping object.
hMapObject=CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security attributes
PAGE_READWRITE, // read/write access
0/*_DLLSharedMem_size/(1<<32UL)*/,// size: high 32-bits
_DLLSharedMem_size/*%(1<<32UL)*/, // size: low 32-bits (2^32 =
4096 MByte)
"dllmemfilemap"); // name of map object
if(hMapObject==NULL)
return(false);
// The first process to attach initializes memory.
fInit=(GetLastError()!=ERROR_ALREADY_EXISTS);
// Get a pointer to the file-mapped shared memory.
*_DLLSharedMem=(struct sDLLSharedMem *)MapViewOfFile(
hMapObject, // object to map view of
FILE_MAP_WRITE, // read/write access
0, // high offset: map from
0, // low offset: beginning
0); // default: map entire file
if(*_DLLSharedMem==NULL)
return(false);
// Initialize memory if this is the first process.
if(fInit)
memset(*_DLLSharedMem, '\0', _DLLSharedMem_size);
break;
// The DLL is unloading from a process due to
// process termination or a call to FreeLibrary.
case DLL_PROCESS_DETACH:
// Unmap shared memory from the process's address space.
/*fIgnore=*/UnmapViewOfFile(*_DLLSharedMem);
// Close the process's handle to the file-mapping object.
/*fIgnore=*/CloseHandle(hMapObject);
break;
// The attached process creates a new thread.
case DLL_THREAD_ATTACH:
break;
// The thread of the attached process terminates.
case DLL_THREAD_DETACH:
break;
default:
break;
}
return(true);
}
~~~~~~
I even tried to rename DLLSharedMem_Handle to make it's name unique
for each dll, but the result was the same.
What could be my problem and how can I try to solve it?
Thanks a lot,
Michael
Technical Details
Message-ID:
<1e76kvs3dii9cjg74pbhppfukh7m6vserh@4ax.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,51ab868baa5f5d8e,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-08-20 00:10:23 PST From: M_R <> Newsgroups: borland.public.cppbuilder.nativeapi Subject: How to use more than one shared-memory-blocks inside a dll? Date: Wed, 20 Aug 2003 09:05:43 +0200 Message-ID: <1e76kvs3dii9cjg74pbhppfukh7m6vserh@4ax.com> X-Newsreader: Forte Free Agent 1.93/32.576 Deutsch (Standard) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 217.80.34.76 X-Trace: newsgroups.borland.com 1061363142 217.80.34.76 (20 Aug 2003 00:05:42 -0700) Lines: 110 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail