Skip to content

Commit 3d024e2

Browse files
Avoid legacy COM paths under NativeAOT
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 50645c8 commit 3d024e2

3 files changed

Lines changed: 24 additions & 34 deletions

File tree

‎src/UniGetUI.Avalonia/Infrastructure/AppShortcutAumidStamper.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Diagnostics.CodeAnalysis;
44
using System.IO;
5+
using System.Runtime.CompilerServices;
56
using System.Runtime.InteropServices;
67
using UniGetUI.Core.Logging;
78

@@ -31,6 +32,12 @@ public static void EnsureStamped()
3132
if (!OperatingSystem.IsWindows())
3233
return;
3334

35+
if (!RuntimeFeature.IsDynamicCodeSupported)
36+
{
37+
Logger.Info("Skipping Start Menu shortcut AUMID stamping because NativeAOT does not support legacy COM activation.");
38+
return;
39+
}
40+
3441
try
3542
{
3643
string? shortcutPath = ResolveStartMenuShortcut();

‎src/UniGetUI.Avalonia/Infrastructure/WindowsAvaloniaRenderingPolicy.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.CompilerServices;
34
using System.Runtime.InteropServices;
45
using System.Runtime.Versioning;
56
using Avalonia.Controls;
@@ -63,6 +64,14 @@ private static bool HasHardwareGpu
6364
Justification = "The DXGI COM interfaces are declared in full and referenced directly here, so their members are preserved by trimming.")]
6465
private static bool DetectHardwareGpu()
6566
{
67+
if (!RuntimeFeature.IsDynamicCodeSupported)
68+
{
69+
Logger.Info(
70+
"Skipping DXGI hardware GPU detection because NativeAOT does not support legacy COM activation."
71+
);
72+
return true;
73+
}
74+
6675
try
6776
{
6877
Guid factoryIid = typeof(IDXGIFactory1).GUID;

‎src/WindowsPackageManager.Interop/WindowsPackageManager/WindowsPackageManagerStandardFactory.cs‎

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Diagnostics.CodeAnalysis;
54
using System.Runtime.InteropServices;
65
using System.Runtime.Versioning;
76
using Windows.Win32.System.Com;
@@ -18,40 +17,8 @@ public WindowsPackageManagerStandardFactory(
1817
)
1918
: base(clsidContext, allowLowerTrustRegistration) { }
2019

21-
[UnconditionalSuppressMessage(
22-
"Trimming",
23-
"IL2072",
24-
Justification = "WinGet COM projected activation types come from the Windows Package Manager WinMD and registered COM server; this path does not depend on app-owned trimmed constructors.")]
2520
protected override T CreateInstance<T>(Guid clsid, Guid iid)
2621
{
27-
if (!_allowLowerTrustRegistration)
28-
{
29-
Type? projectedType = Type.GetTypeFromCLSID(clsid);
30-
if (projectedType is null)
31-
{
32-
throw new WinGetComActivationException(
33-
clsid,
34-
iid,
35-
unchecked((int)0x80040154),
36-
_allowLowerTrustRegistration
37-
);
38-
}
39-
40-
object? activatedInstance = Activator.CreateInstance(projectedType);
41-
if (activatedInstance is null)
42-
{
43-
throw new WinGetComActivationException(
44-
clsid,
45-
iid,
46-
unchecked((int)0x80004003),
47-
_allowLowerTrustRegistration
48-
);
49-
}
50-
51-
IntPtr pointer = Marshal.GetIUnknownForObject(activatedInstance);
52-
return MarshalGeneric<T>.FromAbi(pointer);
53-
}
54-
5522
CLSCTX clsctx = CLSCTX.CLSCTX_LOCAL_SERVER;
5623
if (_allowLowerTrustRegistration)
5724
{
@@ -76,7 +43,14 @@ out IntPtr instance
7643
);
7744
}
7845

79-
return MarshalGeneric<T>.FromAbi(instance);
46+
try
47+
{
48+
return MarshalGeneric<T>.FromAbi(instance);
49+
}
50+
finally
51+
{
52+
Marshal.Release(instance);
53+
}
8054
}
8155

8256
[DllImport(

0 commit comments

Comments
 (0)