如何使用Process ID来获得Excel的实例或Excel实例CLSID? [英] How to get Excel instance or Excel instance CLSID using the Process ID?

查看:802
本文介绍了如何使用Process ID来获得Excel的实例或Excel实例CLSID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#的工作,我需要通过它来获取Excel中的特定实例的进程ID;我得到一些从其他应用程序所需要的实例的进程ID,但我不知道自己能做什么,我不知道我怎样才能得到的Excel的运行实例给他的进程ID。

I'm working with C#, I need to obtain a specific instance of excel by it's process ID; I get the Process ID of the instance that I need from another application but I don't know what else to do, I don't know how can I get a running instance of excel given his process ID.

我已经研究了网络上很多,但我只看到我不能使用通过Marshal.GetActiveObject(...)或Marshal.BindToMoniker(...)的例子,因为第一个返回第一个Excel的实例,在ROT注册的,而不是precisely一个,我需要的,而第二个要求,你想获得实例之前保存Excel文件。

I have researched a lot on the web, but I have only see examples of using Marshal.GetActiveObject(...) or Marshal.BindToMoniker(...), which I can't use since the first one returns the first Excel instance registered in the ROT and not precisely the one that I need, and the second one requires that you save the excel file before trying to get the instance.

另外,如果我在那里能得到的Excel实例,我需要的CLSID,使用过程中的ID,那么我或许可以致电

Also, if I where able to get the CLSID of the excel instance that I need, using the process ID, then I may be able to call

GetActiveObject(ref _guid, _ptr, out objApp);

这最终将返回Excel的情况下,我需要的。

that ultimately will return the excel instance that I need.

推荐答案

一旦你通过进程ID标识的过程中,你可以得到<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx\">Process.MainWindowHandle然后使用与 AccessibleObjectFromWindow API 一起可以访问到Excel对象模型的过程。

Once you identify the process via the process id, you can get the Process.MainWindowHandle and then use that along with the AccessibleObjectFromWindow API to get access to the Excel object model for that process.

文章获取应用程序对象在匀自动化加载由安德鲁白教堂详细描述了这一技术,样本code一起。

The article Getting the Application Object in a Shimmed Automation Add-in by Andrew Whitechapel describes this technique in detail, along with sample code.

这文章你的关键code开始在该行:

The key code in that article for you begins at the line:

int hwnd = (int)Process.GetCurrentProcess().MainWindowHandle

而你的情况可能看起来更像是:

Which in your case might look more like:

int excelId = 1234; // Change as appropriate!
int hwnd = (int)Process.GetProcessById(excelId).MainWindowHandle

其中'excelId'是您正在寻找的进程ID号。否则,如本文中给出的code应该基本上相同。 (忽略他的code是一个附加书面的事实;这方面会不会影响你在这里的需求,所以就忽略吧)

where the 'excelId' is the process id number that you are looking for. Otherwise, the code should be essentially the same as given in the article. (Ignore the fact that his code is written for an add-in; that aspect won't affect your needs here, so just ignore it.)

如果你没有进程ID,那么你就需要使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname.aspx\">Process.GetProcessesByName,因此,你可以列举每个Excel的实例中的每一个,抢控制与访问对象模型,可以根据需要。

If you do not have the process id, then you you would want to use Process.GetProcessesByName, whereby you could enumerate each one and grab control of each Excel instance with access to the object model, as needed.

希望这有助于

迈克

这篇关于如何使用Process ID来获得Excel的实例或Excel实例CLSID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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