To resolve this problem, send the WebBrowser the OLECMDID_ONUNLOAD command in your host application. This can be accomplished by calling the WebBrowser control's ExecWB() method.
To fully emulate Internet Explorer's behavior, you must examine the return value of ExecWB's fourth argument, which tells you whether or not the shutdown attempt failed or succeeded. This is because script authors can use the OnBeforeUnload event to ask the user whether they want to leave the page or not. If the user answers
no, the WebBrowser should remain activated.
Visual C++
HRESULT hr;
VARIANT vOut;
VariantInit(&vOut);
hr = pWebBrowser2->ExecWB(OLECMDID_ONUNLOAD, OLECMDEXECOPT_DODEFAULT, NULL, &vOut);
if (SUCCEEDED(hr)) {
// Check VT_BOOL return value of vOut to make sure we're really shutting down - IE or the user
// might decide not to honor this request.
if (vOut.bVal == TRUE) {
// Continue shutdown process
} else {
// recover
}
}
Visual Basic
Public Sub Form_Unload(Cancel as Integer)
WebBrowser1.ExecWB OLECMDID_ONUNLOAD, OLECMDEXECOPT_DODEFAULT, 0, ret
If ret = False Then
Cancel = 1
End If
End Sub