Solved

Free a DLL Reference (equivalent to FreeLibrary).

Posted on 2002-06-19
6
1,009 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 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

18 Experts available now in Live!

Get 1:1 Help Now