I have TEdgeBrowser
and I am using ExecuteScript
method from the DefaultInterface
like this:
EdgeBrowser1->DefaultInterface->ExecuteScript()
ExecuteScript
, when called this way takes 2 parameters:
System::WideChar * javaScript
and const _di_ICoreWebView2ExecuteScriptCompletedHandler handler
What I want to write is a lambda function (using clang compiler) which gets executed (and destroyed) after a script is executed...
Something like:
EdgeBrowser1->DefaultInterface->(L"alert('Hello World!')",
_di_ICoreWebView2ExecuteScriptCompletedHandler(
new TCppInterfacedObject<TCoreWebView2ExecuteScriptCompletedHandler>(
[](HRESULT errorCode, PCWSTR resultObjectAsJson) -> HRESULT {
if (FAILED(errorCode))
{
ShowMessage("Failed to execute script");
return errorCode;
}
ShowMessage("Script executed successfully");
return S_OK;
})));
However, TCoreWebView2ExecuteScriptCompletedHandler
doesn't seem to be defined? What do I need to insert there to make it work?
EDIT:
A few issues I encountered with otherwise excellent reply by @RemyLebeau
- in
WebView2.hpp
the second parameter ofInvoke
is defined asSystem::WideChar *
so I couldn't useLPCWSTR
as it reported that the virtual methodInvoke
is unimplemented. Also there wasn't a__stdcall
. Corrected by using instead:
HRESULT __stdcall Invoke(HRESULT errorCode, System::WideChar* resultObjectAsJson)
is the use of
INTFOBJECT_IMPL_IUNKNOWN
really necessary since the TCppInterfacedObject documentation describes that it already implementsIUnknown
methods likeQueryInterface
,AddRef
, andRelease
? When commented out, there is no warning that those methods are unimplemented, like when it is when I "manually" implement theICoreWebView2ExecuteScriptCompletedHandler
Is it necessary to
Release()
the object before it returns S_OK? I know that there is reference counting involved but does theExecuteScript
call then does that? Basically, after executing the lambda or instance ofICoreWebView2ExecuteScriptCompletedHandler
, it is no longer needed to be in memory.