如何获取某个Excel进程的窗口或进程句柄?

发布于 2024-10-13 03:49:41 字数 638 浏览 6 评论 0原文

如何获取属于我们创建的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦中的蝴蝶 2024-10-20 03:49:41

应用程序类似乎没有可调用的 HWnd 属性

Excel.Application 类确实有一个 Hwnd 属性。

回复评论:

我们没有使用 Microsoft.Office.Interop.Excel.dll,而是使用缺少此属性的 Interop.Excel.dll

我通常建议使用 PIA (Microsoft.Office.Interop.Excel.dll)。如果您有充分的理由不想这样做,并且您使用的 RCW 由于某种原因没有公开该属性,则另一种选择是使用后期绑定,例如:

typeof(Excel.Application).InvokeMember("Hwnd", BindingFlags.GetProperty, null, app, null);

The application class seem to have no HWnd property to call

The Excel.Application class does have a property Hwnd.

In response to the comment:

We are not using Microsoft.Office.Interop.Excel.dll but instead Interop.Excel.dll which lacks this property

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.:

typeof(Excel.Application).InvokeMember("Hwnd", BindingFlags.GetProperty, null, app, null);
债姬 2024-10-20 03:49:41

虽然我得到的所有答案都没有帮助,但无论如何还是感谢您的答案。我现在自己找到了一个解决方法:

我现在创建了一个工作进程,它定期检查早于一分钟的 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.

避讳 2024-10-20 03:49:41

您可以使用此构造来调用 Windows API 函数,

    [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    IntPtr hWnd = FindWindow("XLMAIN", null);

但是您必须在对 Application 对象进行任何操作之前执行此操作 - 创建它,设置 Visible=true 并使用下一行代码获取句柄

You can use this construction for call Windows API function

    [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    IntPtr hWnd = FindWindow("XLMAIN", null);

But you must do it BEFORE any operations with Application object - create it, set Visible=true and with next line of code get handle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文