Solved

Free a DLL Reference (equivalent to FreeLibrary).

Posted on 2002-06-19
6
1,050 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
[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
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
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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

635 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