无法枚举shellwindows实例

时间:2015-11-12 15:53:52

标签: internet-explorer winapi automation

我在尝试访问shellwindows实例以找到IE时遇到异常,所以我可以附加到它并在自动模式下进行web-serfing。我无法找出为什么会发生这种情况所以我只是显示代码片段。这是:

#include "stdafx.h"
#include <Windows.h>

#include <Exdisp.h>
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD dwError;
    IShellWindows *psw;
    HRESULT hr = CoInitialize(NULL);
    hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_IShellWindows, (void **)&psw);
    if (!SUCCEEDED(hr))
    {
        dwError = GetLastError();
        printf("Error: %d\n", dwError);
        return 0;
    }
    IWebBrowser2* pBrowser2 = 0;
    long nCount = 0;
    hr = psw->get_Count(&nCount);
    printf("%d\n", nCount);
    for (long i = 0; i < nCount; i++)
    {
        _variant_t va(i, VT_I4);
        IDispatch *spDisp;
        hr = psw->Item(va, &spDisp);
        hr = spDisp->QueryInterface(IID_IWebBrowserApp, (void**)&pBrowser2);
        if (SUCCEEDED(hr))
        {
            BSTR name;
            pBrowser2->get_FullName(&name);
            if (wcsstr(name, L"IEXPLORE.EXE"))
            {
                wprintf(L"%ls\n", name);
                break;

            }
        }
    }
    psw->Release();

    VARIANT vEmpty;
    VariantInit(&vEmpty);
    _variant_t URL;
    //URL.SetString("http://googl.ecom");
    pBrowser2->Navigate2(&URL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
    Sleep(2000);
    //pBrowser2->GoBack();
    pBrowser2->GoBack();
    Sleep(2000);
    pBrowser2->GoBack();

    //pBrowser2->GoForward();
    //pBrowser2->GoForward();
    getchar();
    return 0;
}

以下是执行execption的行

hr = spDisp->QueryInterface(IID_IWebBrowserApp, (void**)&pBrowser2);

调试器显示spDisp为NULL。我试图搜索有关该问题的一些信息,但并未真正找到线索。问题出在哪里?谢谢你的时间!

1 个答案:

答案 0 :(得分:1)

您的代码应该以这种方式编写。 (我不保证结果)

#include "stdafx.h"
#include <Windows.h>

#include <Exdisp.h>
#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD dwError;
    IShellWindows *psw;
    HRESULT hr = CoInitialize(NULL);
    hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_IShellWindows, (void **)&psw);
    if (!SUCCEEDED(hr))
    {
        dwError = GetLastError();
        printf("Error: %d\n", dwError);
        return 0;
    }
    IWebBrowser2* pBrowser2 = 0;
    long nCount = 0;
    hr = psw->get_Count(&nCount);
    printf("%d\n", nCount);
    for (long i = 0; i < nCount; i++)
    {
        _variant_t va(i, VT_I4);
        IDispatch *spDisp;
        hr = psw->Item(va, &spDisp);
        if(SUCCEEDED(hr)){
            hr = spDisp->QueryInterface(IID_IWebBrowserApp, (void )&pBrowser2);
            if (SUCCEEDED(hr))
            {
                BSTR name;
                pBrowser2->get_FullName(&name);
                if (wcsstr(name, L"IEXPLORE.EXE"))
                {
                    wprintf(L"%ls\n", name);

                    VARIANT vEmpty;
                    VariantInit(&vEmpty);
                    _variant_t URL;
                    //URL.SetString("http://googl.ecom");
                    pBrowser2->Navigate2(&URL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
                    Sleep(2000);
                    //pBrowser2->GoBack();
                    pBrowser2->GoBack();
                    Sleep(2000);
                    pBrowser2->GoBack();

                }
                pBrowser2->Release();
            }
        }
    }
    psw->Release();

    getchar();
    return 0;
}