views:

54

answers:

1

Hello.

I have CWnd in main thread, and a CWnd with a WebBrower control created in a seperate thread. This is necessary, because the WebBrowser navigates to urls which have javascripts running which will block the WebBrowser. So I with the WebBrowser control in a seperate thread I prevent the GUI Thread vom hang. Also I have done this in main thread in my application:

CCustomOccManager *pMgr = new CCustomOccManager();

AfxEnableControlContainer(pMgr);

That's for extending the WebBrowser control to my on "window.external" interface.

The seperate thread mechanism works fine in Windows 7 and Vista. But in Windows XP I get MFC asserts.

That's my code:

    m_WndMain.CreateEx(WS_EX_TOPMOST | WS_EX_TOOLWINDOW, m_mainWndClass, NULL, WS_POPUP, m_pArgs->m_WindowRect, NULL, 0);
    m_WndMain.ShowWindow(SW_SHOW);
    m_WndMain.UpdateWindow();

    CRect clientRect;
    m_WndMain.GetClientRect(&clientRect);

    /* !!! CreateControl FAILS IN WINDOWS XP (=ASSERT)!!! */
    m_CtrlBrowser.CreateControl(CLSID_WebBrowser, NULL, WS_VISIBLE | WS_CHILD, clientRect, &m_WndMain, AFX_IDW_PANE_FIRST);

    .....

MFC Assert (Line 925 internally calls Line 305):

wincore.cpp, LINE 925
-----------------------------------
CHandleMap* pMap = afxMapHWND();
ASSERT(pMap != NULL);   <--- pMap is NULL, ASSERT is raised


wincore.cpp, LINE 305
-----------------------------------
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();

...some code

pState->m_pmapHWND;  <--- m_pmapHWND is NULL!

So "m_pmapHWND" is NULL in Windows XP. But it's not NULL in Vista and 7.

Under Windows XP, if I create the WebBrowser Control in the main thread, it works.

So what's the problem? Different MFC versions or a Windows XP issue?

Please help, it's urgent. Thank you very much!

Chris

+1  A: 

I have debugged the MFC and found out, that COM has to be initialized in the newly created thread.

So in the thread startroutine I'm doing a

CoInitializeEx(NULL,  COINIT_APARTMENTTHREADED);

at first and everything works.

Interesting that this isn't needed under Windows 7 and Vista, only XP.

Regards

Chris

Chris