Help Center

CWebView2.AddHostObjectToScriptmethod

Add the provided host object to script running in the WebView with the specified name.

WebViewmethoddocumented

Syntax

FUNCTION AddHostObjectToScript (BYVAL _name AS LPCWSTR, BYVAL _object AS VARIANT PTR) AS HRESULT
Also documented as RemoveHostObjectToScript — one description covers them all.

Parameters

NameDescription
_nameThe name of the host object.
_objectThe host object to be added to script.

Description

Adds/removes the provided host object to script running in the WebView with the specified name.

Remarks

Host objects are exposed as host object proxies using window.chrome.webview.hostObjects.{name}. Host object proxies are promises and resolves to an object representing the host object. The promise is rejected if the app has not added an object with the name. When JavaScript code access a property or method of the object, a promise is return, which resolves to the value returned from the host for the property or method, or rejected in case of error, for example, no property or method on the object or parameters are not valid.

Note: While simple types, IDispatch and array are supported, and IUnknown objects that also implement IDispatch are treated as IDispatch, generic IUnknown, VT_DECIMAL, or VT_RECORD variant is not supported. Remote JavaScript objects like callback functions are represented as an VT_DISPATCH``VARIANT with the object implementing IDispatch. The JavaScript callback method may be invoked using DISPID_VALUE for the DISPID. Such callback method invocations will return immediately and will not wait for the JavaScript function to run and so will not provide the return value of the JavaScript function. Nested arrays are supported up to a depth of 3. Arrays of by reference types are not supported. VT_EMPTY and VT_NULL are mapped into JavaScript as null. In JavaScript, null and undefined are mapped to VT_EMPTY.

Reference

  • Defined in AfxNova/CWebView2.inc:823
  • Documented in WebView/CWebWiew2 Class.md
  • Topic: CWebView2 Class