Newsgroups: comp.windows.ms.programmer
Path: utzoo!utgpu!watserv1!watmath!hyper.hyper.com!bonneau
From: bonneau@hyper.hyper.com (Paul Bonneau)
Subject: Re: GlobalAlloc by DLL
Message-ID: <1991May21.224503.21936@hyper.hyper.com>
Reply-To: bonneau@hyper.UUCP (Paul Bonneau,,)
Organization: HyperCube Inc.
References: <1652@msa3b.UUCP>
Date: Tue, 21 May 1991 22:45:03 GMT

In article <1652@msa3b.UUCP> kevin@msa3b.UUCP (Kevin P. Kleinfelter) writes:
>I'd like to do a GlobalAlloc in a DLL, and have the memory owned by the
>DLL (not the application that calls the DLL).  I don't mean I want to
>allocate the memory in LibMain; my application calls a DLL routine, and
>I want the DLL routine to GlobalAlloc.
>
>What SEEMS to happen is that the GlobalAlloc allocates memory that is
>"owned" by the application.  I have another application that makes other
>calls to the DLL to update and retrieve data from this memory.
>
From the "Guide To Programming", page 20-34 "When a DLL
allocates a global object, the application that called the
DLL owns that object".

I don't know of any API to allow a DLL to "own" global
handles, but it obviously can be done, since GDI (which is
nothing more than a DLL itself) does so when you create
brushes, icons, etc.

If the amount of memory you need is small, you might consider
using a LocalHeap in the DLL (in this case you must
explicitly create the heap with LocalInit()).  When you pass
a reference to this memory to the caller, locking the local
object, and casting to a far pointer will provide the address
of the data.  You can then use LocalHandle() to extract the
handle from the locked address for those APIs of the DLL
that accept the reference to the memory back from the app.

cheers - Paul Bonneau.
