From f2aaa463cdd8691a286136b8262243d4a4a3e1d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 20 Jan 2022 19:25:02 +0100 Subject: [PATCH] feat(injector.cli): allow part of process name in inject command --- .../Commands/InjectCommand.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Local/NosSmooth.Injector.CLI/Commands/InjectCommand.cs b/Local/NosSmooth.Injector.CLI/Commands/InjectCommand.cs index ff7e0353f6d75876f76e5e79319f4da555322622..03f6b9966cd1eff4bfa44c38ce9ac133ae927658 100644 --- a/Local/NosSmooth.Injector.CLI/Commands/InjectCommand.cs +++ b/Local/NosSmooth.Injector.CLI/Commands/InjectCommand.cs @@ -5,6 +5,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.ComponentModel; +using System.Diagnostics; using Remora.Commands.Attributes; using Remora.Commands.Groups; using Remora.Results; @@ -30,7 +31,7 @@ namespace NosSmooth.Injector.CLI.Commands /// /// The command to inject. /// - /// The id of the process. + /// The id of the process or part of its name. /// The path to the dll to inject. /// The full type specifier. Default is LibraryName.DllMain, LibraryName. /// The name of the UnmanagedCallersOnly method. Default is Main. @@ -39,7 +40,7 @@ namespace NosSmooth.Injector.CLI.Commands public Task Inject ( [Description("The id of the process to inject into.")] - int processId, + string process, [Description("The path to the dll to inject.")] string dllPath, [Option('t', "type"), Description("The full type specifier. Default is LibraryName.DllMain, LibraryName")] @@ -48,6 +49,17 @@ namespace NosSmooth.Injector.CLI.Commands string? methodName = null ) { + if (!int.TryParse(process, out var processId)) + { + var foundProcess = Process.GetProcesses().FirstOrDefault(x => x.ProcessName.Contains(process, StringComparison.OrdinalIgnoreCase)); + if (foundProcess is null) + { + return Task.FromResult(Result.FromError(new NotFoundError("Could not find the given process."))); + } + + processId = foundProcess.Id; + } + var dllName = Path.GetFileNameWithoutExtension(dllPath); return Task.FromResult (_injector.Inject(processId, dllPath, $"{dllName}.DllMain, {dllName}", methodName ?? "Main"));