Knowledge Base

PRB: ShowHTMLDialog Returns E_NOINTERFACE (0x80004002)

Article ID: 183413

Article Last Modified on 11/19/1999


APPLIES TO


This article was previously published under Q183413

SYMPTOMS

ShowHTMLDialog returns an HRESULT of 0x80004002 - E_NOINTERFACE.

CAUSE

ShowHTMLDialog must be called from a thread that is a member of an apartment initialized by CoInitialize.

RESOLUTION

Change CoInitializeEx(NULL, COINIT_MULTITHREADED) calls to CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) or move the ShowHTMLDialog call to another thread that is apartment threaded.

STATUS

This behavior is by design.

MORE INFORMATION

The following code snippet demonstrates the problem:
#include <windows.h>
#include <mshtmhst.h>
#include <urlmon.h>
#include <tchar.h>
#include <comdef.h>
#include <objbase.h>
#pragma comment(lib, "urlmon.lib")

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
                     LPSTR lpCmdLine, int nCmdShow)
{
    HRESULT   hr;
    IMonikerPtr pmk;
    OLECHAR* pchHTML = L"http://www.microsoft.com";
    SHOWHTMLDIALOGFN* pfnShowHTMLDialog = NULL;
    HINSTANCE hinstMSHTML = NULL;

    // ***
    // *** The following should be
    // *** CoInitialize(NULL, COINIT_APARTMENTTHREADED);
    // ***
    CoInitializeEx(NULL, COINIT_MULTITHREADED);

    hinstMSHTML = LoadLibrary(TEXT("MSHTML.DLL"));

    if (NULL != hinstMSHTML)
    {
       pfnShowHTMLDialog = (SHOWHTMLDIALOGFN*)
                   GetProcAddress(hinstMSHTML, TEXT("ShowHTMLDialog"));
    }

    if (NULL != pfnShowHTMLDialog)
       hr = CreateURLMoniker(NULL, pchHTML, &pmk);

    if (SUCCEEDED(hr))
    {
       hr = pfnShowHTMLDialog(NULL,       // HWND        hwndParent,
                              pmk,        // IMoniker *  pMk,
                              NULL,       // TCHAR *     pchURL,
                              NULL,       // VARIANT *   pvarArgIn,
                              NULL        // VARIANT *   pvarArgOut,
                              );
    }

    if (hinstMSHTML)
        FreeLibrary(hinstMSHTML);

    CoUninitialize();

    return 0;
}
				

Keywords: kbprb kbcode KB183413