Solved

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

Posted on 1998-08-03
12
899 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 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