Automation Error in VB 6.0 from a C# class

北城以北 提交于 2019-12-11 06:28:42

问题


I have created a C# class library and I am using it through a VB 6.0 application. But when I try to call any method (which returns a string) it gives me an automation error. The C# class is running fine otherwise.

Any idea why?


回答1:


As fbinder says, you should strong sign your assembly, and use some attributes. The attributes we use (successfully) are:

[ComVisible( true )]
[ClassInterface( ClassInterfaceType.None )]
[Guid( "00000000-0000-0000-0000-000000000000" )]
[ComDefaultInterface( typeof( IExposedClass ) )]
public class ExposedClass : IExposedClass
{
    //need a parameterless constructor - could use the default
    public ExposedClass() { }

    public string GetThing()
    {
        return "blah";
    }
}

[ComVisible( true )]
[Guid( "00000000-0000-0000-0000-000000000000" )]
[InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
public interface IExposedClass
{
    string GetThing();
}



回答2:


You should strong sign your class library, register it with regasm and put this before your class definition:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("Class GUID")]

Also, you should define an Interface to expose the desired methods. The interface should have the attributes:

 [Guid("Interface GUID")]
 [ComVisible(true)]


来源:https://stackoverflow.com/questions/1012655/automation-error-in-vb-6-0-from-a-c-sharp-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!