private void LoadDll(string dllPath) { Guard.AgainstNullOrEmptyOrWhiteSpaceString(dllPath, nameof(dllPath)); dllHandle = WinFunctions.LoadLibrary(dllPath); if (dllHandle == IntPtr.Zero) { throw new Exception("加载dll失败."); } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (!disposed) { WinFunctions.FreeLibrary(dllHandle); } disposed = true; } }
public static TDelegate LoadUnmanagedFunction <TDelegate>(IntPtr dllHandle, string functionName) where TDelegate : class { if (dllHandle == IntPtr.Zero) { throw new ArgumentException("DLL handle is invalid.", nameof(dllHandle)); } Guard.AgainstNullOrEmptyOrWhiteSpaceString(functionName, nameof(functionName)); var functionPtr = WinFunctions.GetProcAddress(dllHandle, functionName); if (functionPtr == IntPtr.Zero) { return(null); } return((TDelegate)(object)Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(TDelegate))); }