/// <summary> /// Loads the given library /// </summary> /// <param name="libraryName">Name of the library to load</param> public Library(string libraryName) { _handle = NativeMethods.LoadLibrary(libraryName); if (_handle == IntPtr.Zero) { throw new ArgumentException($"Cannot load library '{libraryName}'. {InteropUtility.GetLastErrorCodeAndMessage()}"); } Name = libraryName; }
/// <summary> /// Gets a delegate for an unmanaged function of the given name /// </summary> /// <typeparam name="TDelegate">The type of delegate to which to convert the function pointer</typeparam> /// <param name="functionName">The name of the function</param> public TDelegate GetDelegate <TDelegate>(string functionName) { if (!TryGetFunctionPointer(functionName, out IntPtr functionPointer)) { throw new ArgumentException($"Cannot load function '{functionName}' from library '{Name}'. {InteropUtility.GetLastErrorCodeAndMessage()}"); } TDelegate result = Marshal.GetDelegateForFunctionPointer <TDelegate>(functionPointer); return(result); }
/// <summary> /// Frees the unmanaged library represented by this object /// </summary> public void Dispose() { InteropUtility.ClearPointer(ref _handle, NativeMethods.FreeLibrary); Name = null; }
/// <summary> /// Frees the unmanaged memory via <see cref="Marshal.FreeHGlobal"/> /// </summary> public virtual void Dispose() { InteropUtility.ClearPointer(ref _pointer, Marshal.FreeHGlobal); }