Created
April 18, 2025 17:25
-
-
Save Aetopia/b81f510a9fb8483de9342776545c408e to your computer and use it in GitHub Desktop.
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.Runtime.InteropServices; | |
using System.Security; | |
using System.Security.Principal; | |
using Windows.Security.Cryptography.Certificates; | |
[StructLayout(LayoutKind.Sequential)] | |
unsafe readonly ref struct WTS_PROCESS_INFO | |
{ | |
internal readonly int SessionId, ProcessId; | |
internal readonly nint pProcessName, pUserSid; | |
} | |
[ComImport, Guid("660B90C8-73A9-4B58-8CAE-355B7F55341B")] | |
class AppResolver; | |
[Guid("DE25675A-72DE-44B4-9373-05170450C140"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | |
unsafe interface IAppResolver | |
{ | |
void GetAppIDForShortcut(); | |
void GetAppIDForShortcutObject(); | |
void GetAppIDForWindow(nint hWnd, out nint pszAppId, nint pUnknown1 = default, nint pUnknown2 = default, nint pUnknown3 = default); | |
void GetAppIDForProcess(uint dwProcessId, out nint pszAppId, nint pUnknown1 = default, nint pUnknown2 = default, nint pUnknown3 = default); | |
} | |
[SuppressUnmanagedCodeSecurity] | |
unsafe static class Unmanaged | |
{ | |
[DllImport("User32")] | |
static extern nint FindWindowExW(nint hWndParent = default, nint hWndChildAfter = default, nint lpszClass = default, nint lpszWindow = default); | |
[DllImport("Userenv", CharSet = CharSet.Unicode, PreserveSig = default)] | |
static extern void DeriveAppContainerSidFromAppContainerName(string pszAppContainerName, out nint ppsidAppContainerSid); | |
[DllImport("Wtsapi32", CharSet = CharSet.Unicode)] | |
unsafe static extern bool WTSEnumerateProcesses(nint hServer, int Reserved, int Version, out WTS_PROCESS_INFO* ppProcessInfo, out int pCount); | |
[DllImport("Wtsapi32")] | |
unsafe static extern void WTSFreeMemory(void* pMemory); | |
[DllImport("Wtsapi32")] | |
static extern bool EqualSid(nint pSid1, nint pSid2); | |
[DllImport("Kernel32")] | |
static extern nint GetCurrentProcessToken(); | |
[DllImport("Kernel32")] | |
static extern nint OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); | |
[DllImport("Kernel32")] | |
static extern bool OpenProcessToken(nint ProcessHandle, int DesiredAccess, out nint TokenHandle); | |
[DllImport("Kernel32")] | |
static extern nint OpenThread(int dwDesiredAccess, bool bInheritHandle, nint dwThreadId); | |
[DllImport("Kernel32")] | |
static extern void CloseHandle(nint hObject); | |
[DllImport("User32")] | |
static extern int GetWindowThreadProcessId(nint hWnd, out int lpdwProcessId); | |
[DllImport("Kernel32")] | |
static extern int GetApplicationUserModelId(nint hProcess, in int applicationUserModelIdLength = APPLICATION_USER_MODEL_ID_MAX_LENGTH, char* applicationUserModelId = default); | |
[DllImport("Kernel32")] | |
static extern int CompareStringOrdinal(char* lpString1 = default, int cchCount1 = -1, char* lpString2 = default, int cchCount2 = -1, bool bIgnoreCase = default); | |
const int PROCESS_QUERY_LIMITED_INFORMATION = 0x1000; | |
const int TOKEN_QUERY = 0x0008; | |
const int APPLICATION_USER_MODEL_ID_MAX_LENGTH = 130; | |
const int CSTR_EQUAL = 2; | |
internal static void Get() | |
{ | |
WTS_PROCESS_INFO* pProcessInfo = default; | |
try | |
{ | |
WTSEnumerateProcesses(default, default, 1, out pProcessInfo, out var value); | |
} | |
finally { WTSFreeMemory(pProcessInfo); } | |
} | |
const string lpString1 = "Microsoft.MinecraftUWP_8wekyb3d8bbwe!App"; | |
internal static nint Enumerate() | |
{ | |
nint hWnd = default; | |
while ((hWnd = FindWindowExW(hWndChildAfter: hWnd)) != default) | |
{ | |
GetWindowThreadProcessId(hWnd, out var dwProcessId); | |
nint hProcess = default; | |
try | |
{ | |
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, dwProcessId); | |
char* lpString2 = stackalloc char[APPLICATION_USER_MODEL_ID_MAX_LENGTH]; | |
GetApplicationUserModelId(hProcess, applicationUserModelId: lpString2); | |
if (lpString1.Equals(new string(lpString2), StringComparison.OrdinalIgnoreCase)) | |
{ | |
Console.WriteLine("Hello World!"); | |
break; | |
} | |
} | |
finally { CloseHandle(hProcess); } | |
} | |
return hWnd; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment