Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Free a DLL Reference (equivalent to FreeLibrary).

Posted on 2002-06-19
6
Medium Priority
?
1,059 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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.
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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