WebBrowser控件(MFC)在单独的线程中工作在Windows 7和Vista,但Windows XP中创建 [英] WebBrowser Control (MFC) created in seperate thread working in Windows 7 and Vista, but Windows XP

查看:398
本文介绍了WebBrowser控件(MFC)在单独的线程中工作在Windows 7和Vista,但Windows XP中创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主线程中有CWnd,在单独的线程中创建了一个带有WebBrower控件的CWnd。
这是必要的,因为WebBrowser导航到有javascripts运行的URL,这将阻止WebBrowser。所以我用WebBrowser控件在一个单独的线程中防止GUI Thread vom挂起。我也在我的应用程序的主线程中这样做:

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);

这是将WebBrowser控件扩展到我的window.external接口。

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

独立的线程机制在Windows 7和Vista中正常工作。但是在Windows XP中我得到MFC断言。

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

这是我的代码:

    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内部调用Line 305):

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!

因此,m_pmapHWND在Windows XP中为NULL。但在Vista和7中它不是NULL。

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

在Windows XP下,如果我在主线程中创建WebBrowser控件,它会工作。

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

那么问题是什么呢?不同的MFC版本或Windows XP问题?

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

请帮助,这是紧急。非常感谢!

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

Chris

推荐答案

MFC并发现,COM必须在新创建的线程中初始化。

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

所以在线程startroutine我做一个

So in the thread startroutine I'm doing a

CoInitializeEx(NULL,  COINIT_APARTMENTTHREADED);

一切正常。

有趣的是,这是不需要在Windows 7和Vista,只有XP。

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

这篇关于WebBrowser控件(MFC)在单独的线程中工作在Windows 7和Vista,但Windows XP中创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