15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Pointers to shared memory within DLL ?

Bondono -- bondono@homer.eaglequest.com
Saturday, July 20, 1996

Environment: VC++4.1 under Win95.

I have a DLL that controls access to an external device.  It needs to
keep track of what resources on that external device are allocated to
the win95 programs which are talking to that device through the DLL.

I believe I need to share memory between the applications to
accomplish this.

My problem is that since all forms of shared memory that I've found
have different addresses within the different processes which are
using the dll, I can't have pointers to objects, like CObArrays or
CMapStringToPtrs.

Does anyone know a way that I can use pointers to simplify the
implementation of this dll ?

Thanks,
Jeff



Vilas Patil -- vilasp@rpsadf.atlantaga.ncr.com
Tuesday, July 23, 1996

[Mini-digest: 5 responses]


Hi:

No, you can not share memory in 32 bit dlls.  Ruled out.  I suggest if you 
have fairly small piece of data e.g. small string, a number etc.  Then you 
can think of Atoms.  Register atoms and then use 'em in other apps. 
 Secondly, and the best, is to use OLE dataobject.  But the secons one is 
bit involved and will need some fundamental changes in your app.


 ----------
From: owner-mfc-l
To: mfc-l
Subject: Pointers to shared memory within DLL ?
Date: Saturday, July 20, 1996 7:28AM

Environment: VC++4.1 under Win95.

I have a DLL that controls access to an external device.  It needs to
keep track of what resources on that external device are allocated to
the win95 programs which are talking to that device through the DLL.

I believe I need to share memory between the applications to
accomplish this.

My problem is that since all forms of shared memory that I've found
have different addresses within the different processes which are
using the dll, I can't have pointers to objects, like CObArrays or
CMapStringToPtrs.

Does anyone know a way that I can use pointers to simplify the
implementation of this dll ?

Thanks,
Jeff
-----From: "Tomasz Pawlowski" 

