如何获取某个Excel进程的窗口或进程句柄?
如何获取属于我们创建的Excel应用程序实例的应用程序窗口句柄或Excel进程的进程句柄? 我们使用的是 Interop.Excel.dll 版本 1.3.0.0。应用程序类似乎没有可调用的 HWnd 属性。
请注意,简单地查找名为 excel.exe 的所有进程并不是解决方案,因为我们有许多并行运行的 excel 实例,而我们只想关闭某个实例。
Excel.Application app = new Excel.Application();
// .. do something with excel here
app.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
// this is in some cases still not enough to get excel killed
uint processID;
GetWindowThreadProcessId((IntPtr)hWnd, out processID); // how to get HWnd from this Excel application?
Process.GetProcessById((int)processID).Kill();
How can we get the application window handle or process handle of the excel process which belongs to the excel application instance that we have created?
We are using Interop.Excel.dll Version 1.3.0.0. The application class seem to have no HWnd property to call.
Note that it is no solution to simply find all processes with the name excel.exe because we have many excel instances running in parallel and we only want to close a certain instance.
Excel.Application app = new Excel.Application();
// .. do something with excel here
app.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
// this is in some cases still not enough to get excel killed
uint processID;
GetWindowThreadProcessId((IntPtr)hWnd, out processID); // how to get HWnd from this Excel application?
Process.GetProcessById((int)processID).Kill();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Excel.Application 类确实有一个 Hwnd 属性。
回复评论:
我通常建议使用 PIA (Microsoft.Office.Interop.Excel.dll)。如果您有充分的理由不想这样做,并且您使用的 RCW 由于某种原因没有公开该属性,则另一种选择是使用后期绑定,例如:
The Excel.Application class does have a property Hwnd.
In response to the comment:
I would generally recommending using the PIA (Microsoft.Office.Interop.Excel.dll). If you have a good reason for not wanting to do so, and the RCW you are using doesn't expose the property for some reason, an alternative would be to use late-binding, e.g.:
虽然我得到的所有答案都没有帮助,但无论如何还是感谢您的答案。我现在自己找到了一个解决方法:
我现在创建了一个工作进程,它定期检查早于一分钟的 Excel 进程。由于我的 Excel 处理持续时间不应超过一分钟,因此我可以确定超过一分钟的 Excel 进程已完成,因此我可以终止此进程。
我现在想到的另一种方法是在实例化 Excel 流程之前获取所有 Excel 流程的列表。然后实例化后再次检查。新的pid是新的excel进程。请记住在此周围放置一个lock语句以避免多线程问题。
Although all the answers I got did not help, thanks for the answers anyway. I now found a workaround by myself:
I now created a worker process which periodically checks for excel processes which are older than one minute. Since my excel processing should never last longer than one minute, I can be sure that excel processes older than one minute are done, so I can kill this process.
An other way which comes now to my mind is to get a list of all excel processes before you instantiate your excel process. Then after instantiating you check again. The pid which is new, is the new excel process. Remember to put a lock statement around this to void multithreading issues.
您可以使用此构造来调用 Windows API 函数,
但是您必须在对 Application 对象进行任何操作之前执行此操作 - 创建它,设置 Visible=true 并使用下一行代码获取句柄
You can use this construction for call Windows API function
But you must do it BEFORE any operations with Application object - create it, set Visible=true and with next line of code get handle