You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use c++ to make to dll extention for a MFC application ( I don't have source code of it), and I want to create a dialog and use webview2 on this dialog, but got error when open the dialog , it say "cannot change thread mode after it is set". code like this,
namespacewbWebview {
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
// Pointer to WebViewControllerstatic wil::com_ptr<ICoreWebView2Controller> webviewController;
// Pointer to WebView windowstatic wil::com_ptr<ICoreWebView2> webviewWindow;
voidshow() {
DialogBoxParam(FuncHook::resModuleHandle, MAKEINTRESOURCE(IDD_DIALOG), wbWindowHandle, DialogProc, 0);
}
voidOnInitDialog(HWND hWnd) {
CreateCoreWebView2EnvironmentWithOptions(
nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Environment *env) -> HRESULT
{
WCHAR wszMsgBuff[512]; // Buffer for text.
DWORD dwChars; // Number of chars returned.// Try to get the message from the system errors.
dwChars = FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
result,
0,
wszMsgBuff,
512,
NULL );
//error here !!!MessageBoxW(nullptr, wszMsgBuff, nullptr, MB_OK);
// Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window// hWnd
env->CreateCoreWebView2Controller(
hWnd,
Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Controller *controller) -> HRESULT
{
if (controller != nullptr)
{
webviewController = controller;
webviewController->get_CoreWebView2(&webviewWindow);
}
// Add a few settings for the webview// The demo step is redundant since the values are the default settings
ICoreWebView2Settings *Settings;
webviewWindow->get_Settings(&Settings);
Settings->put_IsScriptEnabled(TRUE);
Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
Settings->put_IsWebMessageEnabled(TRUE);
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewController->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
HRESULT hresult = webviewWindow->Navigate(L"https://www.baidu.com/");
if (hresult != S_OK)
{
WCHAR wszMsgBuff2[512]; // Buffer for text.
DWORD dwChars2; // Number of chars returned.// Try to get the message from the system errors.
dwChars2 = FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hresult,
0,
wszMsgBuff2,
512,
NULL );
MessageBoxW(nullptr, wszMsgBuff2, nullptr, MB_OK);
}
// Step 4 - Navigation events// register an ICoreWebView2NavigationStartingEventHandler to cancel any non-https// navigation
EventRegistrationToken token;
// webviewWindow->add_NavigationStarting(// Callback<ICoreWebView2NavigationStartingEventHandler>(// [](ICoreWebView2 *webview,// ICoreWebView2NavigationStartingEventArgs *args) -> HRESULT// {// PWSTR uri;// args->get_Uri(&uri);// std::wstring source(uri);// if (source.substr(0, 5) != L"https")// {// args->put_Cancel(true);// }// CoTaskMemFree(uri);// return S_OK;// })// .Get(),// &token);// Step 5 - Scripting// Schedule an async task to add initialization script that freezes the Object object
webviewWindow->AddScriptToExecuteOnDocumentCreated(L"Object.freeze(Object);", nullptr);
// Schedule an async task to get the document URL
webviewWindow->ExecuteScript(
L"window.document.URL;",
Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
[](HRESULT errorCode, LPCWSTR resultObjectAsJson) -> HRESULT
{
LPCWSTR URL = resultObjectAsJson;
// doSomethingWithURL(URL);return S_OK;
})
.Get());
// Step 6 - Communication between host and web content// Set an event handler for the host to return received message back to the web content// webviewWindow->add_WebMessageReceived(// Callback<ICoreWebView2WebMessageReceivedEventHandler>(// [](ICoreWebView2 *webview,// ICoreWebView2WebMessageReceivedEventArgs *args) -> HRESULT// {// PWSTR message;// args->TryGetWebMessageAsString(&message);// // processMessage(&message);// webview->PostWebMessageAsString(message);// CoTaskMemFree(message);// return S_OK;// })// .Get(),// &token);// Schedule an async task to add initialization script that// 1) Add an listener to print message from the host// 2) Post document URL to the host// webviewWindow->AddScriptToExecuteOnDocumentCreated(// L"window.chrome.webview.addEventListener(\'message\', event => alert(event.data));"// L"window.chrome.webview.postMessage(window.document.URL);",// nullptr);return S_OK;
})
.Get());
return S_OK;
})
.Get());
}
BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
OnInitDialog(hWnd);
break;
case WM_SIZE:
if (webviewController != nullptr)
{
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewController->put_Bounds(bounds);
};
break;
case WM_CLOSE:
ShowWindow(hWnd, SW_HIDE);
returnTRUE;
default:
returnDefWindowProc(hWnd, uMsg, wParam, lParam);
break;
}
returnFALSE;
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I use c++ to make to dll extention for a MFC application ( I don't have source code of it), and I want to create a dialog and use webview2 on this dialog, but got error when open the dialog , it say "cannot change thread mode after it is set". code like this,
Beta Was this translation helpful? Give feedback.
All reactions