Search code examples
c++builderwebview2tedgebrowser

Usage ICoreWebView2EnvironmentOptions in C++Builder 11


I am trying to disable CORS in TEdgeBrowser and found a lot of solutions by using ICoreWebView2EnvironmentOptions because TEdgeBrowser is implement by WebView2.

In Microsoft's document, the sample code seems to used for Visual C++ and C++ Builder is not applicable:

auto options = Microsoft::WRL::Make<CoreWebView2ExperimentalEnvironmentOptions>();

Here is the code I have tried in C++ Builder 11:

_di_ICoreWebView2EnvironmentOptions *m_WV2_EnvOpt = new _di_ICoreWebView2EnvironmentOptions();
m_WV2EnvOpt->put_AdditionalBrowserArguments(L"--disable-web-security");

It would fail in m_WV2EnvOpt->put_AdditionalBrowserArguments().

I have no idea and maybe I got the wrong way. Can someone help me?


Solution

  • _di_ICoreWebView2EnvironmentOptions is a typedef for DelphiInterface holding a ICoreWebView2EnvironmentOptions* pointer. You don't use new on DelphiInterface itself, you new a class that implements the interface, eg:

    class TCoreWebView2EnvironmentOptionsImpl : public ICoreWebView2EnvironmentOptions
    {
        // implement IUnknown and ICoreWebView2EnvironmentOptions as needed...
    };
    
    _di_ICoreWebView2EnvironmentOptions m_WV2_EnvOpt = new TCoreWebView2EnvironmentOptionsImpl;
    

    However, WRL objects are not used this way.

    In this case, the WebView2 library exposes ICoreWebView2EnvironmentOptions as a COM object, so you can use CoCreateInstance() to instantiate it (the CLSID for the WebView2 library is 26D34152-879F-4065-BEA2-3DAA2CFADFB8, and the IID for ICoreWebView2EnvironmentOptions is 2FDE08A8-1E9A-4766-8C05-95A9CEB9D1C5), eg:

    _di_ICoreWebView2EnvironmentOptions m_WV2_EnvOpt;
    CoCreateInstance(LIBID_WebView2, NULL, CLSCTX_INPROC_SERVER, IID_ICoreWebView2EnvironmentOptions, (LPVOID*)&m_WV2_EnvOpt);
    

    However, that doesn't help you in this situation, because you would have to provide the created ICoreWebView2EnvironmentOptions object when creating the WebView2 object, and AFAIK TEdgeBrowser simply does not allow you to do that.

    You could try setting the WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS environment variable before the WebView2 object is created, but AFAIK this method does not support the --disable-web-security option.