枚举服务上的窗口句柄(从服务创建)以确定其标题
我有一项服务需要运行一个应用程序(我们称之为 X),该应用程序通常使用 Excel 自动化。
在自动化 Excel 时,应用程序 X 通常会让 Excel 显示一个对话框。对话框显示的内容和原因并不重要,不幸的是,这超出了我的控制范围。
我只是希望能够关闭 Excel 中的该对话框。
当代码不作为服务运行时,这当然很容易完成。问题是,在枚举窗口以确定对话框是否打开时,任何 win32 窗口调用都会返回 0 作为句柄。我明白为什么会这样,因为服务是独立运行的,并且预期结果为 0。
该服务也不可能在启用了交互式桌面的本地系统下运行。
我希望在 Excel 进程的线程上使用 GetThreadDesktop,然后打开该桌面来枚举这些窗口,但是从服务运行时该 api 也会返回 0。
与此相关的问题有很多,但大多数问题都是关于想要显示来自服务的对话框以供用户交互。我不想那样做。只需找到并关闭一个对话框即可。
有人发现有一个聪明的解决方法来枚举窗口并返回标题吗?
注意:
- 应用程序 X 不在我的控制范围内。
- 我知道非交互式环境不支持办公自动化。
- 无论如何,我并不想与登录用户的桌面进行交互。
I have a service that needs to run an application (lets call it X) that in tern uses Excel automation.
Often application X when automating Excel will have Excel display a dialog. What and why the dialog displays isn't important, unfortunately that is out of my control.
I'd simply like to be able to close that dialog in Excel.
This of course is easily done when the code doesn't run as a service. The problem is that any win32 window call returns 0 for a handle when enumerating the windows to determine if a dialog is up. I understand why this is as the service runs in isolation and the result of 0 is expected.
It's also not possible that the service runs under local system with interactive desktop enabled.
I was hoping to use the GetThreadDesktop on a thread for the Excel process, then open that Desktop to enumerate those windows, however that api also returns 0 when running from a service.
There are plenty of questions regarding this, however most are about wanting to display a dialog from a service for user interaction. I do not want to do that. Merely find and close a dialog.
Is there a clever workaround to this that someone has discovered to enumerate windows and return the captions ?
Notes:
- Application X is out of my control.
- I know that office automation is not supported in a non-interactive environment.
- I'm not trying to interact with a logged-in user's desktop in anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我过去曾尝试过这一点,并得出结论,实际上不可能通过服务成功实现办公自动化。
I have attempted this in the past and concluded that it was effectively impossible to achieve successful Office automation from a service.
我可以想到一些解决方法 - 我没有尝试过,而且我不能确定它们是否真的有效...
再次强调,我不确定这些方法是否有效。您试图绕过的障碍是有意设计的。
哦,我对你感到同情,不得不处理 Office 自动化......花了太多天试图解决 Word 自动化的奇怪问题。
I can think of a few workarounds - none of which I've ever tried, and I can't say for sure if they actually work or not...
Again, I'm not sure any of these will work. The barrier you're trying to bypass is there by design.
Oh, and I feel for you, having to deal with Office automation... Have spent too many days trying to solve weird problems automating Word.