如何抑制“此应用程序中仍有活动的 COM 对象”关闭应用程序时出错?
我编写了一些 ATL COM 对象,用于在我的 C++ Builder 应用程序中进行拖放操作。
由于我无法控制的原因,当用户尝试关闭我的应用程序时,它仍然具有活动的 COM 对象。这是因为使用我的对象的其他 COM 客户端似乎缓存了我的 COM 对象并且不释放它们 - 因此,当用户单击“X”关闭我的程序时,我的 COM 对象的引用计数仍然大于零。这会导致用户收到如下不友好的消息:
我希望我的应用程序静默终止而不是问用户这个恼人的问题。
如何抑制此消息?
I've written a few ATL COM objects that are used for drag-and-drop within my C++ Builder application.
Due to reasons beyond my control, my application still has active COM objects when the user attempts to close it. This is because other COM clients that use my objects seem to cache my COM objects and don't release them - as a result, my COM objects still have a reference count greater than zero when the user clicks the "X" to close my program. This results in the user getting an unfriendly message like this:
I would like my application to silently terminate and not ask the user this annoying question.
How do I suppress this message?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
弹出消息由 atlmod.h 中的
TATLModule::AutomationTerminateProc()
回调函数显示。它由TATLModule::InitATLServer()
回调注册,该回调调用 VCL 的AddTerminateProc()
函数。调用TForm::Close()
方法时,它会调用CallTerminationProcs()
来查看应用程序是否可以安全关闭,然后调用TATLModule::AutomationTerminateProc ()
。如果您不提供自己的初始化回调,
TATLModule
构造函数将调用InitATLServer()
。因此,为了避免弹出窗口,只需在项目的主 .cpp 文件中传入一个自定义回调,该回调将执行TATLModule::InitATLServer()
通常执行的所有操作,而不是调用AddTerminateProc()
,例如:The popup message is displayed by the the
TATLModule::AutomationTerminateProc()
callback function in atlmod.h. It is registered by theTATLModule::InitATLServer()
callback, which calls the VCL'sAddTerminateProc()
function. When theTForm::Close()
method is called, it callsCallTerminationProcs()
to see if the app can safely close, which then callsTATLModule::AutomationTerminateProc()
.The
TATLModule
constructor callsInitATLServer()
if you do not provide your own initialization callback. So to avoid the popup, simply pass in a custom callback in your project's main .cpp file that does everythingTATLModule::InitATLServer()
normally does other than callAddTerminateProc()
, eg:我找到了解决这个问题的方法。如果有人发布更好的方法,我将保留这个问题,因为这个方法依赖于 C++ Builder 的 ATL/VCL 库的未记录的内部实现细节。
在主窗体中,place:
VCL 会检查锁定计数,并在引发关闭查询事件后显示这条烦人的消息。仅当锁定计数为 0 时才会显示该消息。因此,我将锁定计数设置为 0,这会导致不显示该消息。我认为这样做是无害的,因为我在ATL/VCL源代码中搜索了使用锁计数的地方,除了检查是否显示此消息的代码之外没有找到任何其他内容。
I found a workaround for this. I'll leave this question open in case anyone posts a better method, since this one is dependent on undocumented internal implementation details of C++ Builder's ATL/VCL libraries.
In the main form, place:
VCL appears to check the lock count and display this annoying message after the close query event is raised. It only displays the message if the lock count is 0. Therefore, I set the lock count to 0 which causes the message to not be shown. I think it's harmless to do, because I searched the ATL/VCL source code for places where the lock count is used, and I didn't find anything other than the code that checks whether to display this message.
当然可以尝试
一下,如果没有更好的办法。
Try
Of course, if there is no better way.