Solved

Free a DLL Reference (equivalent to FreeLibrary).

Posted on 2002-06-19
6
1,000 Views
Last Modified: 2012-06-27
I have referenced a legacy DLL using the DLLImport mechanism. Is it possible to manually free the reference after I have used it? (i.e. equivalent to using Win 32 FreeLibrary()).

Alternatively, if I could use LoadLibrary() & FreeLibrary () instead of the import facility, that would do fine

Explanation:

The C# code calls the DLL function to determine whether a particular application is running. If the DLL reference is held, the application then refuses to start.
0
Comment
Question by:JonathanAyres
6 Comments
 
LVL 4

Expert Comment

by:saar2
ID: 7095114
Paste this in your code and will let you call FreeLibrary to unload the DLL.

using System.Runtime.InteropServices;
[DllImport ("Kernel32")]
public static extern bool FreeLibrary(IntPtr hModule);

[DllImport ("Kernel32")]
public static extern IntPtr GetModuleHandle(string lpModuleName);


As you don't have the module handler for FreeLibrary you'll have to get it by calling GetModuleHandle (pass you dll filename as a parameter).

Please let me know if this works.

I don't think you want to use LoadLibrary as you probably have problems with GetProcAddress.

Good luck.

Saar Carmi
Israel .Net Developer
saar@bigfoot.com
0
 

Author Comment

by:JonathanAyres
ID: 7103093
Thanks Saar

That seems to work but has a rather significant drawback. Once I have freed the library, I can no longer call the method - it seems that the DLLImport facility does not expect to have its library freed and has no reload facility.

Obviously, if I could use LoadLibrary it would allow me to get round this but as you point out, there will be problems with GetProcAddress (or at least using the address that it returns.

Jonathan
0
 
LVL 4

Expert Comment

by:saar2
ID: 7103113
We cannot be certain that the DllImport won't call GetProcAddress again - until we try that.

Saar
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:jrspano
ID: 7122081
It sounds like the functions in your dll are locking the application you are wanting to run.  Just .net holding a reference to the dll and its functions should in no way lock another app from running.  I would check the dll code first.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9485123
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
PAQ/No Refund
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheAvenger
EE Cleanup Volunteer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9537574
PAQed - no points refunded (of 50)

Computer101
E-E Admin
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

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