Should I free a loaded module when disposing the object?
I'm loading a COM dll using this method:
[DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
private extern static IntPtr LoadLibrary(string librayName);
Should I release the d开发者_JS百科ll using:
[DllImport("kernel32", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
or just let the application termination handle it?
You should call FreeLibrary
when you're done using a DLL that you loaded using LoadLibrary
. This will not be an issue unless your application is long-running and you load many of these DLLs, or unless you would want to update the DLL on disk (it would be locked as long as it's loaded).
By the way, why are you using LoadLibrary
to load a COM DLL and not using TLBIMP to create a .NET-accessible wrapper?
精彩评论