COM自动化后Excel进程不会退出 [英] Excel process won't quit after COM automation

查看:407
本文介绍了COM自动化后Excel进程不会退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动化的Excel(2003年),独立安装在目标机器上的默认版本的特定版本。为此,我公司采用以下步骤:


  • 启动Excel通过提供所需的可执行文件的的CreateProcess

  • 找到的 EnumWindows的的主力和获取窗口的 EnumChildWindows

  • 使用的 AccessibleObjectFromWindow 的从Excel对象模型中获取一个对象

  • 通过COM智能指针做自动化的东西

这一切工作正常,但通话结束后,Excel进程不会终止的退出的的的 ExcelApplication 的对象。相同的设置与Word按预期正常运行,并且进程终止,因为它应该。任何想法,为什么Excel中的行为不同会大大AP preciated。

从.NET,在那里晃来晃去COM引用的原因自动化Excel时,我已经知道了类似的问题。但是,如果这是在我的C ++情况的原因还有,我不明白为什么。即使我什么都不做,除了退出的,这个过程仍然活着:

  / *创建过程中,得到处理,以访问Excel窗口* /Excel11 :: _ ApplicationPtr excelApplication;尝试
{
  Excel11 :: WindowPtr ::接口* pInterface;
  如果(:: AccessibleObjectFromWindow(hwndExcelAccessible,OBJID_NATIVEOM,IID_IDispatch,reinter pret_cast<无效**指(安培; pInterface))== S_OK)
  {
    excelApplication = pInterface->应用;
    pInterface->发行();
  }
}
赶上(_com_error急症){/ *略* /}excelApplication->退出();


解决方案

你在做什么是由微软不再支持:的 http://support.microsoft.com/kb/257757 (它谈论的服务器端code,但无论它引用架构的任何非互动解决方案)。

尽管如此,我相信我碰到了一些净code而回了类似的问题和解决方案是,当用它做强行杀死Excel中。我不记得确切的原因为什么必须这样做,虽然。

I need to automate a specific version of Excel (2003), independent of the default version installed on the target machine. For this purpose, I am using the following steps:

  • launch Excel by supplying the desired executable to CreateProcess
  • find the main and accessible window with EnumWindows and EnumChildWindows
  • use AccessibleObjectFromWindow to get an object from the Excel object model
  • do the automation stuff through COM smart pointers

This all works fine, but the Excel process will not terminate after a call to Quit on the ExcelApplication object. The same setup with Word works as intended, and the process terminates as it should. Any idea about why Excel behaves differently would be much appreciated.

I've read about similar problems when automating Excel from .NET, where dangling COM references are the reason. However, if that is the reason in my C++ case as well, I don't understand why. Even if I do nothing except Quit, the process still remains alive:

/* create process, get handle to accessible Excel window */

Excel11::_ApplicationPtr excelApplication;

try
{
  Excel11::WindowPtr::Interface* pInterface;
  if ( ::AccessibleObjectFromWindow( hwndExcelAccessible, OBJID_NATIVEOM, IID_IDispatch, reinterpret_cast< void** >( &pInterface ) ) == S_OK )
  {
    excelApplication = pInterface->Application;
    pInterface->Release();
  }
}
catch ( _com_error& e ) { /* omitted */ }

excelApplication->Quit();

解决方案

What you are doing is no longer supported by Microsoft: http://support.microsoft.com/kb/257757 (it talks about server-side code, but it references any non-interactive solutions regardless of architecture).

That notwithstanding, I believe I ran into a similar problem in some .Net code a while back and the solution was to forcibly kill Excel when done with it. I can't remember the exact reason why that had to be done, though.

这篇关于COM自动化后Excel进程不会退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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