Index: src/Common/DelftTools.Utils/Interop/NativeLibrary.cs =================================================================== diff -u -r8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9 -r5fc71a385897af92ccb092f2f969b5709afab85a --- src/Common/DelftTools.Utils/Interop/NativeLibrary.cs (.../NativeLibrary.cs) (revision 8f6ae890fed8e8eae3a32f9c0498a10f82e0ddf9) +++ src/Common/DelftTools.Utils/Interop/NativeLibrary.cs (.../NativeLibrary.cs) (revision 5fc71a385897af92ccb092f2f969b5709afab85a) @@ -8,6 +8,13 @@ { public abstract class NativeLibrary : IDisposable { + private IntPtr lib = IntPtr.Zero; + + protected NativeLibrary(string fileName) + { + lib = LoadLibrary(fileName); + } + [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] public static extern IntPtr LoadLibrary(string lpFileName); @@ -17,55 +24,15 @@ //private: use SwitchDllSearchDirectory with a using instead [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] - public static extern void SetDllDirectory(string lpPathName); + public static extern void SetDllDirectory(string lpPathName); - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - private static extern int GetDllDirectory(int nBufferLength, StringBuilder lpPathName); - public static string GetDllDirectory() { var tmp = new StringBuilder(4096); GetDllDirectory(4096, tmp); return tmp.ToString(); } - private IntPtr lib = IntPtr.Zero; - - protected NativeLibrary(string fileName) - { - lib = LoadLibrary(fileName); - } - - ~NativeLibrary() - { - Dispose(); - } - - public void Dispose() - { - if (lib == IntPtr.Zero) - { - return; - } - - FreeLibrary(lib); - - lib = IntPtr.Zero; - } - - protected IntPtr Library - { - get - { - if (lib == IntPtr.Zero) - { - throw new InvalidOperationException("Plug-in library is not loaded"); - } - - return lib; - } - } - /// /// Call this from a static constructor in a class that has DllImport external methods. This /// method uses LoadLibrary to load the correct dll for the current process (32bit or 64bit) @@ -112,6 +79,39 @@ return new SwitchDllSearchDirectoryHelper(dllDirectory); } + public void Dispose() + { + if (lib == IntPtr.Zero) + { + return; + } + + FreeLibrary(lib); + + lib = IntPtr.Zero; + } + + protected IntPtr Library + { + get + { + if (lib == IntPtr.Zero) + { + throw new InvalidOperationException("Plug-in library is not loaded"); + } + + return lib; + } + } + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + private static extern int GetDllDirectory(int nBufferLength, StringBuilder lpPathName); + + ~NativeLibrary() + { + Dispose(); + } + private class SwitchDllSearchDirectoryHelper : IDisposable // ??? { private readonly string oldDirectory;