Beispiel #1
0
            public static Delegate LoadFunc(INativeLibImporter importer, IntPtr libraryHandle, string entryPoint, Type delegateType)
            {
                IntPtr procAddress = importer.GetProcAddress(libraryHandle, entryPoint);

                if (procAddress == IntPtr.Zero)
                {
                    throw new EntryPointNotFoundException(string.Format("Unable to get address of {0} ({1})", entryPoint, delegateType));
                }
                return(Marshal.GetDelegateForFunctionPointer(procAddress, delegateType));
            }
            public static T LoadFunc <T>(INativeLibImporter importer, IntPtr libraryHandle, string entryPoint)
            {
                IntPtr procAddress = importer.GetProcAddress(libraryHandle, entryPoint);

                if (procAddress == IntPtr.Zero)
                {
                    throw new NativeLoadException(string.Format("Unable to get address of {0} ({1})", entryPoint, typeof(T)), null);
                }
                return(CurrentFramework.GetDelegateForFunctionPointer <T>(procAddress));
            }
        static object GetDelegate(INativeLibImporter importer, IntPtr lib, string entryPoint, Type delegateType)
        {
            IntPtr procAddress = importer.GetProcAddress(lib, entryPoint);

            if (procAddress == IntPtr.Zero)
            {
                return(null);
            }
            var method = typeof(CurrentFramework).GetTypeInfo().GetMethod(nameof(CurrentFramework.GetDelegateForFunctionPointer)).MakeGenericMethod(delegateType);

            return(method.Invoke(null, new object[] { procAddress }));
        }
Beispiel #4
0
        static object GetDelegate(INativeLibImporter importer, IntPtr lib, string entryPoint, Type delegateType)
        {
            IntPtr procAddress = importer.GetProcAddress(lib, entryPoint);

            if (procAddress == IntPtr.Zero)
            {
                var    invokeMethod  = delegateType.GetTypeInfo().GetMethod("Invoke");
                var    parameters    = invokeMethod.GetParameters().Select(p => Expression.Parameter(p.ParameterType)).ToArray();
                var    returnType    = invokeMethod.ReturnType;
                var    errorMessage  = string.Format("Unable to get address of {0} ({1})", entryPoint, delegateType);
                Action throwAction   = () => throw new NativeLoadException(errorMessage, null);
                var    callThrowExpr = Expression.Constant(throwAction, typeof(Action));
                var    defaultExpr   = Expression.Default(returnType);
                var    block         = Expression.Block(returnType, Expression.Invoke(callThrowExpr), defaultExpr);
                var    lambda        = Expression.Lambda(delegateType, block, parameters);
                return(lambda.Compile());
            }
            var method = typeof(CurrentFramework).GetTypeInfo().GetMethod(nameof(CurrentFramework.GetDelegateForFunctionPointer)).MakeGenericMethod(delegateType);

            return(method.Invoke(null, new object[] { procAddress }));
        }