1

I am developing custom toolbar (added one toolbar button) for Internet Explorer. I written the code in COM. When the user clicks my toolbar button I need to open a URL in new tab. I have written a navigate function as below. This function fails to open URL in new tab and always returns E_FAIL.

HRESUTL Navigate(CString csUrl) {
ATLASSERT(0 <= Url.Length());
ATLASSERT(m_pWebBrowser);

if (m_pWebBrowser == NULL) {
    return E_FAIL;
}

m_pWebBrowser->Stop();

VARIANT vUrl;
vUrl.vt = VT_BSTR;
vUrl.bstrVal = csUrl.AllocSysString();

VARIANT vFlag;
vFlag.vt = VT_I4;
vFlag.intVal = navOpenInNewTab;

VARIANT vEmpty;
VariantInit(&vEmpty);

HRESULT hr = m_pWebBrowser->Navigate2(&vUrl, &vFlag, &vEmpty, &vEmpty, &vEmpty);

if (hr == E_OUTOFMEMORY)
    MessageBox(NULL, L"Out of Memory.", L"Navigate", MB_OK);
else if (hr == E_INVALIDARG || hr == E_FAIL)
    MessageBox(NULL, L"Failed to navigate.", L"Navigate", MB_OK);

::SysFreeString(vUrl.bstrVal);

ATLASSERT(SUCCEEDED(hr));

return hr; }

What parameters I need to pass for "Navigate2" method to open the URL in new tab.

Please suggest me on this.

Regards, Santhosh

santhosh
  • 91
  • 10

1 Answers1

0

I am creating toolbar for IE.

This is my SetSite method

HRESULT CMyClass::SetupBrowser(IUnknown* pUnkSite) {
ATLASSERT(pUnkSite);
HRESULT hr = E_FAIL;

IOleCommandTarget* pCmdTarget = NULL;   
if (SUCCEEDED(pUnkSite->QueryInterface(IID_IOleCommandTarget,
                        (LPVOID*)&pCmdTarget)) && NULL != pCmdTarget) {
    IServiceProvider* pSP = NULL;
    if (SUCCEEDED(pCmdTarget->QueryInterface(IID_IServiceProvider,
                                (LPVOID*)&pSP)) && NULL != pSP) {
        CComPtr<IServiceProvider> child_provider;
        hr = pSP->QueryService(SID_STopLevelBrowser,
                               IID_IServiceProvider,
                               reinterpret_cast<void**>(&child_provider));
        if (SUCCEEDED(hr)) {
            hr = child_provider->QueryService(SID_SWebBrowserApp,
                                              IID_IWebBrowser2,
                                              reinterpret_cast<void**>(&m_pWebBrowser));
        }

        /*hr = pSP->QueryService(SID_SWebBrowserApp,
                                IID_IWebBrowser2,
                                (LPVOID*)&m_pWebBrowser);*/
        ATLASSERT(m_pWebBrowser);
        pSP->Release();
    }
    pCmdTarget->Release();
}

m_ReflectWnd.GetToolBar().SetBrowser(m_pWebBrowser);

return hr; }
santhosh
  • 91
  • 10
  • It'd be more appropriate to edit your original question and add the code there. Anyway, the code seems to be correct, I'm unable to spot a problem. What if you use `navOpenInNewWindow`, does it open a new window or still return `E_FAIL`? – noseratio Aug 09 '13 at 10:26
  • sorry for that. Yes If I use navOpenInNewWindow it returns E_FAIL. – santhosh Aug 09 '13 at 10:48
  • Can I use a javascript to open the URL in new tab and can it be executed from my native code? Just a thought. – santhosh Aug 09 '13 at 10:50
  • You could, by injecting some JavaScript into the current document (through `IWebBrowser2::get_Document`). Before you go that route, could you check the value of `IWebBrowser2::get_FullName`, does it have "iexplore.exe" in it? Also, have you tried your IE extension on a different machine? – noseratio Aug 09 '13 at 10:55
  • get_FullName returns "C:\ProgramFile\Internet Explorer\iexplorer.exe". I tried in different system too, the result is same. To inform you that I tried with one more approach by creating COM object for CLSID_InternetExplorer, query for IWebBrowser2 like hr = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)&m_spWebBrowser); call m_spWebBrowser->Navigate2() twice one with navOpenInNewTab enabled opens a new window then creates tab.Ideally this is not my requirement(where it opens a new window). – santhosh Aug 09 '13 at 11:33
  • I don't really know why this is happening, but I'd try one more thing. Defer the navigation by posting a custom message to your own window (with `PostMessage`). Handle the message and call `Navigate2` there. – noseratio Aug 09 '13 at 11:54
  • 1
    @ Noseratio, Thanks for your help. To resolve this issue I used ShellExecute to navigate the URL. – santhosh Aug 13 '13 at 05:18