views:

45

answers:

1

Hi Folks,

In C++, I am creating an IE window (IE7) with CoCreateInstance, and using Navigate2 to navigate to a web page. I want to be able to get the handle to the IE window created by Navigate2. Notice in Navigate2, I give a window name as a parameter. Using get_HWND() is not working. I'm getting a handle to an IE window but not the one that has the URL I navigated to. I want to use this handle to restore the IE Window if it happens to be minimized. But so far, while I do get an IE handle (which I suspect is the "main" ie window") calling functions with this handle do nothing to the IE window I care about, the one with the newly navigated to page. Any idea what I'm doing wrong? Code below.


IWebBrowser2*   pIE;

hr = CoCreateInstance(CLSID_InternetExplorer, 
        NULL, 
        CLSCTX_LOCAL_SERVER, 
        IID_IWebBrowser2, 
        (void**)&pIE);

if( SUCCEEDED(hr) )
{
    VARIANT vtEmpty;
    VARIANT vtTarget;
VARIANT vtUrl;

    VariantInit(&vtEmpty);
    VariantInit(&vtTarget);
VariantInit(&vtUrl);

    vtTarget.vt = VT_BSTR;
    vtTarget.bstrVal = SysAllocString(L"RAD_AUTHIE_WINDOW");

vtUrl.vt = VT_BSTR;
    vtUrl.bstrVal = SysAllocString(strAuthURL);

    hr = pIE->Navigate2(&vtUrl,&vtEmpty,&vtTarget,&vtEmpty,&vtEmpty);

HWND hWnd = NULL;
pIE->get_HWND((SHANDLE_PTR*)&hWnd);
if (hWnd != NULL)
{
        //not the right window handle
    ::ShowWindow(hWnd,SW_NORMAL);
}


    VariantClear(&vtTarget);
VariantClear(&vtUrl);


pIE->Quit();
    pIE->Release();
A: 

The WebBrowser2 you reference is the top-level one. For getting references to the frames, take a look at this MSDN sample.

Most notably you need to get the IOleContainer interface for the document:

IDispatch* pDisp;
pIE->get_Document(&pDisp);
IOleContainer* pContainer;
pDisp->QueryInterface(IID_IOleContainer, (void**)&pContainer);

Get an enumerator for the frames:

IEnumUnknown* pEnumerator;
pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);

Enumerate the frames and find the right one:

IUnknown* pUnk;
ULONG uFetched;

for (UINT i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); ++i)
{
    IWebBrowser2* pBrowser;
    hr = pUnk->QueryInterface(IID_IWebBrowser2, (void**)&pBrowser);
    if (FAILED(hr)) continue;

All thats left is to compare with IWebBrowser2::Name to check wether you've found the right frame yet.

Georg Fritzsche
Dan G
@Dan: As posted in your question, with doing it directly after `CoCreateInstance()`, there probably won't be any frames yet. You could do it later, e.g. by reacting to / just grabbing the instance you want via the [navigate events](http://msdn.microsoft.com/en-us/library/aa768283%28v=VS.85%29.aspx).
Georg Fritzsche