- Processes, 00:00:00.0000342
- Unmanaged_Fixed_Buffers_Processes, 00:00:00.0000328
- Managed, 00:00:00.0000308
- Unmanaged, 00:00:00.0000318
- Unmanaged_Fixed_Buffers, 00:00:00.0000314
Last active
April 23, 2025 18:36
-
-
Save Aetopia/81dfd7fbfe9cecb90dc8ccd36fc9c270 to your computer and use it in GitHub Desktop.
Process Enumerator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Diagnostics; | |
| using System.Collections.Generic; | |
| using System.Runtime.InteropServices; | |
| using static Constants; | |
| using static Native; | |
| static class Constants | |
| { | |
| internal const int PROCESS_QUERY_LIMITED_INFORMATION = 0x1000; | |
| internal const int APPLICATION_USER_MODEL_ID_MAX_LENGTH = 130; | |
| internal const int CSTR_EQUAL = 2; | |
| } | |
| static class Native | |
| { | |
| [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)] | |
| internal static extern bool GetApplicationUserModelId(nint hProcess, in int applicationUserModelIdLength = APPLICATION_USER_MODEL_ID_MAX_LENGTH, in APPLICATION_USER_MODEL_ID applicationUserModelId = default); | |
| [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)] | |
| internal static extern bool GetApplicationUserModelId(nint hProcess, in int applicationUserModelIdLength = Constants.APPLICATION_USER_MODEL_ID_MAX_LENGTH, nint applicationUserModelId = default); | |
| [DllImport("User32", ExactSpelling = true, EntryPoint = "FindWindowExW", SetLastError = true, CharSet = CharSet.Unicode)] | |
| internal static extern nint FindWindowEx(nint hWndParent = default, nint hWndChildAfter = default, string lpszClass = "MSCTFIME UI", string lpszWindow = default); | |
| [DllImport("Kernel32", SetLastError = true, ExactSpelling = true)] | |
| internal static extern nint OpenProcess(int dwDesiredAccess = Constants.PROCESS_QUERY_LIMITED_INFORMATION, bool bInheritHandle = default, int dwProcessId = default); | |
| [DllImport("Kernel32", SetLastError = true, ExactSpelling = true)] | |
| internal static extern void CloseHandle(nint hObject); | |
| [DllImport("User32", ExactSpelling = true, SetLastError = true)] | |
| internal static extern void GetWindowThreadProcessId(nint hWnd, out int lpdwProcessId); | |
| [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)] | |
| internal static extern int CompareStringOrdinal(nint lpString1 = default, int cchCount1 = -1, nint lpString2 = default, int cchCount2 = -1, bool bIgnoreCase = default); | |
| [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)] | |
| internal static extern int CompareStringOrdinal(in APPLICATION_USER_MODEL_ID lpString1 = default, int cchCount1 = -1, nint lpString2 = default, int cchCount2 = -1, bool bIgnoreCase = default); | |
| } | |
| [StructLayout(LayoutKind.Sequential, Size = sizeof(char) * APPLICATION_USER_MODEL_ID_MAX_LENGTH)] | |
| readonly struct APPLICATION_USER_MODEL_ID; | |
| static class Class | |
| { | |
| internal static readonly nint Address = GCHandle.Alloc("Microsoft.MinecraftUWP_8wekyb3d8bbwe!App", GCHandleType.Pinned).AddrOfPinnedObject(); | |
| internal static IEnumerable<nint> Unmanaged_Fixed_Buffers_Processes | |
| { | |
| get | |
| { | |
| nint hWnd = default, hProcess = default; | |
| APPLICATION_USER_MODEL_ID @this = new(); | |
| while ((hWnd = FindWindowEx(hWndChildAfter: hWnd)) != default) | |
| { | |
| GetWindowThreadProcessId(hWnd, out var dwProcessId); | |
| try | |
| { | |
| hProcess = OpenProcess(dwProcessId: dwProcessId); | |
| if (GetApplicationUserModelId(hProcess, applicationUserModelId: @this)) continue; | |
| else if (CompareStringOrdinal(@this, lpString2: Address, bIgnoreCase: true) == CSTR_EQUAL) yield return dwProcessId; | |
| } | |
| finally { CloseHandle(hProcess); } | |
| } | |
| } | |
| } | |
| internal static void Unmanaged_Fixed_Buffers() | |
| { | |
| nint hWnd = default, hProcess = default; | |
| APPLICATION_USER_MODEL_ID @this = new(); | |
| while ((hWnd = FindWindowEx(hWndChildAfter: hWnd)) != default) | |
| { | |
| GetWindowThreadProcessId(hWnd, out var dwProcessId); | |
| try | |
| { | |
| if (GetApplicationUserModelId(hProcess = OpenProcess(dwProcessId: dwProcessId), applicationUserModelId: @this)) continue; | |
| else if (CompareStringOrdinal(@this, lpString2: Address, bIgnoreCase: true) == CSTR_EQUAL) break; | |
| } | |
| finally { CloseHandle(hProcess); } | |
| } | |
| } | |
| internal static IEnumerable<int> Processes | |
| { | |
| get | |
| { | |
| nint hWnd = default, hProcess = default; | |
| var @this = Marshal.AllocHGlobal(sizeof(char) * APPLICATION_USER_MODEL_ID_MAX_LENGTH); | |
| try | |
| { | |
| while ((hWnd = FindWindowEx(hWndChildAfter: hWnd)) != default) | |
| { | |
| GetWindowThreadProcessId(hWnd, out var dwProcessId); | |
| try | |
| { | |
| if (GetApplicationUserModelId(hProcess = OpenProcess(dwProcessId: dwProcessId), applicationUserModelId: @this)) continue; | |
| else if (CompareStringOrdinal(@this, lpString2: Address, bIgnoreCase: true) == CSTR_EQUAL) yield return dwProcessId; | |
| } | |
| finally { CloseHandle(hProcess); } | |
| } | |
| } | |
| finally { Marshal.FreeHGlobal(@this); } | |
| } | |
| } | |
| internal static void Managed() | |
| { | |
| nint hWnd = default, hProcess = default; | |
| var @this = Marshal.AllocHGlobal(sizeof(char) * APPLICATION_USER_MODEL_ID_MAX_LENGTH); | |
| try | |
| { | |
| while ((hWnd = FindWindowEx(hWndChildAfter: hWnd)) != default) | |
| { | |
| GetWindowThreadProcessId(hWnd, out var dwProcessId); | |
| try | |
| { | |
| if (GetApplicationUserModelId(hProcess = OpenProcess(dwProcessId: dwProcessId), applicationUserModelId: @this)) continue; | |
| else if (CompareStringOrdinal(@this, lpString2: Address, bIgnoreCase: true) == CSTR_EQUAL) break; | |
| } | |
| finally { CloseHandle(hProcess); } | |
| } | |
| } | |
| finally { Marshal.FreeHGlobal(@this); } | |
| } | |
| internal unsafe static void Unmanaged() | |
| { | |
| nint hWnd = default, hProcess = default; | |
| var @this = stackalloc char[APPLICATION_USER_MODEL_ID_MAX_LENGTH]; | |
| while ((hWnd = FindWindowEx(hWndChildAfter: hWnd)) != default) | |
| { | |
| GetWindowThreadProcessId(hWnd, out var dwProcessId); | |
| try | |
| { | |
| if (GetApplicationUserModelId(hProcess = OpenProcess(dwProcessId: dwProcessId), applicationUserModelId: (nint)@this)) continue; | |
| else if (CompareStringOrdinal((nint)@this, lpString2: Address, bIgnoreCase: true) == CSTR_EQUAL) break; | |
| } | |
| finally { CloseHandle(hProcess); } | |
| } | |
| } | |
| } | |
| static class Benchmark | |
| { | |
| internal static void Run(string value, Action action) | |
| { | |
| Stopwatch stopwatch = new(); | |
| long @this = default, @object = 1000; | |
| for (int index = default; index < @object; index++) | |
| { | |
| try | |
| { | |
| stopwatch.Start(); | |
| action(); | |
| stopwatch.Stop(); | |
| } | |
| finally { @this += stopwatch.ElapsedTicks; stopwatch.Reset(); } | |
| } | |
| Console.WriteLine($"{value}, {TimeSpan.FromTicks(@this / @object)}"); | |
| } | |
| } | |
| static class Program | |
| { | |
| static void Main() | |
| { | |
| Benchmark.Run(nameof(Class.Processes), () => Class.Processes.Any()); | |
| Benchmark.Run(nameof(Class.Unmanaged_Fixed_Buffers_Processes), () => Class.Unmanaged_Fixed_Buffers_Processes.Any()); | |
| Benchmark.Run(nameof(Class.Managed), Class.Managed); | |
| Benchmark.Run(nameof(Class.Unmanaged), Class.Unmanaged); | |
| Benchmark.Run(nameof(Class.Unmanaged_Fixed_Buffers), Class.Unmanaged_Fixed_Buffers); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment