0

I created a Com object using ATL C++

interface IHostObjectSample2 : IDispatch
{
    [propget, id(2), helpstring("Property.")] HRESULT Property([out, retval] BSTR* stringResult);
    [propput, id(2), helpstring("Property.")] HRESULT Property([in] BSTR stringValue);
};

dispinterface _IHostObjectSample2Events
{
    methods:
        [id(1), helpstring("message")] void Event();
};

coclass HostObjectSample2
{
    [default] interface IHostObjectSample2;
    [default, source] dispinterface _IHostObjectSample2Events;
};

I added the Com object to script using AddHostObjectToScript m_Webview->AddHostObjectToScript(L"sample", &remoteObjectAsVariant));

I can call methods/functions from the Script side

I want to fire the event _IHostObjectSample2Events::Event in c++ side and received it in the Javascript side

something like window.chrome.webview.hostObjects.sample.addEventListener("Event", () => { do something });

Roman R.
  • 68,205
  • 6
  • 94
  • 158
Eng KFKE
  • 21
  • 3
  • This is not documented, and I doubt it wouldn't be if it was implemented because it's a quite complex mechanic (IConnectionPoint, requires COM message pump, etc.). Only Microsoft can answer that (I see have posted the question here https://github.com/MicrosoftEdge/WebView2Feedback/issues/677) – Simon Mourier Nov 26 '20 at 11:48
  • It did not work with IE scripting, I don't anticipate connection points will be implemented with webview2 either. – Roman R. Nov 26 '20 at 12:26
  • You can simply call: `WebView2.ExecuteScriptAsync(String)` from C++ and call a normal javascript `function`. – Poul Bak Nov 26 '20 at 13:17
  • @RomanR. It's working with IE, Example here https://www.codeproject.com/Articles/35532/C-COM-Object-for-Use-In-JavaScript-HTML-Including like this : – Eng KFKE Nov 27 '20 at 18:21
  • I stand corrected then. IE11 however also supported a different method of supplying a callback interface from C++ end. I think this is what is working here with WebView2. Not sure if the aged mechanism of connection poins is intended to be supported. – Roman R. Nov 27 '20 at 21:15

1 Answers1

0

Unfortunately events aren't currently directly supported with WebView2's AddHostObjectToScript. You can file this as a feature request in the WebView2Feedback project.

David Risney
  • 3,886
  • 15
  • 16