コード例 #1
0
        private void LoadDll(string dllPath)
        {
            Guard.AgainstNullOrEmptyOrWhiteSpaceString(dllPath, nameof(dllPath));

            dllHandle = WinFunctions.LoadLibrary(dllPath);
            if (dllHandle == IntPtr.Zero)
            {
                throw new Exception("加载dll失败.");
            }
        }
コード例 #2
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!disposed)
                {
                    WinFunctions.FreeLibrary(dllHandle);
                }

                disposed = true;
            }
        }
コード例 #3
0
        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)));
        }