Skip to content

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

Open
@FreddyDgh

Description

@FreddyDgh

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;
}
  1. (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)
  2. Add a "Windows Application Packaging Project" to the solution
  3. Under the "Applications" node, add a reference to the WPF app
  4. 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).

  1. 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)

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions