示例#1
0
        /// <summary>
        /// Dynamically lookup a function in the dll via kernel32!GetProcAddress.
        /// </summary>
        /// <param name="functionName">raw name of the function in the export table.</param>
        /// <returns>null if function is not found. Else a delegate to the unmanaged function.</returns>
        /// <remarks>GetProcAddress results are valid as long as the dll is not yet unloaded. This
        /// is very very dangerous to use since you need to ensure that the dll is not unloaded
        /// until after you're done with any objects implemented by the dll. For example, if you
        /// get a delegate that then gets an IUnknown implemented by this dll,
        /// you can not dispose this library until that IUnknown is collected. Else, you may free
        /// the library and then the CLR may call release on that IUnknown and it will crash.</remarks>
        public TDelegate GetUnmanagedFunction <TDelegate>(string functionName) where TDelegate : class
        {
            using (AnsiString str = new AnsiString(functionName))
            {
                IntPtr functionPtr;

                NtStatus result = Win32.LdrGetProcedureAddress(this, str.Buffer, 0, out functionPtr);

                // Failure is a common case, especially for adaptive code.
                if (result.IsError())
                {
                    //result.ReturnException().Log();

                    return(null);
                }

                Delegate function = Marshal.GetDelegateForFunctionPointer(functionPtr, typeof(TDelegate));

                // Ideally, we'd just make the constraint on TDelegate be
                // System.Delegate, but compiler error CS0702 (constrained can't be System.Delegate)
                // prevents that. So we make the constraint system.object and do the cast from object-->TDelegate.
                object o = function;

                return((TDelegate)o);
            }
        }
示例#2
0
        public IntPtr GetProcedure(int procedureNumber)
        {
            IntPtr functionPtr;

            NtStatus result = Win32.LdrGetProcedureAddress(this, IntPtr.Zero, procedureNumber, out functionPtr);

            return(functionPtr);
        }
示例#3
0
        public IntPtr GetProcedure(string procedureName)
        {
            AnsiString str = new AnsiString(procedureName);
            {
                IntPtr functionPtr = IntPtr.Zero;

                NtStatus result = Win32.LdrGetProcedureAddress(this, functionPtr, 0, out functionPtr);

                return(functionPtr);
            }
        }
示例#4
0
        public static IntPtr GetProcedure(IntPtr dllHandle, string procedureName)
        {
            IntPtr handle;

            using (AnsiString str = new AnsiString(procedureName))
            {
                Win32.LdrGetProcedureAddress(dllHandle, str.Buffer, 0, out handle).ThrowIf();
            }

            return(handle);
        }