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