GetModuleHandle() private method

private GetModuleHandle ( String InPath ) : IntPtr
InPath String
return System.IntPtr
Ejemplo n.º 1
0
        /// <summary>
        /// Will return the address for a given DLL export symbol. The specified
        /// module has to be loaded into the current process space and also export
        /// the given method.
        /// </summary>
        /// <remarks>
        /// If you wonder how to get native entry points in a managed environment,
        /// this is the anwser. You will only be able to hook native code from a managed
        /// environment if you have access to a method like this, returning the native
        /// entry point. Please note that you will also hook any managed code, which
        /// of course ultimately relies on the native windows API!
        /// </remarks>
        /// <param name="InModule">A system DLL name like "kernel32.dll" or a full qualified path to any DLL.</param>
        /// <param name="InSymbolName">An exported symbol name like "CreateFileW".</param>
        /// <returns>The entry point for the given API method.</returns>
        /// <exception cref="DllNotFoundException">
        /// The given module is not loaded into the current process.
        /// </exception>
        /// <exception cref="MissingMethodException">
        /// The given module does not export the desired method.
        /// </exception>
        public static IntPtr GetProcAddress(
            String InModule,
            String InSymbolName)
        {
            IntPtr Module = NativeAPI.GetModuleHandle(InModule);

            if (Module == IntPtr.Zero)
            {
                throw new DllNotFoundException("The given library is not loaded into the current process.");
            }

            IntPtr Method = NativeAPI.GetProcAddress(Module, InSymbolName);

            if (Method == IntPtr.Zero)
            {
                throw new MissingMethodException("The given method does not exist.");
            }

            return(Method);
        }