Yes you can use pointers. Easiest approach is to use memory mapped files
(see Richter's "Advanced Windows").
You may also consider use of WM_COPYDATA message.

Tomasz
tomasz@ix.netcom.com

-----From:  (Jeff Grossman)

I believe that SmartHeap 3.1 by MicroQuill supports
allocating shared memory that insures the same
address in all processes.

Jeff
-----From: "Jeff Lomax" 

Although I haven't implemented it (yet) my understanding is that you can use 
BASED pointers to overcome the "different starting address in each process" 
problem.  Another "solution" would be to convert you .DLL to a single COM 
object and use out-of-process calls into it.  Of couse, you will have a 
significant performance hit.

Isn't Shared Memory in Win32 fun?

Jeff Lomax
Phoenix Technologies, Ltd.
-----From: Barry Tannenbaum 

The easiest solution is to use offsets instead of pointer.  Every "address"
in your data structures is really an offset from the start of the shared
memory segment.  To get the mapped address, simply add the offset to the
base address.

        - Barry

--------------------------------------------------------------------------------

3DV Technology, Inc              Phone: (603) 595-2200 X228
410 Amherst St., Suite 150       Fax:   (603) 595-2228
Nashua, NH  03063                Net:   barry@dddv.com





Alberto Massari -- alby@belva.laboratorium.dist.unige.it
Monday, July 29, 1996

At 11.16 23/07/96 PDT, you wrote:
>Environment: VC++4.1 under Win95.
>
>I have a DLL that controls access to an external device.  It needs to
>keep track of what resources on that external device are allocated to
>the win95 programs which are talking to that device through the DLL.
>
>I believe I need to share memory between the applications to
>accomplish this.
>
>My problem is that since all forms of shared memory that I've found
>have different addresses within the different processes which are
>using the dll, I can't have pointers to objects, like CObArrays or
>CMapStringToPtrs.
>
>Does anyone know a way that I can use pointers to simplify the
>implementation of this dll ?

Jeff, make the DLL a shared one. In that way, each time an EXE links to the
DLL, it will be linked to the same instance of data. Moreover, under Win95,
you are garanteed that the DLL will be loaded at the same address for each
EXE (while on WinNT this is not true).

To create the shared segment you have to enclose the declaration of the
shared variables between two "pragma" command:

#pragma data_seg("SHARED_SEG")
int 			nCounter=0;
.....
#pragma data_seg()

In the .DEF file, after the DATA command, write

SEGMENTS		SHARED_SEG READ WRITE SHARED

P.S. Remember that only ASSIGNED variable are put in the shared segment
(that is, if you write "int pippo;" between the two pragmas, pippo will not
be allocated in the shared segment; this will happen only if you write "int
pippo=0;")

Cheers

        Alberto Massari
----------------------------
    |\      _,,,--,,_
    /,`.-'`'   ._  \-;;,_
   |,4-  ) )_   .;.(  `'-'
  '---''(_/._)-'(_\_)    
------------------------------------------------------------------
Alby@MusArt.dist.unige.it is: 	Alberto Massari

Laboratorio di Informatica Musicale
D.I.S.T. Dipartimento di Informatica, Sistemistica e Telematica
Universita' di Genova
Via all'Opera Pia 13,
I - 16145 Genova ITALIA		http://MusArt.dist.unige.it/~alby/
------------------------------------------------------------------




Jeff Lomax -- jeff_lomax@ptltd.com
Friday, August 02, 1996

Using #pragma data_seg in Win '95 is not very safe. Use Memory-Mapped Files 
(and based pointers) for reliable shared memory.  Here is MS's Q89187 on the 
subject:  Note that you have NO WAY to determine the failures (and we saw 
them quite frequently).

SUMMARY
=======
 
To have both shared and nonshared data in a dynamic-link library (DLL)
which is built with a 32-bit Microsoft C compiler, you need to use the
#pragma data_seg directive to set up a new named section. You then must
specify the correct sharing attributes for this new named data section in
your .DEF file.
 
The system will try to load the shared memory block created by #pragma
data_seg at the same address in each process. However, if the block cannot
be loaded into the same memory address, the system allocates a new block of
memory for that process and the memory is not shared. No run time warnings
are given when this happens. Using memory-mapped files backed by pagefile
(named shared memory) is a safer option than using #pragma data_seg,
because the APIs will return an error when the mapping fails.


Jeff
-------------
Original Text
From: Alberto Massari , on 7/29/96 
10:29 AM:
At 11.16 23/07/96 PDT, you wrote:
>Environment: VC++4.1 under Win95.
>
>I have a DLL that controls access to an external device.  It needs to
>keep track of what resources on that external device are allocated to
>the win95 programs which are talking to that device through the DLL.
>
>I believe I need to share memory between the applications to
>accomplish this.
>
>My problem is that since all forms of shared memory that I've found
>have different addresses within the different processes which are
>using the dll, I can't have pointers to objects, like CObArrays or
>CMapStringToPtrs.
>
>Does anyone know a way that I can use pointers to simplify the
>implementation of this dll ?

Jeff, make the DLL a shared one. In that way, each time an EXE links to the
DLL, it will be linked to the same instance of data. Moreover, under Win95,
you are garanteed that the DLL will be loaded at the same address for each
EXE (while on WinNT this is not true).

To create the shared segment you have to enclose the declaration of the
shared variables between two "pragma" command:

#pragma data_seg("SHARED_SEG")
int 			nCounter=0;
.....
#pragma data_seg()

In the .DEF file, after the DATA command, write

SEGMENTS		SHARED_SEG READ WRITE SHARED

P.S. Remember that only ASSIGNED variable are put in the shared segment
(that is, if you write "int pippo;" between the two pragmas, pippo will not
be allocated in the shared segment; this will happen only if you write "int
pippo=0;")

Cheers

        Alberto Massari
----------------------------
    |\      _,,,--,,_
    /,`.-'`'   ._  \-;;,_
   |,4-  ) )_   .;.(  `'-'
  '---''(_/._)-'(_\_)    
------------------------------------------------------------------
Alby@MusArt.dist.unige.it is: 	Alberto Massari

Laboratorio di Informatica Musicale
D.I.S.T. Dipartimento di Informatica, Sistemistica e Telematica
Universita' di Genova
Via all'Opera Pia 13,
I - 16145 Genova ITALIA		http://MusArt.dist.unige.it/~alby/
------------------------------------------------------------------




Peter Jones -- peter.jones@autodesk.com
Friday, August 09, 1996

     All,
     
     Regarding the Microsoft KB article Q89187 stating that sharing memory 
     by using #pragma data_seg in Win95 isn't safe. I sent an email to 
     Jeffrey Richter, author of ADVANCED WINDOWS, since he mentions this 
     shared memory technique in his book.
     
     His response is pasted below. In a nutshell he asserts that the MSoft 
     article is totally wrong and this has been admitted to by MSoft. Maybe 
     someone should just do a test causing the shared mem to be mapped 
     differently in different processes and verify that the mem is still 
     shared.
     
     FYI.
     
     Pj.
     
     PS. Jeffrey Richter's response:
     --------------------------------
     
     _________________________________________________________
     Subject: RE: Shared memory in DLL and Win95
     Author:  "Jeffrey Richter (Independent)"  at 
     SMTPCC3
     Date:    8/7/96 11:14 AM
     
     
     Microsoft is aware that this article is completely bogus but for some 
     reason refuse to remove it from MSDN.  The article is incorrect.
     
     --Jeff
     
     >----------
     >From:  peter.jones@autodesk.com[SMTP:peter.jones@autodesk.com] >Sent: 
      Tuesday, August 06, 1996 12:46 PM
     >To:  Jeffrey Richter (Independent)
     >Subject:  Shared memory in DLL and Win95 >
     >     Jeffrey,
     >     
     >     I came across this MS KB article. What do you make of this? Is 
     this >     really a problem?
     >     
     >     I have enjoyed your Advanced Windows book and have adopted the 
     shared >     memory techinque. Do I need to worry about differences in 
     Win95?
     >     
     >     Thanks,
     >     
     >     Peter Jones
     >     Autodesk



______________________________ Reply Separator 
_________________________________
Subject: RE: Pointers to shared memory within DLL ?
Author:  mfc-l@netcom.com at smtpcc1
Date:    8/2/96 8:58 AM


Using #pragma data_seg in Win '95 is not very safe. Use Memory-Mapped Files 
(and based pointers) for reliable shared memory.  Here is MS's Q89187 on 
the subject:  Note that you have NO WAY to determine the failures (and we 
saw them quite frequently).

SUMMARY
=======

To have both shared and nonshared data in a dynamic-link library (DLL) 
which is built with a 32-bit Microsoft C compiler, you need to use the 
#pragma data_seg directive to set up a new named section. You then must 
specify the correct sharing attributes for this new named data section in 
your .DEF file.
     
The system will try to load the shared memory block created by #pragma 
data_seg at the same address in each process. However, if the block cannot 
be loaded into the same memory address, the system allocates a new block of 
memory for that process and the memory is not shared. No run time warnings 
are given when this happens. Using memory-mapped files backed by pagefile 
(named shared memory) is a safer option than using #pragma data_seg, 
because the APIs will return an error when the mapping fails.
     
     
Jeff
-------------
Original Text
From: Alberto Massari , on 7/29/96 
10:29 AM:
At 11.16 23/07/96 PDT, you wrote:
>Environment: VC++4.1 under Win95.
>
>I have a DLL that controls access to an external device.  It needs to 
>keep track of what resources on that external device are allocated to 
>the win95 programs which are talking to that device through the DLL. 
>
>I believe I need to share memory between the applications to 
>accomplish this.
>
>My problem is that since all forms of shared memory that I've found 
>have different addresses within the different processes which are 
>using the dll, I can't have pointers to objects, like CObArrays or 
>CMapStringToPtrs.
>
>Does anyone know a way that I can use pointers to simplify the 
>implementation of this dll ?
     
Jeff, make the DLL a shared one. In that way, each time an EXE links to the 
DLL, it will be linked to the same instance of data. Moreover, under Win95, 
you are garanteed that the DLL will be loaded at the same address for each 
EXE (while on WinNT this is not true).
     
To create the shared segment you have to enclose the declaration of the 
shared variables between two "pragma" command:
     
#pragma data_seg("SHARED_SEG")
int    nCounter=0;
.....
#pragma data_seg()
     
In the .DEF file, after the DATA command, write
     
SEGMENTS  SHARED_SEG READ WRITE SHARED
     
P.S. Remember that only ASSIGNED variable are put in the shared segment 
(that is, if you write "int pippo;" between the two pragmas, pippo will not 
be allocated in the shared segment; this will happen only if you write "int 
pippo=0;")
     
Cheers
     
        Alberto Massari
----------------------------
    |\      _,,,--,,_
    /,`.-'`'   ._  \-;;,_
   |,4-  ) )_   .;.(  `'-'
  '---''(_/._)-'(_\_)    
------------------------------------------------------------------ 
Alby@MusArt.dist.unige.it is:  Alberto Massari
     
Laboratorio di Informatica Musicale
D.I.S.T. Dipartimento di Informatica, Sistemistica e Telematica 
Universita' di Genova
Via all'Opera Pia 13,
I - 16145 Genova ITALIA  http://MusArt.dist.unige.it/~alby/ 
------------------------------------------------------------------
     





| Вернуться в корень Архива |