public void UnloadAndCloseAssembly(InjectionConfig config, string methodName) { using (_memory = new Memory(ProcessHandle)) { _attach = true; IntPtr image = GetImageFromAssembly(config.AssemblyPointer); Utils.EnsureNotZero(image, "GetImageFromAssembly()"); IntPtr @class = GetClassFromName(image, config.Namespace, config.Class); Utils.EnsureNotZero(@class, "GetClassFromName()"); IntPtr method = GetMethodFromName(@class, methodName); Utils.EnsureNotZero(method, "GetMethodFromName()"); RuntimeInvoke(method); CloseAssembly(config.AssemblyPointer); _attach = false; } }
public void Inject(InjectionConfig cfg) { _config = cfg; using (_memory = new Memory(ProcessHandle)) { if (!GetExports(cfg.Target.MonoModuleAddress, cfg.Target.Process.Is64Bit())) { throw new ApplicationException("Unable to obtain the mono function addresses"); } _rootDomain = GetRootDomain(); Utils.EnsureNotZero(_rootDomain, "GetRootDomain()"); IntPtr rawImage = OpenImageFromData(cfg.Assembly); Utils.EnsureNotZero(rawImage, "OpenImageFromData()"); _attach = true; IntPtr assembly = cfg.AssemblyPointer = OpenAssemblyFromImage(rawImage); Utils.EnsureNotZero(assembly, "OpenAssemblyFromImage()"); IntPtr image = GetImageFromAssembly(assembly); Utils.EnsureNotZero(image, "GetImageFromAssembly()"); IntPtr @class = GetClassFromName(image, cfg.Namespace, cfg.Class); Utils.EnsureNotZero(@class, "GetClassFromName()"); IntPtr method = GetMethodFromName(@class, cfg.Method); Utils.EnsureNotZero(method, "GetMethodFromName()"); bool result = RuntimeInvoke(method); _attach = false; if (!result) { throw new ApplicationException($"The invocation of {cfg.Class}.{cfg.Method} failed"); } } }