How to force dll (dsetup.dll) to unload if ref count > 0 ?

Is there any way to explicitly remove a loaded library
other than using FreeLibrary?

Heres my problem:

The problem arrose an anoying problem with DSETUP.dll on multi CD installs.

I have a setup program which installs DirectX. I used
LoadLibrary("dsetup.dll"), GetProcAddress("DirectXSetup")
and then called FreeLibary(hDSetupDLL) when done.

Unfortunatly despite doing this DSETUP.DLL & DSETUP32.DLL
are still mapped and alledgedly referenced after I call
FreeLibrary. This would be okay if it were not for the
fact that user later changes to another CD. Then when my
setup exe terminates, I get a blue screen telling me to
insert the other CD again.

Note the problem only arrises IF I call the exported function DirectXSetup. This appears to increase the
ref count to 2 so when I call my FreeLibary the dll
remains loaded.

Any ideas how?

LVL 5
robpittAsked:
Who is Participating?
 
abesoftCommented:
We had a similar problem with an app a few years ago.  Our work-around was to call FreeLibrary three times instead of just once.  Obviously, this is less than ideal, but we felt justified in doing it since:
    1) We knew that we were the ones responsible for upping the usage count.
    2) We had no way to stop incrmenting the usage count.
    3) We knew that nobody else was using the DLL.
Obviously, I would suggest that you test this, and make sure you don't make any mistakes.  (Although extra FreeLibrary calls are fine- they will fail gracefully if the library has been completely unloaded.)

Hope this helps
0
 
nietodCommented:
Some other program or DLL must be using the DSETUP.DLL.  You need to terminate that program.  There is no other way you could unload the DLL safely (since the other program is using it.)
0
 
nietodCommented:
I suppose another possibility that your program is not actually freeing the library.  You might want to check the return value from FreeLibrary and you might check that you really pass the right handle to it.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
chensuCommented:
I agree with nietod. It shouldn't be like this. There must be something wrong.
0
 
abesoftCommented:
Oh, for the record there are 50,000+ commercial users of the app that had that hack (er, feature) in it, and none have complained....  

Also, the reason we called FreeLibrary three times was because we linked to two other libraries that used the problem DLL.  It looks like you would only need to call it twice.
0
 
nietodCommented:
In general that's a dangerious solution.  If there is a bug where another library loads the library in question and forgets to unload it, you might be forced into that solution.  But if you don't know that that is the case you could be causing problems.  If you unload a library that is still needed you will regret it.
0
 
abesoftCommented:
nietod:
If you can prove (experimentally, at least) that you are incrementing the usage count on the library, and you are reasonably certain that you will never decrement it properly, then my solution is a safe one.  

In the case of my app, it was not explicitly linking to any of the libraries, but using implicit linking.  We had full source to all of the libraries, and nothing that we saw could explain the situation.  Of course, I am very curious if anyone can find an explanation...

If an external app were to start using the app, then decrementing our own usage count should not harm anything, since that app would be responsible for incrementing and decrementing it properly.  (That assumes that we do our work properly, and don't throw away too many copies...)
0
 
nietodCommented:
But the point is you are putting in a patch rather than fixing the original source of the problem.  That ussually comes back to haunt you later.  
0
 
robpittAuthor Commented:
After further investigation I can confirm that calling
dsetup.dll,DirectXSetup() increases the reference count
for by 1. This applies to both DX5 and DX6. BUG????????

Calling FreeLibrary a second time does fix the problem.
But an alternative which I have adopted is to copy the
offending dll to windows\temp and link to it there, this
then means that I can change CDs without the trouble.



If anyones really bored I have a test program below.

#include <windows.h>
#include <dsetup.h>

char szDirectXDir[]="E:\\directx\\";

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hNull,LPSTR cmd,int sw)
{
    HANDLE hDSetupDLL=NULL;    
    int (WINAPI * DirectXSetup)(HWND,LPSTR,DWORD);
    int i;

    SetCurrentDirectory(szDirectXDir);
    hDSetupDLL=LoadLibraryEx("dsetup.dll",NULL,LOAD_WITH_ALTERED_SEARCH_PATH);

    DirectXSetup=(int (WINAPI *)(HWND,LPSTR,DWORD))GetProcAddress(hDSetupDLL,"DirectXSetupA");

    i=DirectXSetup(NULL,szDirectXDir,DSETUP_DIRECTX);

    i=FreeLibrary(hDSetupDLL);
    //DSetup.DLL still loaded here so Free again!    

    i=FreeLibrary(hDSetupDLL);    
   
    return 0;
}
0
 
abesoftCommented:
As a suggestion, you might want to flag the temporary file for deletion the next time the system re-boots.  Use MoveFileEx with the MOVEFILE_DELAY_UNTIL_REBOOT flag.

This makes sure that the file will be removed the next time the user installs a product from Microsoft, which always seem to need a reboot ;)
0
 
abesoftCommented:
If you are not running under NT, there is another approach to deleting files on reboot.  E-mail me at abesoft@cgo.wave.ca if you need info on this.
0
 
robpittAuthor Commented:
<html>

IGNORE THIS POST - ITS JUST A TEST TO SEE IF YOU CAN PUT POST HTML RESPONSES




<P>
<B>Test</B><BR>
<I>Test</I><BR>
</html>

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.