My C# application (.NET Framework 4.0) imports an external unmanaged DLL with the following code:
[DllImport("myDLL.dll"), EntryPoint="GetLastErrorText"]
private static extern IntPtr GetLastErrorText();
Unfortunately there seems to be a bug in the third-party DLL. As a workaround I would need to unload the DLL and reload it afterwards. How can I do this? I've seen several posts but they all talk about managed DLLs.
Rather than using
DllImport
to import the DLL, you could try usingLoadModule
(from WinAPI) to do so, and then useGetProcAddress
andFreeLibrary
to do what you need as far as calling functions in it and unloading/reloading it.See here.
Might be a bit prettier/manageable if you used C++/CLR to glue C# and the unmanaged DLL together.
I think you'll need to go down to using LoadLibrary/FreeLibrary/GetProcAddress as shown in Difference between dllimport and getProcAddress : Abbreviated sample (no error handling) below:
You can write a wrapper around the library that manages the access to it. Then you can use native methods to call the library. Take a look at this blog post.