【发布时间】:2014-04-30 02:27:11
【问题描述】:
所以我觉得这是非常基本的,但无论出于何种原因,我都无法让它发挥作用。
我有一个 C# 类,我想通过 COM 在 Excel 中使用它。因此,当 C# 类注册到 ROT(并且我的应用程序正在运行)时,当我在 Excel 中执行 GetObject 时,它应该找出我的类是否有任何正在运行的实例,如果有,则返回该实例使用的 Excel。我的问题是,我收到 429 错误 - 它找不到类:(
我不想只贴一堵代码墙(除非有人要求),但我在我的 .exe 上做了一个regasm /codebase,其中包含我想在 Excel 中使用的 COM 可见类。我还指定了自己的 GUID 和 ProgID,ProgID 是我在 Excel 中用来获取对象的。
大部分代码是由其他人编写的,我必须弄清楚如何修复它,所以下面的代码 sn-ps 是我不知道他们做什么但可能是有用的附加信息:
// this gets called before my class (MyApp) is made
RegistrationServices services = new RegistrationServices();
return services.RegisterTypeForComClients(typeof(MyApp), RegistrationClassContext.LocalServer, RegistrationConnectionType.MultipleUse);
// this gets called right after my class (MyApp) is made
if (MyApp == null) return;
Guid guid = typeof(MyApp).GUID;
RegisterActiveObject(MyApp, ref guid, 0, out _RegActiveObjectKey);
这里只是一些关于课程的信息,如果有用的话:
[Guid("FDDBEB42-9E44-45fb-806B-EA535E2947F2")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("MyExe.MyApp")]
[ComVisible(true)]
public class MyApp : IDisposable
{
// ...
}
编辑:我什至使用以下代码在 ROT 中明确注册了我的课程:
IRunningObjectTable rot = null;
IMoniker pMoniker = null;
string filename = string.Empty;
int retVal = GetRunningObjectTable(0, out rot);
retVal = CreateFileMoniker(filename, out pMoniker);
retVal = rot.Register(1, _MyApp, pMoniker);
【问题讨论】:
-
您是否考虑过使用 InterOp...? .Net 有大量示例来支持 Microsoft.Interop