Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Last active April 18, 2025 18:24
Show Gist options
  • Save Aetopia/51f40ef53da0888d7fe9f7147664c09a to your computer and use it in GitHub Desktop.
Save Aetopia/51f40ef53da0888d7fe9f7147664c09a to your computer and use it in GitHub Desktop.
Check for running UWP apps.
  • Unmanaged.Get(): 7651
  • Unmanaged.GetUnmanaged(): 7309
  • Unmanaged.Enumerate(): 4393
  • Unmanaged.EnumerateUnmanaged(): 4308
using System;
using System.Diagnostics;
static class Developer
{
internal static long Benchmark(string value, Action action)
{
Stopwatch stopwatch = new();
stopwatch.Start(); action(); stopwatch.Stop();
// Console.WriteLine($"{value}, {stopwatch.ElapsedTicks}");
return stopwatch.ElapsedTicks;
}
}
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using Windows.ApplicationModel;
using Windows.Management.Deployment;
using Windows.System;
using Windows.System.Diagnostics;
[SuppressUnmanagedCodeSecurity]
static class Program
{
// static readonly AppDiagnosticInfo Object = AppDiagnosticInfo.RequestInfoForAppAsync("Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe!App").AsTask().Result[default];
static readonly PackageManager PackageManager = new();
[DllImport("Kernel32", CharSet = CharSet.Unicode)]
static extern long GetPackagesByPackageFamily(string packageFamilyName, out int count, nint packageFullNames, out int bufferLength, nint buffer);
// static bool Unmanaged()
// {
// GetPackagesByPackageFamily("Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe", out var value, default, out _, default);
// return value != default;
// }
static void Main()
{
// Developer.Benchmark("PackageManager", () => PackageManager.FindPackagesForUser(string.Empty, "Microsoft.MinecraftWindowsBeta_8wekyb3d8bbwe").Any());
// Developer.Benchmark("GetPackagesByPackageFamily", () => Unmanaged());
// _ = Object;
// Developer.Benchmark("ProcessDiagnosticInfo.GetForProcesses()", () => _ = ProcessDiagnosticInfo.GetForProcesses());
// Developer.Benchmark("Process.GetProcesses()", () => _ = Process.GetProcesses());
//
var count = 1000;
long value = default;
for (int index = default; index < count; index++)
value += Developer.Benchmark("Unmanaged.Get()", () => Unmanaged.Get());
Console.WriteLine($"Unmanaged.Get(): {value / count}");
value = default;
for (int index = default; index < count; index++)
value += Developer.Benchmark("Unmanaged.GetUnmanaged()", () => Unmanaged.GetUnmanaged());
Console.WriteLine($"Unmanaged.GetUnmanaged(): {value /count}");
value = default;
for (int index = default; index < count; index++)
value += Developer.Benchmark("Unmanaged.Enumerate()", () => Unmanaged.Enumerate());
Console.WriteLine($"Unmanaged.Enumerate(): {value /count}");
value = default;
for (int index = default; index < count; index++)
value += Developer.Benchmark("Unmanaged.EnumerateUnmanaged()", () => Unmanaged.EnumerateUnmanaged());
Console.WriteLine($"Unmanaged.EnumerateUnmanaged(): {value / count}");
// Developer.Benchmark("AppDiagnosticInfo", () =>
// {
// foreach (var item in Object.GetResourceGroups())
// if (item.GetProcessDiagnosticInfos().Count != default) return;
// });
}
}
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
{
char* lpString2 = stackalloc char[APPLICATION_USER_MODEL_ID_MAX_LENGTH];
WTSEnumerateProcesses(default, default, 1, out pProcessInfo, out var value);
for (int index = default; index < value; index++)
{
nint hProcess = default;
try
{
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pProcessInfo[index].ProcessId);
_ = GetApplicationUserModelId(hProcess, applicationUserModelId: lpString2);
if (new string(lpString2).Equals(String, StringComparison.OrdinalIgnoreCase)) break;
}
finally { CloseHandle(hProcess); }
}
}
finally { WTSFreeMemory(pProcessInfo); }
}
internal static void GetUnmanaged()
{
fixed (char* lpString1 = String)
{
WTS_PROCESS_INFO* pProcessInfo = default;
try
{
char* lpString2 = stackalloc char[APPLICATION_USER_MODEL_ID_MAX_LENGTH];
WTSEnumerateProcesses(default, default, 1, out pProcessInfo, out var value);
for (int index = default; index < value; index++)
{
nint hProcess = default;
try
{
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pProcessInfo[index].ProcessId);
_ = GetApplicationUserModelId(hProcess, applicationUserModelId: lpString2);
if (CompareStringOrdinal(lpString1: lpString1, lpString2: lpString2, bIgnoreCase: true) == CSTR_EQUAL) break;
}
finally { CloseHandle(hProcess); }
}
}
finally { WTSFreeMemory(pProcessInfo); }
}
}
const string String = "Microsoft.MinecraftUWP_8wekyb3d8bbwe!App";
internal static nint Enumerate()
{
nint hWnd = default;
char* lpString2 = stackalloc char[APPLICATION_USER_MODEL_ID_MAX_LENGTH];
while ((hWnd = FindWindowExW(hWndChildAfter: hWnd)) != default)
{
_ = GetWindowThreadProcessId(hWnd, out var dwProcessId);
nint hProcess = default;
try
{
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, dwProcessId);
_ = GetApplicationUserModelId(hProcess, applicationUserModelId: lpString2);
if (new string(lpString2).Equals(String, StringComparison.OrdinalIgnoreCase)) break;
}
finally { CloseHandle(hProcess); }
}
return hWnd;
}
internal static nint EnumerateUnmanaged()
{
fixed (char* lpString1 = String)
{
nint hWnd = default;
char* lpString2 = stackalloc char[APPLICATION_USER_MODEL_ID_MAX_LENGTH];
while ((hWnd = FindWindowExW(hWndChildAfter: hWnd)) != default)
{
_ = GetWindowThreadProcessId(hWnd, out var dwProcessId);
nint hProcess = default;
try
{
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, dwProcessId);
_ = GetApplicationUserModelId(hProcess, applicationUserModelId: lpString2);
if (CompareStringOrdinal(lpString1: lpString1, lpString2: lpString2, bIgnoreCase: true) == CSTR_EQUAL) break;
}
finally { CloseHandle(hProcess); }
}
return hWnd;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment