在 Office 应用程序之间共享加载项
我用 C# 和 .NET 2.0 编写了一个 Office 共享加载项。 它对所有 Office 应用程序使用相同的 COM Shim。 目前,每个实例都会创建自己的类实例 - 它们不知道加载项正在另一个应用程序上运行这一事实。
是否可以做到这样,例如,当 Word 加载项启动时,它可以检测到 Excel 加载项已经在运行? 他们之间可以沟通吗?
假设我的 dll 名为 Addin.dll。 例如,当 Word 打开时,它会运行 Addin.dll 中实现 IExtensibility 接口的代码,并创建一个类,我们将其称为 WordAddin。 当 Excel 打开时,它还会运行 Addin.dll 中的代码,并创建 ExcelAddin 的实例。 现在,假设 Word 正在运行并且 WordAddin 存在。 打开 Excel 时,Addin.dll 会加载到不同的 AppDomain 中。 它不知道 WordAddin 的存在。 我希望 ExcelAddin 知道 WordAddin 存在,并能够与其通信,也许通过创建两者的父类。
有人知道怎么做吗?
I have written an Office shared Add-in with C# and .NET 2.0. It uses the same COM Shim for all office apps. Currently, every instance creates its own instance of my class -- they have no knowledge of the fact that the add-in is running on another application.
Is it possible to make it so that, say, when the Word add-in launches it can detect that the Excel add-in is already running? Can they communicate between each other?
Let's say my dll is called Addin.dll. When, for example, Word opens, it runs the code in Addin.dll that implements the IExtensibility interface, and creates a class, let's call it WordAddin. When Excel opens, it also runs the code in Addin.dll, and it creates an instance of ExcelAddin. Now, suppose that Word is running and WordAddin exists. When Excel is opened, Addin.dll is loaded into a different AppDomain. It has no knowledge that WordAddin exists. I want ExcelAddin to have know WordAddin exists, and be able to communicate with it, perhaps through a parent class that creates both.
Anyone know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Microsoft 消息队列 (MSMQ) 来执行此操作:在 C# 中使用 Microsoft 消息队列进行进程间通信
此代码项目文章展示了如何检测正在运行的实例以及如何在它们之间复制命令行参数:
单实例 C# 应用程序 - for .NET 2.0
不完全是您所要求的,但可能会感兴趣:检测正在运行的程序实例并传递信息
You could do this using a Microsoft Message Queue (MSMQ): Use Microsoft Message Queuing in C# for inter-process communication
This code project article shows how to detect a running instance and how to copy command line parameters between them:
Single-Instance C# Application - for .NET 2.0
Not quite what you are asking but might be of interest: Detecting a running instance of a program and passing it information
我最终使用命名管道在进程之间进行通信
I ended up using named pipes to communicate between processes
事实上我已经完全按照你的要求做了。 我选择了老式的 Windows 消息。 没什么特别的——所以它会正常工作! :-)
让您的外接程序使用 NativeWindow 类创建一个“窗口”,并为其指定一个预先确定的唯一名称。 然后使用 GetWindow 和 GetWindowText 当您的插件启动时。 使用 SendMessage 或 PostMessage 进行通信。 就这么简单。
I have in fact done exactly what you're asking about. I went for old fashioned windows messages. Nothing fancy - so it will just work! :-)
Let your addin create a "window" using the NativeWindow class and give it a predetermned, unique name. Then search for another instance of such a window using GetWindow and GetWindowText when your addin launches. Communicate using SendMessage or PostMessage. Easy as that.