Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-08-03
12
Medium Priority
?
932 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 2

Accepted Solution

by:
abesoft earned 200 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

610 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