Binarysharp.MemoryManagement.Modules.ModuleCore.LoadLibrary C# (CSharp) Méthode

LoadLibrary() public static méthode

Loads the specified module into the address space of the calling process.
public static LoadLibrary ( string libraryPath ) : ProcessModule
libraryPath string The name of the module. This can be either a library module (a .dll file) or an executable module (an .exe file).
Résultat System.Diagnostics.ProcessModule
        public static ProcessModule LoadLibrary(string libraryPath)
        {
            // Check whether the file exists
            if(!File.Exists(libraryPath))
                throw new FileNotFoundException(string.Format("Couldn't load the library {0} because the file doesn't exist.", libraryPath));

            // Load the library
            if(NativeMethods.LoadLibrary(libraryPath) == IntPtr.Zero)
                throw new Win32Exception(string.Format("Couldn't load the library {0}.", libraryPath));

            // Enumerate the loaded modules and return the one newly added
            return Process.GetCurrentProcess().Modules.Cast<ProcessModule>().First(m => m.FileName == libraryPath);
        }

Usage Example

Exemple #1
0
        /// <summary>
        ///     Finds the specified function in the remote module.
        /// </summary>
        /// <param name="functionName">The name of the function (case sensitive).</param>
        /// <returns>A new instance of a <see cref="RemoteFunction" /> class.</returns>
        /// <remarks>
        ///     Interesting article on how DLL loading works: http://msdn.microsoft.com/en-us/magazine/bb985014.aspx
        /// </remarks>
        public RemoteFunction FindFunction(string functionName)
        {
            // Create the tuple
            var tuple = Tuple.Create(functionName, MemorySharp.Handle);

            // Check if the function is already cached
            if (CachedFunctions.ContainsKey(tuple))
            {
                return(CachedFunctions[tuple]);
            }

            // If the function is not cached
            // Check if the local process has this module loaded
            var localModule =
                Process.GetCurrentProcess()
                .Modules.Cast <ProcessModule>()
                .FirstOrDefault(m => string.Equals(m.FileName, Path, StringComparison.CurrentCultureIgnoreCase));
            var isManuallyLoaded = false;

            try
            {
                // If this is not the case, load the module inside the local process
                if (localModule == null)
                {
                    isManuallyLoaded = true;
                    localModule      = ModuleCore.LoadLibrary(Native.FileName);
                }

                // Get the offset of the function
                var offset = ModuleCore.GetProcAddress(localModule, functionName).ToInt64() -
                             localModule.BaseAddress.ToInt64();

                // Rebase the function with the remote module
                var function = new RemoteFunction(MemorySharp, new IntPtr(Native.BaseAddress.ToInt64() + offset),
                                                  functionName);

                // Store the function in the cache
                CachedFunctions.Add(tuple, function);

                // Return the function rebased with the remote module
                return(function);
            }
            finally
            {
                // Free the module if it was manually loaded
                if (isManuallyLoaded)
                {
                    ModuleCore.FreeLibrary(localModule);
                }
            }
        }