Skip to content

Commit 6fe0322

Browse files
authored
Merge pull request #161 from tinodin/taskbarhelper-nativeaot-compatible
Make TaskbarHelper compatible with NativeAOT
2 parents e8bb8fb + 9e28296 commit 6fe0322

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

dev/DevWinUI/Helpers/TaskbarHelper.cs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace DevWinUI;
1+
using System.Runtime.InteropServices.Marshalling;
2+
3+
namespace DevWinUI;
24

35
public enum TaskbarStates
46
{
@@ -8,12 +10,16 @@ public enum TaskbarStates
810
Error = 0x4,
911
Paused = 0x8
1012
}
13+
1114
public static partial class TaskbarHelper
1215
{
13-
[ComImport()]
16+
private static readonly Guid CLSID_TaskbarList = new("56fdf344-fd6d-11d0-958a-006097c9a090");
17+
private static readonly Guid IID_ITaskbarList3 = new("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf");
18+
1419
[Guid("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
1520
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
16-
private interface ITaskbarList3
21+
[GeneratedComInterface]
22+
internal partial interface ITaskbarList3
1723
{
1824
// ITaskbarList
1925
[PreserveSig]
@@ -33,20 +39,37 @@ private interface ITaskbarList3
3339

3440
// ITaskbarList3
3541
[PreserveSig]
36-
void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
42+
void SetProgressValue(IntPtr hwnd, ulong ullCompleted, ulong ullTotal);
3743
[PreserveSig]
3844
void SetProgressState(IntPtr hwnd, TaskbarStates state);
3945
}
4046

41-
[ComImport()]
42-
[Guid("56fdf344-fd6d-11d0-958a-006097c9a090")]
43-
[ClassInterface(ClassInterfaceType.None)]
44-
private class TaskbarInstance
47+
private static readonly ITaskbarList3 taskbarInstance;
48+
private static readonly bool taskbarSupported = Environment.OSVersion.Version >= new Version(6, 1);
49+
50+
static TaskbarHelper()
4551
{
46-
}
52+
if (taskbarSupported)
53+
{
54+
unsafe
55+
{
56+
void* ppv;
57+
HRESULT hr = PInvoke.CoCreateInstance(
58+
in CLSID_TaskbarList,
59+
null,
60+
Windows.Win32.System.Com.CLSCTX.CLSCTX_INPROC_SERVER,
61+
in IID_ITaskbarList3,
62+
out ppv);
4763

48-
private static ITaskbarList3 taskbarInstance = (ITaskbarList3)new TaskbarInstance();
49-
private static bool taskbarSupported = Environment.OSVersion.Version >= new Version(6, 1);
64+
if (hr.Succeeded && ppv != null)
65+
{
66+
taskbarInstance = (ITaskbarList3)new StrategyBasedComWrappers().GetOrCreateObjectForComInstance((IntPtr)ppv, CreateObjectFlags.None);
67+
Marshal.Release((IntPtr)ppv);
68+
taskbarInstance.HrInit();
69+
}
70+
}
71+
}
72+
}
5073

5174
/// <summary>
5275
/// Sets the progress state of a specified window in the taskbar if supported.

dev/DevWinUI/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ClientToScreen
2+
CoCreateInstance
23
CombineRgn
34
CoWaitForMultipleObjects
45
CreateEllipticRgn

0 commit comments

Comments
 (0)