Dll unloading 
Author Message
 Dll unloading

Can I unload an VB ActiveX Dll from the Dll itself?

Anthony



Wed, 12 May 2004 07:05:12 GMT  
 Dll unloading
Anthony,

Any VB object will unload as soon as there are no more references to it.
Therefore to get the dll to unload all that needs to be done is to release
all references to the objects created from the dll and it should unload by
itself.

One problem that may occur is where the dll objects themselves hold
references to itself - this is called circular references. To solve this you
will need to build your own terminate function that will be able to release
these references and the parent application will need to initiate this call.

If you want the object itself to unload by ignoring the references to it - I
do not believe that it is possible.  A workaround could be to create an
unload event and make a general rule that any app that uses your dll
implements this event and in this event all references to the object could
then be released.  You could then internally call this event to unload it.
Its no automated but it does work.

Anyway - hope this helps


Quote:
> Can I unload an VB ActiveX Dll from the Dll itself?

> Anthony



Thu, 13 May 2004 09:05:43 GMT  
 Dll unloading

Quote:
> Anthony,

<snip>
> One problem that may occur is where the dll objects themselves hold
> references to itself - this is called circular references. To solve this
you
> will need to build your own terminate function that will be able to
release
> these references and the parent application will need to initiate this

call.

There are other ways to deal with circular references (in case you were
wondering)
The above method works (and I've used it in the past), but does require
the client to use the object correctly (ie if the client doesn't call your
teardown method, you will still get a circular reference problem
IMHO this method is good in a small project, which is not to be used
by a third party (A third party would blame your DLL even if they didn't
use it correctly, and call the teardown method, and I say a small project
because which the larger ones during maintainence and adding new "client"
code calling the Teardown method can get forgotten about)

The method I use is to use a thing called a "proxy"

Basically, it's an object that the parent object holds and passes to the
child
objects when they are created.  The parent references this object
WithEvents.
When the child needs to use the parent, it calls a function on the proxy.
The proxy then raises an event with a ByRef parameter in it.
The parent handles the event by passing a reference to itself back to the
proxy.
The proxy simply passes this back to the child object :-)

Of course if the child object now kept that reference, it would produce
a circular reference, so it simply uses it for what it needs to, then
releases it

The details are at:
http://www.vb-faq.com/Articles/Hughson/proxyobjects.asp

The only problem is that if the client releases all it's references to the
parent
but maintains a reference to the client, the parent will unload, leaving
the child orphaned (and the proxy function will return Nothing)

Another method is to use a combination of ObjPtr and CopyMemory
to fool VB into having an additional reference to the parent without
incrementing the reference count
This has the same "orphaning" problem as above, but if you try to use
the parent in this case, the app will GPF, so measures need to be made
in the parents Terminate event to inform the children that it's unloading

Quote:
> If you want the object itself to unload by ignoring the references to it -
I
> do not believe that it is possible.  A workaround could be to create an
> unload event and make a general rule that any app that uses your dll
> implements this event and in this event all references to the object could
> then be released.  You could then internally call this event to unload it.
> Its no automated but it does work.

Good idea :-)

Another problem is programs like IIS that keep hold of the DLL even
when no objects within it are being used
They usually do this to save time, so the DLL doesn't have to be loaded
again the next time it's used (which in the case of IIS can be quite often
<g>)
These apps need to be stopped/exited to unload the DLL

--
Chris Anderson
(aka Merak on VISBAS-L)



Thu, 13 May 2004 11:03:52 GMT  
 Dll unloading

Quote:


> The details are at:
> http://www.vb-faq.com/Articles/Hughson/proxyobjects.asp

You can get the same with one reusable (weakly typed object) :
http://users.skynet.be/wvdd2/General_techniques/Cyclic_References/cyc...
erences.html

(In .NET we don't have to care about these things anymore.  We don't know
yet about which things we DO have to care)
--
Van den Driessche Willy
For a work in progress :
http://users.skynet.be/wvdd2/index.html



Fri, 14 May 2004 08:25:23 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. DLLs, Unloading and Dynamic selection

2. Error when DLL unloading from MTS.. please help!

3. unload dll

4. Excel doesn't unload dll after FreeLibrary

5. Unloading a DLL from the webserver

6. Unload idle DLL

7. Unloading an Activex dll from Memory....

8. unloading dlls

9. How to unload a dll file ?

10. Unloading DLL

11. How to load, unload IIS .DLLs?

12. How unload on run time a xxxxx.dll

 

 
Powered by phpBB® Forum Software