Skip to content
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

Only Unwrap If Given IDataObject #12738

Merged
merged 1 commit into from
Jan 10, 2025
Merged

Conversation

lonitra
Copy link
Member

@lonitra lonitra commented Jan 9, 2025

Fixes #12665

In .NET 9, we had switched to leveraging cswin32 and ComWrappers for our OLE interop. During this we discovered that switching from built-in COM to ComWrappers took away the ability to automatically cast native pointers back to their original managed object when round tripping through native code. In order to remedy this, we updated to wrap the data on clipboard set and unwrap the object retrieved from the clipboard manually before returning it to get the original data.

While doing this, we had missed the fact that the native IDataObject interface doesn't have the concept of explicit autoConvert. Meaning that when calling our Clipboard APIs with autoConvert = false, it will effectively be ignored and produce same results as autoConvert = true if we're calling to a native IDataObject interface. In .NET 9, since we always unwrap the object retrieved from the clipboard to get our completely managed DataObject, we lost this behavior for types that don't already derive from managed IDataObject.

To fix this, the change here is that when we wrap types that don't already derive from the managed IDataObject, we won't automatically unwrap to the wrapping DataObject we created.

Changes also includes adding more tests to capture behavior and minor optimization with not wrapping twice

Microsoft Reviewers: Open in CodeFlow

@JeremyKuhne
Copy link
Member

It isn't COM giving us back the other data types in this case. It is our own implementation. The native IDataObject interface doesn't have the concept of explicit autoConvert. What this means is if you call our managed Clipboard APIs with autoConvert = false (directly or indirectly) it will be ignored if we're calling to a native IDataObject interface (it is just assumed to be true).

Switching to ComWrappers from built-in COM took away the ability to automatically cast native pointers back to their original managed object when they are round tripped through native code. To work around this, we were unwrapping the original object manually before returning it. We missed this subtlety for autoConvert when doing this. Using ComWrappers is a necessity for enabling trimming and AOT (which we don't fully support yet).

The mitigation here is when we're wrapping arbitrary types (objects that don't already derive from the managed IDataObject), we won't automatically unwrap to the wrapping DataObject we created.

@lonitra lonitra marked this pull request as ready for review January 9, 2025 21:49
@lonitra lonitra requested a review from a team as a code owner January 9, 2025 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clipboard behaves different between net8 and net9
3 participants