-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix NativeMarshalling attribute override for COM interfaces #121643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
10
commits into
main
Choose a base branch
from
copilot/fix-com-interface-marshalling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+187
−11
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
31a3982
Initial plan
Copilot 5cd3516
Add NativeMarshalling attribute support for COM interfaces
Copilot 3d67609
Add behavioral tests for NativeMarshalling on COM interfaces
Copilot 1a37329
Simplify UniqueMarshalling native export using StrategyBasedComWrappers
Copilot ba7deb8
Determine which attribute-based marshaller to use based on attribute …
jtschuster 0a56126
Cache COM pointer in native export and update test expectations
Copilot 6027161
Improve test comment clarity
Copilot 88a4db6
Move LibraryImport marshalling test to LibraryImportGenerator.Tests
Copilot 89300a9
Merge branch 'main' into copilot/fix-com-interface-marshalling
jtschuster 61e59f5
Merge branch 'main' into copilot/fix-com-interface-marshalling
AaronRobinsonMSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...time.InteropServices/tests/ComInterfaceGenerator.Tests/NativeMarshallingAttributeTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.InteropServices.Marshalling; | ||
| using SharedTypes.ComInterfaces; | ||
| using Xunit; | ||
|
|
||
| namespace ComInterfaceGenerator.Tests | ||
| { | ||
| public unsafe partial class NativeMarshallingAttributeTests | ||
| { | ||
| [LibraryImport(NativeExportsNE.NativeExportsNE_Binary, EntryPoint = "new_unique_marshalling")] | ||
| internal static partial IUniqueMarshalling NewUniqueMarshalling(); | ||
|
|
||
| [Fact] | ||
| public void GetSameComInterfaceTwiceReturnsUniqueInstances() | ||
| { | ||
| // When using NativeMarshalling with UniqueComInterfaceMarshaller, | ||
| // getting the same COM interface twice should return different managed instances | ||
| var obj1 = NewUniqueMarshalling(); | ||
| var obj2 = NewUniqueMarshalling(); | ||
|
|
||
| Assert.NotSame(obj1, obj2); | ||
|
|
||
| // Verify they work independently | ||
| obj1.SetValue(42); | ||
| obj2.SetValue(100); | ||
|
|
||
| Assert.Equal(42, obj1.GetValue()); | ||
| Assert.Equal(100, obj2.GetValue()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MethodReturningComInterfaceReturnsUniqueInstance() | ||
| { | ||
| // When a COM interface method returns the same interface type, | ||
| // it should return a new managed instance, not the cached one | ||
| var obj = NewUniqueMarshalling(); | ||
| obj.SetValue(42); | ||
|
|
||
| var returnedObj = obj.GetThis(); | ||
|
|
||
| // Should be a different managed object | ||
| Assert.NotSame(obj, returnedObj); | ||
|
|
||
| // But should refer to the same underlying COM object | ||
| Assert.Equal(42, returnedObj.GetValue()); | ||
|
|
||
| // Modifying through one should affect the other | ||
| returnedObj.SetValue(100); | ||
| Assert.Equal(100, obj.GetValue()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...libraries/System.Runtime.InteropServices/tests/Common/ComInterfaces/IUniqueMarshalling.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.InteropServices.Marshalling; | ||
|
|
||
| namespace SharedTypes.ComInterfaces | ||
| { | ||
| [GeneratedComInterface] | ||
| [Guid(IID)] | ||
| [NativeMarshalling(typeof(UniqueComInterfaceMarshaller<IUniqueMarshalling>))] | ||
| internal partial interface IUniqueMarshalling | ||
| { | ||
| int GetValue(); | ||
| void SetValue(int x); | ||
| IUniqueMarshalling GetThis(); | ||
|
|
||
| public const string IID = "E11D5F3E-DD57-4E7E-A78C-F5F8B8E0A1F4"; | ||
| } | ||
|
|
||
| [GeneratedComClass] | ||
| internal partial class UniqueMarshalling : IUniqueMarshalling | ||
| { | ||
| int _data = 0; | ||
| public int GetValue() => _data; | ||
| public void SetValue(int x) => _data = x; | ||
| public IUniqueMarshalling GetThis() => this; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...InteropServices/tests/TestAssets/NativeExports/ComInterfaceGenerator/UniqueMarshalling.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.InteropServices.Marshalling; | ||
| using SharedTypes.ComInterfaces; | ||
|
|
||
| namespace NativeExports.ComInterfaceGenerator | ||
| { | ||
| public static unsafe class UniqueMarshalling | ||
| { | ||
| // Call from another assembly to get a ptr to make an RCW | ||
| [UnmanagedCallersOnly(EntryPoint = "new_unique_marshalling")] | ||
| public static void* CreateComObject() | ||
| { | ||
| StrategyBasedComWrappers wrappers = new(); | ||
| var myObject = new SharedTypes.ComInterfaces.UniqueMarshalling(); | ||
| nint ptr = wrappers.GetOrCreateComInterfaceForObject(myObject, CreateComInterfaceFlags.None); | ||
|
|
||
| return (void*)ptr; | ||
| } | ||
jtschuster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.