using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; namespace Artemis.Core { /// /// A static class providing extensions /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1060:Move pinvokes to native methods class", Justification = "I don't care, piss off")] public static class ProcessExtensions { /// /// Gets the file name of the given process /// /// The process /// The filename of the given process public static string GetProcessFilename(this Process p) { int capacity = 2000; StringBuilder builder = new(capacity); IntPtr ptr = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, p.Id); if (!QueryFullProcessImageName(ptr, 0, builder, ref capacity)) return string.Empty; return builder.ToString(); } [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] private static extern bool QueryFullProcessImageName([In] IntPtr hProcess, [In] int dwFlags, [Out] StringBuilder lpExeName, ref int lpdwSize); [DllImport("kernel32.dll")] private static extern IntPtr OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, int processId); [Flags] private enum ProcessAccessFlags : uint { QueryLimitedInformation = 0x00001000 } } }