using System.Runtime.InteropServices;
namespace EmbyToolbox.Interop;
/// Вызов SetCurrentProcessExplicitAppUserModelID до показа окон/unpackaged toasts Windows.
internal static class AppUserModelIdRegistration
{
[DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
private static extern int SetCurrentProcessExplicitAppUserModelID(string appID);
public static string? LastRegisteredId { get; private set; }
public static int LastHr { get; private set; }
public static string? LastDiagnostics { get; private set; }
/// true, если получен код 0 (S_OK).
public static bool TryRegister(string appUserModelId)
{
LastDiagnostics = null;
if (!OperatingSystem.IsWindowsVersionAtLeast(6, 1))
{
LastDiagnostics = "требуется Windows.";
LastHr = -1;
return false;
}
try
{
var hr = SetCurrentProcessExplicitAppUserModelID(appUserModelId);
LastHr = hr;
LastRegisteredId = hr == 0 ? appUserModelId : null;
if (hr != 0)
{
LastDiagnostics =
$"SetCurrentProcessExplicitAppUserModelID вернул 0x{hr:X8} для «{appUserModelId}».";
return false;
}
return true;
}
catch (Exception ex)
{
LastHr = -2;
LastDiagnostics = $"{ex.GetType().Name}: {ex.Message}";
LastRegisteredId = null;
return false;
}
}
}