コード例 #1
0
        public static void FreeLibraryEx(LibraryHandle libraryHandle)
        {
            FreeLibrary(libraryHandle.DangerousGetHandle());

            if (File.Exists(libraryHandle.LibraryPath))
            {
                File.Delete(libraryHandle.LibraryPath);
            }
        }
コード例 #2
0
        internal static TDelegate GetUnmanagedDelegate <TDelegate>(this LibraryHandle library) where TDelegate : class
        {
            var delegateType     = typeof(TDelegate);
            var attributeType    = typeof(UnmanagedProcedureAttribute);
            var customAttributes = delegateType.GetCustomAttributes(attributeType, false);

            if (customAttributes.Length != 1)
            {
                throw new InvalidOperationException("Delegate " + delegateType.FullName + "should be marked with " + attributeType.FullName);
            }

            var procedureName = ((UnmanagedProcedureAttribute)customAttributes[0]).Name;
            var methodHandle  = GetProcAddress(library, procedureName);

            if (methodHandle.IsInvalid)
            {
                var error = Marshal.GetLastWin32Error();
                throw new InvalidOperationException("Cannot get proc address '" + procedureName + "'. Win32 error =" + error);
            }

            return(Marshal.GetDelegateForFunctionPointer(methodHandle.DangerousGetHandle(), delegateType) as TDelegate);
        }
コード例 #3
0
 private static extern MethodHandle GetProcAddress(LibraryHandle library, string procedureName);