WebBrowser Control (MFC) created in seperate thread working in Windows 7 and Vista, but Windows XP

久未见 提交于 2020-01-13 19:08:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/3699633/webbrowser-control-mfc-created-in-seperate-thread-working-in-windows-7-and-vis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!