Repository navigation
Conversation
|
@RussKie can I ask you start looking into this masterpiece. |
|
I’ll put aside some time next week.
|
| if (dataObject is IDataObject ido && !Marshal.IsComObject(dataObject)) | ||
| { | ||
| return ido; | ||
| } | ||
|
|
There was a problem hiding this comment.
Could you please explain this removal?
There was a problem hiding this comment.
This is effectively dead code after ComVisible removal. My current plan is provide for DataObject CCW's which supports 2 interfaces IComDataObject and IWinFormsDataObject which resurrect code in some form.
dataObject is always DataObjectWrapper here which implements only IComDataObject for now. And previously we have _ComObject which react just to IComDataObject, that why @filipnavara create issue in first place.
|
|
||
| internal IntPtr Instance => _wrappedInstance; | ||
|
|
||
| public void Dispose() |
There was a problem hiding this comment.
How is this called? I didn't see any direct callers.
There was a problem hiding this comment.
I add finalizer after this comment. Also I leave this method for consistency with other wrappers.
There was a problem hiding this comment.
I don't think the Dispose pattern shouldn't be used in this case. Consistency isn't the priority here, but rather correctness. See the note under https://docs.microsoft.com/dotnet/standard/native-interop/tutorial-comwrappers#step-4--handle-native-object-wrapper-lifetime-details.
There was a problem hiding this comment.
I can attribute my ignorance to the fact that there now documentation when I start 😉 , but thank for keeping me responsible.
There was a problem hiding this comment.
And crap. I add finalizer in different place. In the DataObject....
|
Looks good. I've run some local manual tests too. @AaronRobinsonMSFT @JeremyKuhne I'd appreciate it if you could review as well. |
| { | ||
| [DllImport(Libraries.Ole32, ExactSpelling = true)] | ||
| public static extern HRESULT OleSetClipboard(IDataObject? pDataObj); | ||
| private static extern HRESULT OleSetClipboard(IntPtr pDataObj); |
There was a problem hiding this comment.
Entirely up to the WinForms owners, @RussKie and @JeremyKuhne, but this could be declared within the method body below as a static local function.
There was a problem hiding this comment.
As in like this?
| private static extern HRESULT OleSetClipboard(IntPtr pDataObj); | |
| public static HRESULT OleSetClipboard(IDataObject? pDataObj) | |
| { | |
| if (pDataObj is null) | |
| { | |
| return OleSetClipboard(IntPtr.Zero); | |
| } | |
| return OleSetClipboard(WinFormsComWrappers.Instance.GetComPointer(pDataObj, IID.IDataObject)); | |
| [DllImport(Libraries.Ole32, ExactSpelling = true)] | |
| static extern HRESULT OleSetClipboard(IntPtr pDataObj); | |
| } |
There was a problem hiding this comment.
I think that's what Aaron means. Do you want me apply this change?
There was a problem hiding this comment.
As in like this?
Yep.
There was a problem hiding this comment.
How will it play if we switch to LibraryImport later on? Will source gen produce code for this?
There was a problem hiding this comment.
I guess we'll cross that bridge when we get to it :)
There was a problem hiding this comment.
Isn’t #7172 practically done. And it will land definitely faster then this one. Am I miss something?
There was a problem hiding this comment.
Judging by the @JeremyKuhne's comments there are perf implications that may need to be addressed first.
There was a problem hiding this comment.
My understanding on this, that this comment applied only if custom marshaller would be used with HandleRef or IHandle, but with IntPtr these concerns are not applicable. So that PR looks like it can be merged as is right now.
There was a problem hiding this comment.
How will it play if we switch to LibraryImport later on? Will source gen produce code for this?
If we're just switching to LibraryImport it won't necessitate changing anything here.
|
|
||
| internal partial class Interop | ||
| { | ||
| internal unsafe partial class WinFormsComWrappers |
There was a problem hiding this comment.
I don't see anything here around apartment marshalling. If I recall, the entire clipboard stack is required to be on the STA. This doesn't enforce that or even bother to check, which means we can get into data corruption or random crashes. This type only ever going to be used on the STA and if so, how is that is that enforced?
There was a problem hiding this comment.
In the clipboard we check:
Perhaps we should be doing the same here as well.
There was a problem hiding this comment.
I'm all for multilayered checks, but I wonder, how .NET already handling that? Also My understnading was that I should use IAgileReference like it is here https://github.com/RussKie/ClipboardRedux
There was a problem hiding this comment.
All .NET objects are free-threaded so COM servers implemented in .NET can run on any apartment. For built-in RCWs, this is incredibly complex and handled in the RCW implementation - see UnmarshalIUnknownForCurrContext for an example of the marshalling across apartments.
There was a problem hiding this comment.
Yes, IAgileReference is the best way to do this on Windows 8+. However, it doesn't work on Windows 7, which means we need to have a fallback if Winforms is still required to support that OS. On pre-Windows 8 using the Global Interface Table (GIT) is required.
I believe this is mentioned in the ClipboardRedux example above.
There was a problem hiding this comment.
Independent of the answer on Windows 7 support, can we just implement new Windows Forms features the new best way and say that we'll document which features are not supported on Windows 7 going forward if that proves necessary?
Put another way, if we decide to support Windows 7 again -- which I really hope not -- it's not really intended as a first-class citizen.
There was a problem hiding this comment.
@AaronRobinsonMSFT and @RussKie can we consider current implementation as "limited support" for Win7? I'm not sure that Clipboard can be considered new feature. Or maybe for Win7 I can revert back to built-in Com Interop, but how all this playout with LibraryImport work. Just throwing ideas.
There was a problem hiding this comment.
My take on this is that we add OS checks to use the new functionality on Win8+ and use the original implementations for Win7.
There was a problem hiding this comment.
There was a problem hiding this comment.
Seems to be Did not matter. I have to somehow drag to that point and I need old implementation hanging around.
Still looking for approximate list of test cases for clipboard.
| (_wrappedInstance, formatPtr, &mediumRaw).ThrowIfFailed(); | ||
| medium = new() | ||
| { | ||
| pUnkForRelease = mediumRaw.pUnkForRelease == IntPtr.Zero ? null : Marshal.GetObjectForIUnknown(mediumRaw.pUnkForRelease), |
There was a problem hiding this comment.
Could you please help me understand why we need to marshal again, and can't just copy mediumRaw.pUnkForRelease here?
There was a problem hiding this comment.
what if somebody replace value, stored in the mediumRaw.pUnkForRelease. We do not know if somebody change value of pUnkForRelease. That's highly unlikely, but who knows.
There was a problem hiding this comment.
Since there was a question here consider a clarifying comment
|
|
||
| internal partial class Interop | ||
| { | ||
| internal unsafe partial class WinFormsComWrappers |
There was a problem hiding this comment.
In the clipboard we check:
Perhaps we should be doing the same here as well.
| IntPtr enumAdvisePtr; | ||
| var result = ((delegate* unmanaged<IntPtr, IntPtr*, HRESULT>)(*(*(void***)_wrappedInstance + 11))) | ||
| (_wrappedInstance, &enumAdvisePtr); | ||
| enumAdvise = result.Succeeded() ? null : (IEnumSTATDATA)Marshal.GetObjectForIUnknown(enumAdvisePtr); |
There was a problem hiding this comment.
@AaronRobinsonMSFT @kant2002 could you please help me understand the best practices for Marshal.GetObjectForIUnknown? According to the docs it increments the ref count. Does it mean we need to release enumAdvisePtr before we exit?
There was a problem hiding this comment.
Release is responsibility of RCW finalizer.
| ~EnumFORMATETCWrapper() | ||
| { | ||
| this.DisposeInternal(); | ||
| } | ||
|
|
||
| private void DisposeInternal() | ||
| { | ||
| Marshal.Release(_wrappedInstance); | ||
| _wrappedInstance = IntPtr.Zero; | ||
| } |
There was a problem hiding this comment.
Any reason we can't inline these together?
There was a problem hiding this comment.
Technically we can. That was needed because I thought that having Dispose method is valuable, but right now there no reason.
Create missing RCW and CCW Testing happens using existing test suite which is quite good.
Co-authored-by: Igor Velikorossov <[email protected]>
Add finalizer
f7b52b9 to
0c812d2
Compare
|
|
||
| public int QueryGetData(ref FORMATETC format) | ||
| { | ||
| using var dataObject = Unwrap(); |
There was a problem hiding this comment.
If this is how apartment support for IDataObject is being provided, then I'm not convinced we should permit it to be IDisposable. That pattern has non-trivial performance impact and is likely to be noticeable in large applications. We should instead defer to the built-in lifetime management provided by the runtime.
| { | ||
| internal unsafe partial class WinFormsComWrappers | ||
| { | ||
| internal sealed class DataObjectWrapper : IDataObject, IDisposable |
There was a problem hiding this comment.
I would expect the IAgileReference to be an implementation detail of DataObjectWrapper.
| /// </remarks> | ||
| private static IDataObject? GetDataObject(int retryTimes, int retryDelay) | ||
| { | ||
| IComDataObject? dataObject = null; |
There was a problem hiding this comment.
Since W7 support is still in scope, we need to retain the original implementation, and conditionally invoke the new implementation. E.g., using OsVersion.IsWindows8OrGreater or doing something like:
private static IDataObject? GetDataObject(int retryTimes, int retryDelay)
{
// new code
}
private static IDataObject? GetDataObjectWin7(int retryTimes, int retryDelay)
{
// original code
}There was a problem hiding this comment.
Thanks! does agility an issue for OleSetClipboard in same way as for OleGetClipboard?
|
@kant2002 unfortunately resource constraints have pushed this out of .NET 8. We hope to tackle the rest of the COM interop for .NET 9. I appreciate all of the effort here and if you're interested, I'll keep you looped in. As the COM code has changed significantly, I think it's better to close this one and use it as a reference in a new PR. Note: One particular difficulty here is that we need to make sure we've fully understood and documented changes in castability. Without built-in COM interop you don't get the "magic" casting of pointers back to their original objects or out to arbitrary implemented |
|
@JeremyKuhne I agree with you on this one. And since I did not push this in time we should rethink how it can be done in .NET 9. I still interested in this area, but cannot allocate cycles for pushing this forward. |
Create missing RCW and CCW
Testing happens using existing test suite which is quite good.
Microsoft Reviewers: Open in CodeFlow