microsoft / WindowsAppSDK

The Windows App SDK empowers all Windows desktop apps with modern Windows UI, APIs, and platform features, including back-compat support, shipped via NuGet.
https://docs.microsoft.com/windows/apps/windows-app-sdk/
MIT License
3.74k stars 311 forks source link

Clipboard/IDataPackageView APIs throwing COMException (RPC_E_WRONG_THREAD) in partial trust app #959

Open FreddyDgh opened 3 years ago

FreddyDgh commented 3 years ago

Description

When trying to get data from the clipboard, the following exception is thrown:

System.Runtime.InteropServices.COMException
  HResult=0x8001010E
  Message=The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))
  Source=System.Private.CoreLib
  StackTrace:
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at TempClipboardPartialTrust.MainWindow.<Button_Click>d__1.MoveNext() in C:\Users\freddy\source\Repos\TempClipboardPartialTrust\TempClipboardPartialTrust\MainWindow.xaml.cs:line 34

The issue only occurs when the application is packaged and running in partial trust. No exception is thrown when unpackaged or when packaged running in full trust.

Environment Information

.NET SDK version: 5.0.100-rc.1.20452.10 VS Version: Version 16.8.0 Preview 3.1

Repro Steps

  1. Create a WPF app
  2. Use 'net5.0-windows10.0.18362.0' as the target framework.
  3. Add code to get data from the clipboard (e.g. a button handler like the following):
    private async void Button_Click(object sender, RoutedEventArgs e)
    {
      Windows.ApplicationModel.DataTransfer.DataPackageView content = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
      Windows.Foundation.IAsyncOperation<string> getTextOperation = content.GetTextAsync();
      string s = await getTextOperation;
      this.clipboardResultTextBox.Text = s;
    }
  4. (Optional) run the app and observe that the API can get data from the clipboard when not packaged (ensure appropriate data exists on the clipboard)
  5. Add a "Windows Application Packaging Project" to the solution
  6. Under the "Applications" node, add a reference to the WPF app
  7. Change the "Trust Level" in the properties of the referenced app from "Full Trust" to "Partial Trust" 7b. Workaround: Currently, it looks like the packaging project doesn't automatically copy the needed dependencies when targeting 'net5.0-windows10.0.18362.0'. After building the packaging project, manually copy WinRT.Runtime.dll and Microsoft.Windows.SDK.NET.dll to the bin\AnyCPU\Debug\AppX\[AppName] folder. Omitting this workaround was giving the following message when trying to debug the packaged app:

    The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core. The program '[13840] WpfApp1.exe' has exited with code -2147450740 (0x8000808c).

  8. Run the packaged application and observe the exception being thrown (ensure appropriate data exists on the clipboard)

Edit: packaging project now seems to copy dependencies correctly, so workaround in step 7 should no longer be needed (tested using .NET 5 RC2/Visual Studio 16.8.0 Preview 4)

FreddyDgh commented 3 years ago

Can anyone confirm that this is the correct repo for this issue? I am sure that people are busy due to the upcoming .NET 5 release, but I noticed that the issue has not even been given a bug label yet.

My original reasoning for posting in this repo was because the underlying Windows Clipboard API obviously works with partial trust (e.g. from UWP apps), and because the same exact .NET 5 code works when in full trust but not in partial trust when using the CsWinRT projections, it seemed likely that the issue would have to be fixed at the CsWinRT level. Perhaps that is not the case?

I am wondering if it might be better to open an issue on the Project Reunion repo, but I was hoping somebody might confirm what is actually the best repo before opening more issues elsewhere.

Scottj1s commented 3 years ago

@FreddyD-GH thanks for your patience. This repo is probably as good as any, since the issue intersects several Project Reunion technologies. We'll try to triage soon, but it may take a few days.

AdamBraden commented 3 years ago

@FreddyD-GH - the dev team is looking into this but they want to confirm exactly how you set Partial Trust for your package. Do you have a pointer to a github repo or can you include your package.appxmanifest?

FreddyDgh commented 3 years ago

I appreciate you taking a look at this.

I just change the setting to partial trust using the Visual Studio 2019 Properties Window. This does not modify the template version of the package.appxmanifest file, but instead adds the line <TrustLevel>Partial</TrustLevel> to the <ProjectReference> node in the *.wapproj file. If I recall correctly, this does generate a slightly different output package.appxmanifest when building, because the template values are overwritten differently based on whether it's full trust or partial trust.

I setup a repo you can look at: https://github.com/FreddyD-GH/TempClipboardPartialTrust

FreddyDgh commented 3 years ago

@AdamBraden @Scottj1s were you able to look into this issue at all? I just found out that I can successfully use the Clipboard API if I copy text from certain search boxes contained in built-in Windows apps. For example, I can copy successfully from the Windows search box (keyboard shortcut: WIN + S), the File Explorer search box, and the Photos app search box. However, other apps that I've tried copying from (Visual Studio, MS Edge, notepad, etc.) all give the threading error. I am starting to think the issue is probably not about CsWinRT per se, but maybe more about the details of how Windows handles "AppContainer" apps, and perhaps this issue is better suited for the Project Reunion repo.

asklar commented 3 years ago

fyi the apps where this works are likely using the WinRT clipboard APIs whereas the others are just using the OLE clipboard

angelazhangmsft commented 3 years ago

may be fixed with microsoft/CsWinRT#612

AdamBraden commented 3 years ago

This is a limitation of the underlying api and has nothing to do with cswinrt.

Moving to Project Reunion as the appropriate backlog.