diff --git a/dlls/mshtml/htmldoc.c b/dlls/mshtml/htmldoc.c
index 49b05371dbb..134b108cb42 100644
--- a/dlls/mshtml/htmldoc.c
+++ b/dlls/mshtml/htmldoc.c
@@ -1957,6 +1957,7 @@ HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
{
HTMLDocumentObj *doc;
nsIDOMWindow *nswindow = NULL;
+ nsresult nsres;
HRESULT hres;
TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppvObject);
@@ -1972,23 +1973,26 @@ HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
doc->ref = 1;
doc->basedoc.doc_obj = doc;
+ list_init(&doc->bindings);
+ doc->usermode = UNKNOWN_USERMODE;
+ doc->readystate = READYSTATE_UNINITIALIZED;
+
+ doc->nscontainer = NSContainer_Create(doc, NULL);
+ if(!doc->nscontainer) {
+ ERR("Failed to init Gecko, returning CLASS_E_CLASSNOTAVAILABLE\n");
+ htmldoc_release(&doc->basedoc);
+ return CLASS_E_CLASSNOTAVAILABLE;
+ }
+
hres = htmldoc_query_interface(&doc->basedoc, riid, ppvObject);
htmldoc_release(&doc->basedoc);
if(FAILED(hres))
return hres;
- doc->nscontainer = NSContainer_Create(doc, NULL);
- list_init(&doc->bindings);
- doc->usermode = UNKNOWN_USERMODE;
- doc->readystate = READYSTATE_UNINITIALIZED;
- if(doc->nscontainer) {
- nsresult nsres;
-
- nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &nswindow);
- if(NS_FAILED(nsres))
- ERR("GetContentDOMWindow failed: %08x\n", nsres);
- }
+ nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &nswindow);
+ if(NS_FAILED(nsres))
+ ERR("GetContentDOMWindow failed: %08x\n", nsres);
hres = HTMLWindow_Create(doc, nswindow, NULL /* FIXME */, &doc->basedoc.window);
if(nswindow)
diff --git a/dlls/mshtml/tests/dom.c b/dlls/mshtml/tests/dom.c
index ba851306d8a..d9642e1f5b9 100644
--- a/dlls/mshtml/tests/dom.c
+++ b/dlls/mshtml/tests/dom.c
@@ -438,6 +438,8 @@ static IHTMLDocument2 *create_document(void)
hres = CoCreateInstance(&CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
&IID_IHTMLDocument2, (void**)&doc);
ok(hres == S_OK, "CoCreateInstance failed: %08x\n", hres);
+ if(FAILED(hres))
+ return NULL;
hres = IHTMLDocument2_QueryInterface(doc, &IID_IHTMLDocument5, (void**)&doc5);
if(FAILED(hres)) {