我们如何获取我们创建的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();