1

The problem is that I want to get the handle value of the new tab created here.

By default, when you get the handle value of pWebBrowser2, it gets caught as the main handle of the current window, not the new handle's unique handle.

There is one operating condition here. IE Tools -> Internet Options -> General -> Tab Settings -> Always open popup in new window should be enabled.

Always open popup in new window Because of the setting, the newly opened window runs as a separate process.

The problem is that you should not get the handle value after the window pops up, but I need to know the handle value before Navigate2.

If you provide a basic sample file to fix the problem, we will support $ 500 with PayPal.

CString strUrl = _T("http://www.google.com");

CoInitialize(NULL);

HINSTANCE hInst = ::LoadLibrary(_T("OLEACC.DLL"));

if (hInst != NULL)
{
    if (hwnd)
    {
        CComPtr<IHTMLDocument2> spDoc;
        LRESULT lRes;

        UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));
        ::SendMessageTimeout(hwnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes);

        HRESULT hr = ::ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, (void**)&spDoc);

        if (SUCCEEDED(hr))
        {
            CComPtr<IHTMLWindow2> spWnd2;
            hr = spDoc->get_parentWindow((IHTMLWindow2**)&spWnd2);

            if (SUCCEEDED(hr))
            {
                CComPtr<IServiceProvider> spServiceProv;
                hr = spWnd2->QueryInterface(IID_IServiceProvider, (void**)&spServiceProv);

                if (SUCCEEDED(hr))
                {
                    IWebBrowser2* pWebBrowser2 = 0;
                    hr = spServiceProv->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&pWebBrowser2);

                    if (SUCCEEDED(hr))
                    {
                        // Here IWebBrowser2 makes navigate2 as a new tab.

                        VARIANT vtUrl;
                        VariantInit(&vtUrl);
                        vtUrl.vt = VT_BSTR;
                        vtUrl.bstrVal = strUrl.AllocSysString();

                        VARIANT vFlags;
                        V_VT(&vFlags) = VT_I4;
                        V_I4(&vFlags) = navOpenInNewWindow;

                        VARIANT vEmpty;
                        VariantInit(&vEmpty);

                        pWebBrowser2->Navigate2(&vtUrl, &vFlags, &vEmpty, &vEmpty, &vEmpty);

                        VariantClear(&vtUrl);
                        VariantClear(&vFlags);
                        VariantClear(&vEmpty);

                        pWebBrowser2->Release();
                    }

                    spServiceProv.Release();
                }

                spWnd2.Release();
            }

            spDoc.Release();
        }
    }

    ::FreeLibrary(hInst);

}

CoUninitialize();

Basically, when you get the handle value of pWebBrowser2, it gets caught as the main handle of the current window, not the new handle's unique handle.

yyms
  • 31
  • 5
  • 1
    You should handle the NewWindow3 event fired then create your own window, or IE will create its own process. – tunglt Dec 27 '18 at 23:39

0 Answers0