Silverlight 4 和 COM 互操作
我创建了一个 ComVisible 类:
[Guid("73a3f91f-baa9-46ab-94b8-e526c22054a4"), ComVisible(true)]
public interface ITest
{
void Foo();
}
[Guid("99f72d92-b302-4fde-89bb-2dac899f5a48"), ComVisible(true)]
public class Class1 : ITest
{
public void Foo() { }
}
并将其注册
regasm ComClassTest.dll /tlb:ComClassTest.tlb
到注册表中。 当我尝试在 Silverlight 4 浏览器外、增强的信任应用程序中调用它时,如下所示:
var foo = AutomationFactory.CreateObject("ComClassTest.Class1");
我收到异常“{System.Exception:无法为指定的 ProgID 创建对象实例”。
但是,如果复制 ComClassTest,我可以在正常的 C# 控制台应用程序中调用 AutomationFactory.CreateObject("Word.Application") 而不会出现异常,并调用 Activator.CreateInstance(Type.GetTypeFromProgID("ComClassTest.Class1")) .dll 进入 bin 目录。
我忘记了什么?
I've created a ComVisible-class:
[Guid("73a3f91f-baa9-46ab-94b8-e526c22054a4"), ComVisible(true)]
public interface ITest
{
void Foo();
}
[Guid("99f72d92-b302-4fde-89bb-2dac899f5a48"), ComVisible(true)]
public class Class1 : ITest
{
public void Foo() { }
}
and registered it via
regasm ComClassTest.dll /tlb:ComClassTest.tlb
into the registry.
When I try to call it in my Silverlight 4 out-of-browser, elevated trust application like this:
var foo = AutomationFactory.CreateObject("ComClassTest.Class1");
I get an exception "{System.Exception: Failed to create an object instance for the specified ProgID."
However, I am able to call AutomationFactory.CreateObject("Word.Application") without an Exception and to call Activator.CreateInstance(Type.GetTypeFromProgID("ComClassTest.Class1")) in a normal C#-console application if I copy the ComClassTest.dll into the bin-directory.
What have I forgotton?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先要做的是测试您是否可以从其他地方(例如 VBScript)创建对象。创建一个包含以下内容的 .vbs 文件:-
如果没有生成错误,则表明 SL OOB 对此特别不满意,否则您的问题与 Silverlight 根本无关。
考虑对您的 COM 代码进行以下更改。
在程序集级别指定
ComVisible(true)
通常更容易。您可以从“程序集信息”对话框上项目属性的“应用程序”选项卡执行此操作。您还可以让 Visual Studio 使用 COM 注册程序集并使用项目属性的“生成具体说明您要公开的
ComInterfaceType
是个好主意。如果直接公开类接口,事情会变得非常混乱,通常您只希望使用您定义的接口,并且这是该类的默认接口。此外,最好坚持类的默认接口的 COM 命名约定。
最后(对于您的情况可能至关重要)明确用于该类的 ProgId 是一个好主意。
应用上述,我们得到:-
First thing to do is test that you can create the object from somewhere else such as VBScript. Create a .vbs file with the content:-
If that doesn't generate an error then there is something specifically that SL OOB is upset with otherwise your problem isn't really related to Silverlight at all.
Consider making the following changes to your COM code.
Its often easier to specify
ComVisible(true)
at the assembly level. You can do that from the application tab of the project properties on the Assembly Information dialog. You can also get Visual Studio to register the assembly with COM and build time using the option found on the build tab of the project properties.Its a good idea to be specific about the
ComInterfaceType
you want to expose.Things get really messy if you expose the class interface directly generally you only want the interface you have defined to be used and that this be the default interface for the class. In addition it probably better to stick to COM naming conventions for the default interface of a class.
Finally (and possibly crucially in your case) its a good idea to be explicit about the ProgId to use for the class.
Applying the above and we get:-