示例#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);
        }
示例#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));
     }
 }
示例#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));
        }
示例#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));
        }
示例#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);
        }
示例#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));
        }
示例#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);
        }
示例#8
0
 public static extern bool K32GetModuleInformation(
     SafeProcessHandle hProcess,
     ModuleInstance hModule,
     out MODULEINFO lpmodinfo,
     uint cb);
示例#9
0
 public static extern uint K32GetModuleFileNameExW(
     SafeProcessHandle hProcess,
     ModuleInstance hModule,
     SafeHandle lpFileName,
     uint nSize);
示例#10
0
 public static extern uint GetModuleFileNameW(
     ModuleInstance hModule,
     SafeHandle lpFileName,
     uint nSize);
示例#11
0
 public static extern IntPtr GetProcAddress(
     ModuleInstance hModule,
     [MarshalAs(UnmanagedType.LPStr)] string methodName);