AppDomain - . LoadLibrary FreeLibrary pinvoke, . , FreeLibrary, , .
, , :
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr LoadLibrary(string libname);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool FreeLibrary(IntPtr hModule);
class LibraryHandle
{
readonly string libName;
int refCount = 0;
IntPtr handle;
public LibraryHandle(string name)
{
libName = name;
}
public void Increment()
{
if (refCount == 0)
{
handle = LoadLibrary(libName);
if (Handle == IntPtr.Zero)
{
int error = Marshal.GetLastWin32Error();
throw new Exception(string.Format("{0})", error));
}
}
++refCount;
}
public void Decrement()
{
if (refCount <= 0)
return;
if (--refCount)
{
FreeLibrary(handle);
FreeLibrary(handle);
}
}
}
, , ASP.NET, .
In addition, since this may violate some assumptions made by the runtime, using FreeLibrarya library that you did not load may not be a good idea.
Another alternative would be to perform any operation in a new one AppDomain, and then unload it when you are done.
source
share