tags:

views:

408

answers:

2

Hi,

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  A: 

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)]
fbinder
I have done this. I also created the tlb file and added it as an reference in my VB 6.0 project. But i still get the same error. I am returning a string from my C# class to my VB 6.0.
Bhaskar
+1  A: 

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();
}
Ant
I have changed my code to match exactly as yours, but it gives me the error message - "Function or interface marked as restricted, or the function uses a Automation type not supported in Visual Basic"
Bhaskar
You've changed the Guid attributes to use your own guids? I suggest the next step should be opening your generated tlb with OleView (mine's in "c:\Program Files\Microsoft Visual Studio\COMMON\Tools\OLEVIEW.EXE") and having a look at what RegAsm registered for you.Another thought - does RegAsm give any warnings?
Ant
I assume the fact that you've accepted this answer means it worked for you. :)
Ant