Ejemplo n.º 1
0
        /// <summary>
        ///  Gets info for the given module in the given process.
        /// </summary>
        /// <param name="process">The process for the given module or null for the current process.</param>
        /// <remarks>External process handles must be opened with PROCESS_QUERY_INFORMATION|PROCESS_VM_READ</remarks>
        public static unsafe ModuleInfo GetModuleInfo(ModuleInstance module, SafeProcessHandle?process = null)
        {
            Error.ThrowLastErrorIfFalse(
                Imports.K32GetModuleInformation(process ?? Processes.GetCurrentProcess(), module, out var info, (uint)sizeof(ModuleInfo)));

            return(info);
        }
Ejemplo n.º 2
0
 /// <summary>
 ///  Gets the file name (path) for the given module handle in the given process.
 /// </summary>
 /// <param name="process">The process for the given module or null for the current process.</param>
 /// <remarks>External process handles must be opened with PROCESS_QUERY_INFORMATION|PROCESS_VM_READ</remarks>
 public static unsafe string GetModuleFileName(ModuleInstance module, SafeProcessHandle?process = null)
 {
     if (process == null)
     {
         return(PlatformInvoke.GrowableBufferInvoke(
                    (ref ValueBuffer <char> buffer) =>
         {
             fixed(char *b = buffer)
             {
                 return Imports.GetModuleFileNameW(module, b, buffer.Length);
             }
         },
                    ReturnSizeSemantics.BufferTruncates
                    | ReturnSizeSemantics.LastErrorInsufficientBuffer
                    | ReturnSizeSemantics.SizeIncludesNullWhenTruncated));
     }
     else
     {
         return(PlatformInvoke.GrowableBufferInvoke(
                    (ref ValueBuffer <char> buffer) =>
         {
             fixed(char *b = buffer)
             {
                 return Imports.K32GetModuleFileNameExW(process, module, b, buffer.Length);
             }
         },
                    ReturnSizeSemantics.BufferTruncates));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the file name (path) for the given module handle in the given process.
        /// </summary>
        /// <param name="process">The process for the given module or null for the current process.</param>
        /// <remarks>External process handles must be opened with PROCESS_QUERY_INFORMATION|PROCESS_VM_READ</remarks>
        public static string GetModuleFileName(ModuleInstance module, SafeProcessHandle process = null)
        {
            var wrapper = new ModuleFileNameWrapper {
                Module = module, Process = process
            };

            return(BufferHelper.TruncatingApiInvoke(ref wrapper));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get a delegate for the given native method
        /// </summary>
        /// <remarks>
        /// Here is a sample delegate definition:
        ///
        ///     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        ///     private delegate int DoubleDelegate(int value);
        ///
        /// And it's native signature:
        ///
        ///     extern "C" __declspec (dllexport) int Double(int);
        /// </remarks>
        public static DelegateType GetFunctionDelegate <DelegateType>(ModuleInstance library, string methodName)
        {
            IntPtr method = Imports.GetProcAddress(library, methodName);

            if (method == IntPtr.Zero)
            {
                throw Error.GetExceptionForLastError(methodName);
            }

            return(Marshal.GetDelegateForFunctionPointer <DelegateType>(method));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Load the library at the given path.
        /// </summary>
        public static ModuleInstance LoadLibrary(string path, LoadLibraryFlags flags)
        {
            ModuleInstance handle = Imports.LoadLibraryExW(path, IntPtr.Zero, flags);

            if (handle.IsInvalid)
            {
                throw Error.GetExceptionForLastError(path);
            }

            return(handle);
        }
Ejemplo n.º 6
0
        /// <summary>
        ///  Get a delegate for the given native method
        /// </summary>
        /// <remarks>
        ///  Here is a sample delegate definition:
        ///
        ///     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        ///     private delegate int DoubleDelegate(int value);
        ///
        ///  And it's native signature:
        ///
        ///     extern "C" __declspec (dllexport) int Double(int);
        /// </remarks>
        public static TDelegate GetFunctionDelegate <TDelegate>(ModuleInstance library, string methodName)
        {
            IntPtr method = Imports.GetProcAddress(library, methodName);

            if (method == IntPtr.Zero)
            {
                Error.GetLastError().Throw(methodName);
            }

            return(Marshal.GetDelegateForFunctionPointer <TDelegate>(method));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets info for the given module in the given process.
        /// </summary>
        /// <param name="process">The process for the given module or null for the current process.</param>
        /// <remarks>External process handles must be opened with PROCESS_QUERY_INFORMATION|PROCESS_VM_READ</remarks>
        public unsafe static ModuleInfo GetModuleInfo(ModuleInstance module, SafeProcessHandle process = null)
        {
            if (process == null)
            {
                process = Processes.GetCurrentProcess();
            }

            if (!Imports.K32GetModuleInformation(process, module, out var info, (uint)sizeof(ModuleInfo)))
            {
                throw Error.GetExceptionForLastError();
            }

            return(info);
        }
Ejemplo n.º 8
0
 public static extern bool K32GetModuleInformation(
     SafeProcessHandle hProcess,
     ModuleInstance hModule,
     out MODULEINFO lpmodinfo,
     uint cb);
Ejemplo n.º 9
0
 public static extern uint K32GetModuleFileNameExW(
     SafeProcessHandle hProcess,
     ModuleInstance hModule,
     SafeHandle lpFileName,
     uint nSize);
Ejemplo n.º 10
0
 public static extern uint GetModuleFileNameW(
     ModuleInstance hModule,
     SafeHandle lpFileName,
     uint nSize);
Ejemplo n.º 11
0
 public static extern IntPtr GetProcAddress(
     ModuleInstance hModule,
     [MarshalAs(UnmanagedType.LPStr)] string methodName);