Solved

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

Posted on 1998-08-03
12
852 Views
Last Modified: 2013-12-04
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?

0
Comment
Question by:robpitt
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1412938
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
 
LVL 22

Expert Comment

by:nietod
ID: 1412939
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
 
LVL 23

Expert Comment

by:chensu
ID: 1412940
I agree with nietod. It shouldn't be like this. There must be something wrong.
0
 
LVL 2

Accepted Solution

by:
abesoft earned 100 total points
ID: 1412941
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
 
LVL 2

Expert Comment

by:abesoft
ID: 1412942
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
 
LVL 22

Expert Comment

by:nietod
ID: 1412943
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Expert Comment

by:abesoft
ID: 1412944
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
 
LVL 22

Expert Comment

by:nietod
ID: 1412945
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
 
LVL 5

Author Comment

by:robpitt
ID: 1412946
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
 
LVL 2

Expert Comment

by:abesoft
ID: 1412947
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
 
LVL 2

Expert Comment

by:abesoft
ID: 1412948
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
 
LVL 5

Author Comment

by:robpitt
ID: 2697734
<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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now