microsoft-ui-xaml: AccessViolation 0xC0000005 was thrown while calling AddHostObjectToScript

Describe the bug

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Create a WinUI 3 project (either UWP or Desktop)
  2. Follow https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addhostobjecttoscript
  3. AccessViolation was thrown while calling AddHostObjectToScript

Expected behavior

No exception was thrown.

Version Info

NuGet package version: WASDK 1.0, WinUI 3

Windows app type:

UWP Win32
Yes Yes
Windows 10 version Saw the problem?
Windows 11 (22000) Yes
Insider Build (21322) Yes
October 2020 Update (19042)
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Yeah, we’re still waiting on this so we can more easily port the Monaco wrapper over to WinUI 3 without a big work-around.

This API is not yet implemented in the core WebView2 WinRT API.

@krschau this issue was filed before the UWP version existed. We also need this on WinUI 2, please leave this issue open if it’s not working there still as well.

The goal is to be able to target abstractions on top of WebView2 across WinForms, WFP, UWP, and WinUI 3.

I also tested against WinUI 3 Desktop app and it also happens to WinUI 3 Desktop Apps.

I’ve upgrade to 2.8.0-prerelease, but this time it produces AV instead of NotImplementedException.

@michael-hawker Yes, the underlying functionality has existed for a while. The issue holding it back from WinUI is not having support for this API via the WinRT wrappers.

This is now fixed internally and will be part of the next release. It will require a little indirection, since the underlying functionality currently still only supports IDispatch objects, rather than IInspectable objects which WinRT APIs required. So use requires going via a COM interface, which will look something like this:

  var webview2Interop = (ICoreWebView2Interop)myWebView2.CoreWebView2;
  webview2Interop.AddHostObjectToScript("name", dispatchObject);

The expectation is eventually the WinRT API will be supported (as long as the app passes in an IInspectable), but the interop interface shown above unblocks the scenario.

@codendone is this scheduled on the roadmap yet for an upcoming release? It’s already working for WPF, right?

This was a pain to work-around, and I’ve been hesitant to make any sweeping changes to the Monaco component as it’s a significant fork with our workaround vs. having basically just a method rename between UWP and WinUI 3.