Solved

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

Posted on 1998-08-03
12
882 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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
 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Enable Clear Text in Win 8.1 7 48
Query performance - SQL Server 20 85
Need to create an object factory 2 49
Where to obtain Oracle Data Provider for .NET 3.5 7 60
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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