
Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
On Feb 22, 2:05 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
> 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
[quoted text clipped - 24 lines]
> > 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.
Willy Denoyette [MVP] - 22 Feb 2008 20:15 GMT
> On Feb 22, 2:05 pm, "Nicholas Paldino [.NET/C# MVP]"
> <m...@spam.guard.caspershouse.com> wrote:
[quoted text clipped - 39 lines]
> 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.