예제 #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);