使用CoInitializeEx初始化MFC SDI App中的COM时出现的问题 [英] Problems when initialise COM in MFC SDI App with CoInitializeEx

查看:146
本文介绍了使用CoInitializeEx初始化MFC SDI App中的COM时出现的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MFC SDI GUI应用程序,其中在启动时设置了一个CMyApp成员变量,这需要一个COM接口.

因此,在CMyApp :: InitInstance()中,我调用了:

I have a MFC SDI GUI application in which I have a CMyApp member variable set at start-up which requires a COM interface.

So, in CMyApp::InitInstance() I have called:

CoInitializeEx(NULL, COINIT_MULTITHREADED);


但是,当我关闭该应用程序时,它会崩溃.

如果我使用


However, when I shut down the app it crashes.

If I use

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) 

很好.

谁能告诉我问题是什么?我在应用程序的其他地方使用了

it is fine.

Can anyone please tell me what the problem is? I have used

CoInitializeEx(NULL, COINIT_MULTITHREADED)

,例如在MyDoc.cpp中,它不会引起任何问题.我没有使用多个线程,所以我不需要使用MULTITHREADED版本,但是我不明白为什么它会导致关机时崩溃(当它试图以Microsoft代码的深度发布m_pInterace时) ).


另外,CMyApp :: InitInstance()是初始化与所查看文档无关的GUI成员变量的最佳位置.它将XMLDOMDocPtr存储到文件,该文件包含指向GUI后面使用的某些信息的文件路径.

非常感谢.

elsewhere in the application, e.g. in MyDoc.cpp and it does not cause any problem. I am not using multiple threads so I do not need to use the MULTITHREADED version, but i don''t understand why it is causing the crash at shutdown (when it is trying to release an m_pInterace in the depths of microsoft''s code).


Also, is CMyApp::InitInstance() the best place to initialise a member variable for the GUI that is not related to a document that is viewed. It stores an XMLDOMDocPtr to a file that contains a file path to some information that is used behind the GUI.

Many thanks.

推荐答案

要做的第一件事是检查调用返回的内容.您可以在此处找到详细信息 [ ^ ],但简单的解释是:仅当您的调用CoInitialize成功时,才应调用CoUninitialise:
"要在线程上正常关闭COM库,每次对CoInitialize或CoInitializeEx的成功调用,包括任何返回S_FALSE的调用,都必须通过对CoUninitialize的相应调用来平衡. "

我的猜测是XMLDOMDocPtr用APARTMENTTHREADED模型调用CoInitialize,然后您从调用中获得RPC_E_CHANGED_MODE.如果是这种情况,则不应调用CoUninitialize.
The first thing to do would be to check what the call returns. You can find more details here[^] but the simple explanation is: you should only call CoUninitialise if your call to CoInitialize SUCCEEDED:
"To close the COM library gracefully on a thread, each successful call to CoInitialize or CoInitializeEx, including any call that returns S_FALSE, must be balanced by a corresponding call to CoUninitialize."

My guess would be that the XMLDOMDocPtr calls CoInitialize with the APARTMENTTHREADED model and you get a RPC_E_CHANGED_MODE from your call. If this is the case you should not call CoUninitialize.


这篇关于使用CoInitializeEx初始化MFC SDI App中的COM时出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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