【发布时间】:2009-07-08 08:47:56
【问题描述】:
我有一个应用程序,它有自己的数据库作为 COM 组件。我需要使用 C# 编写一个链接到该 COM 数据库的已运行实例的应用程序,以便我可以 tweek 数据库中的值并查看它们如何影响应用程序。
过去我曾使用 Monikers 编写过类似的应用程序。
是否可以从 .NET 应用程序访问已准备好运行的 COM 组件?
【问题讨论】:
标签: interop
我有一个应用程序,它有自己的数据库作为 COM 组件。我需要使用 C# 编写一个链接到该 COM 数据库的已运行实例的应用程序,以便我可以 tweek 数据库中的值并查看它们如何影响应用程序。
过去我曾使用 Monikers 编写过类似的应用程序。
是否可以从 .NET 应用程序访问已准备好运行的 COM 组件?
【问题讨论】:
标签: interop
要在 .NET 中使用名字对象,您可能需要查看 Marshal.BindToMoniker,它在内部调用 Win32 BindToMoniker 函数:
void BindToMoniker()
{
string pptxFile;
PowerPoint.Presentation pptx;
pptx = (PowerPoint.Presentation)Marshal.BindToMoniker(pptxFile);
pptx.Application.Visible = Office.MsoTriState.msoTrue;
}
另一种选择是使用 AccessibleObjectFromWindow 从窗口句柄获取 IDispatch 指针(完整示例在 this question 中描述):
// AccessibleObjectFromWindow gets the IDispatch pointer of an object
// that supports IAccessible, which allows us to get to the native OM.
[DllImport("Oleacc.dll")]
private static extern int AccessibleObjectFromWindow(
int hwnd, uint dwObjectID,
byte[] riid,
ref PowerPoint.DocumentWindow ptr);
Andrew Whitechapel 的以下博客文章(与 MS Office 自动化相关)也可能是有关如何在 .NET 中创建和检索 COM 对象的有用资源:
【讨论】: