Closing an unmanaged DLL

D

dvestal

I have a C# app that must use a C# class library, which in turn
references an unmanaged C++ DLL. My code interacts briefly with the
C# class library, after which my code must fully release it, retaining
no references to either the class library or the DLL (which the class
library uses via DllImport).

Unfortunately, neither the class library nor the C++ DLL are under my
control, and I can't change the constraints above.

I've managed to release the C# dll by interacting with it in a
temporary AppDomain, which I then unload. However, my process still
retains a reference to the C++ DLL. How can I either avoid that, or
close the reference?
 
N

Nicholas Paldino [.NET/C# MVP]

The only way I can think of to do this would be to not use the P/Invoke
layer directly, and instead use calls to LoadLibrary/FreeLibrary along with
GetProcAddress to get a pointer to the functions you are calling, then call
the static GetDelegateForFunctionPointer method on the Marshal class to get
a delegate which you can call which will perform the call to the unmanaged
code.
 
D

dvestal

The only way I can think of to do this would be to not use the P/Invoke
layer directly, and instead use calls to LoadLibrary/FreeLibrary along with
GetProcAddress to get a pointer to the functions you are calling, then call
the static GetDelegateForFunctionPointer method on the Marshal class to get
a delegate which you can call which will perform the call to the unmanaged
code.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have a C# app that must use a C# class library, which in turn
references an unmanaged C++ DLL. My code interacts briefly with the
C# class library, after which my code must fully release it, retaining
no references to either the class library or the DLL (which the class
library uses via DllImport).
Unfortunately, neither the class library nor the C++ DLL are under my
control, and I can't change the constraints above.
I've managed to release the C# dll by interacting with it in a
temporary AppDomain, which I then unload. However, my process still
retains a reference to the C++ DLL. How can I either avoid that, or
close the reference?

Nicholas,

Unfortunately, neither the class library nor the managed code are
under my control. I can't change how they're written. As a last
resort, I can interact with them in another process entirely, then
kill that process, but I really don't want to do that. The old
analogy of using a cannon to kill a gnat comes to mind.
 
W

Willy Denoyette [MVP]

The only way I can think of to do this would be to not use the
P/Invoke
layer directly, and instead use calls to LoadLibrary/FreeLibrary along
with
GetProcAddress to get a pointer to the functions you are calling, then
call
the static GetDelegateForFunctionPointer method on the Marshal class to
get
a delegate which you can call which will perform the call to the
unmanaged
code.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have a C# app that must use a C# class library, which in turn
references an unmanaged C++ DLL. My code interacts briefly with the
C# class library, after which my code must fully release it, retaining
no references to either the class library or the DLL (which the class
library uses via DllImport).
Unfortunately, neither the class library nor the C++ DLL are under my
control, and I can't change the constraints above.
I've managed to release the C# dll by interacting with it in a
temporary AppDomain, which I then unload. However, my process still
retains a reference to the C++ DLL. How can I either avoid that, or
close the reference?

Nicholas,

Unfortunately, neither the class library nor the managed code are
under my control. I can't change how they're written. As a last
resort, I can interact with them in another process entirely, then
kill that process, but I really don't want to do that. The old
analogy of using a cannon to kill a gnat comes to mind.



You can get the module handle by calling GetModuleHandle and use this handle
in a call to FreeLibrary.
Here are the DllImport declarations and a usage sample.

[DllImport("kernel32")] static extern IntPtr GetModuleHandle(string
module);
[DllImport("kernel32")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool FreeLibrary(IntPtr handle);

....
IntPtr HModule = GetModuleHandle("nativeModule.dll");
bool success = FreeLibrary(HModule);
if(success)
...
else
...


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top