Skip to content

Commit 8e3d136

Browse files
committed
Add a COMTest to verify one of the scenarios that we couldn't get to work previously with interop between CsWin32 and C#/WinRT.
1 parent c2f7b57 commit 8e3d136

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

src/CsWin32Generator/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public partial class Program
2525
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
2626
};
2727

28-
private static readonly char[] ZeroWhiteSpace = new char[]
29-
{
28+
private static ReadOnlySpan<char> ZeroWhiteSpace =>
29+
[
3030
'\uFEFF', // ZERO WIDTH NO-BREAK SPACE (U+FEFF)
3131
'\u200B', // ZERO WIDTH SPACE (U+200B)
32-
};
32+
];
3333

3434
/// <summary>
3535
/// Entry point for the command line application.
@@ -362,10 +362,10 @@ private static GeneratorOptions LoadGeneratorOptions(FileInfo? nativeMethodsJson
362362
// Determine language version based on target framework
363363
LanguageVersion languageVersion = targetFramework switch
364364
{
365-
var tf when tf?.StartsWith("net9.0", StringComparison.Ordinal) == true => LanguageVersion.Latest,
366-
var tf when tf?.StartsWith("net8.0", StringComparison.Ordinal) == true => LanguageVersion.Latest,
367-
var tf when tf?.StartsWith("net7.0", StringComparison.Ordinal) == true => LanguageVersion.Latest,
368-
var tf when tf?.StartsWith("net6.0", StringComparison.Ordinal) == true => LanguageVersion.CSharp9,
365+
var tfm when tfm?.StartsWith("net9.0", StringComparison.Ordinal) == true => LanguageVersion.Latest,
366+
var tfm when tfm?.StartsWith("net8.0", StringComparison.Ordinal) == true => LanguageVersion.Latest,
367+
var tfm when tfm?.StartsWith("net7.0", StringComparison.Ordinal) == true => LanguageVersion.Latest,
368+
var tfm when tfm?.StartsWith("net6.0", StringComparison.Ordinal) == true => LanguageVersion.CSharp9,
369369
_ => LanguageVersion.CSharp9,
370370
};
371371

test/CsWin32Generator.Tests/CsWin32GeneratorFullTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public CsWin32GeneratorFullTests(ITestOutputHelper logger)
1313
}
1414

1515
[Fact]
16+
[Trait("TestCategory", "FailsInCloudTest")] // these take ~4GB of memory to run.
1617
public async Task FullGeneration()
1718
{
1819
this.fullGeneration = true;

test/GenerationSandbox.BuildTask.Tests/COMTests.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,57 @@
88
using System.Runtime.InteropServices;
99
using System.Runtime.InteropServices.Marshalling;
1010
using System.Runtime.Versioning;
11+
using System.Threading.Tasks;
1112
using Windows.System;
13+
using Windows.UI.Composition;
1214
using Windows.Win32;
1315
using Windows.Win32.Foundation;
16+
using Windows.Win32.Graphics.Direct3D;
17+
using Windows.Win32.Graphics.Direct3D11;
1418
using Windows.Win32.System.Com;
19+
using Windows.Win32.System.WinRT.Composition;
1520

1621
public partial class COMTests
1722
{
1823
[Fact]
19-
public void Placeholder()
24+
public async Task CanInteropWithICompositorInterop()
2025
{
21-
}
26+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
27+
{
28+
var controller = DispatcherQueueController.CreateOnDedicatedThread();
29+
TaskCompletionSource tcs = new();
30+
controller.DispatcherQueue.TryEnqueue(() =>
31+
{
32+
try
33+
{
34+
unsafe // Optional parameters use unsafe
35+
{
36+
Compositor compositor = new();
2237

23-
[LibraryImport("blah")]
24-
internal static unsafe partial void Test(int count, [In][MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] int[] ppBuffer);
38+
PInvoke.D3D11CreateDevice(
39+
null,
40+
D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE,
41+
HINSTANCE.Null,
42+
D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT,
43+
null,
44+
0,
45+
PInvoke.D3D11_SDK_VERSION,
46+
out var device,
47+
null,
48+
out var deviceContext);
2549

26-
//[LibraryImport("blah.dll")]
27-
//internal static unsafe partial void TestOptional(out Nullable<int*> ppBuffer);
50+
var interop = (ICompositorInterop)(object)compositor;
51+
interop.CreateGraphicsDevice(device, out var graphicsDevice);
52+
tcs.SetResult();
53+
}
54+
}
55+
catch (Exception ex)
56+
{
57+
tcs.SetException(ex);
58+
}
59+
});
2860

29-
//unsafe void CreateBuffer(winmdroot.Graphics.Direct3D11.D3D11_BUFFER_DESC* pDesc, [Optional] winmdroot.Graphics.Direct3D11.D3D11_SUBRESOURCE_DATA* pInitialData, [Optional] winmdroot.Graphics.Direct3D11.ID3D11Buffer_unmanaged** ppBuffer);
61+
await tcs.Task;
62+
}
63+
}
3064
}

test/GenerationSandbox.BuildTask.Tests/NativeMethods.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ ChoosePixelFormat
3737
//ID2D1Factory4
3838
//ID3D11VideoContext
3939
RoCreatePropertySetSerializer
40-
RoRegisterActivationFactories
40+
RoRegisterActivationFactories
41+
D3D11CreateDevice
42+
ICompositorInterop
43+
D3D11_SDK_VERSION

0 commit comments

Comments
 (0)