-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAppUserModelIdService.cs
More file actions
62 lines (53 loc) · 1.71 KB
/
AppUserModelIdService.cs
File metadata and controls
62 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TidyWindow.App.Services;
internal static class AppUserModelIdService
{
// Keep in sync with installer/AppUserModelID entries.
private const string AppUserModelId = "TidyWindow";
public static void EnsureCurrentProcessAppUserModelId()
{
if (!OperatingSystem.IsWindows())
{
return;
}
try
{
var current = TryGetCurrentProcessAppUserModelId();
if (string.Equals(current, AppUserModelId, StringComparison.Ordinal))
{
return;
}
var hr = SetCurrentProcessExplicitAppUserModelID(AppUserModelId);
if (hr < 0)
{
Debug.WriteLine($"SetCurrentProcessExplicitAppUserModelID failed with HRESULT 0x{hr:X8}.");
}
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to set AppUserModelID: {ex}");
}
}
private static string? TryGetCurrentProcessAppUserModelId()
{
var hr = GetCurrentProcessExplicitAppUserModelID(out var rawId);
if (hr != 0 || rawId == IntPtr.Zero)
{
return null;
}
try
{
return Marshal.PtrToStringUni(rawId);
}
finally
{
Marshal.FreeCoTaskMem(rawId);
}
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int SetCurrentProcessExplicitAppUserModelID(string appID);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetCurrentProcessExplicitAppUserModelID(out IntPtr appID);
}