3

I try to pass vb6 object to my c# code via interop. The VB6 able to reach the c# code, but when try to read the object, it does not have any properties in it. Can anyone pls point out where did i get it wrong?

[VB6]

Set CSharp = CreateObject("CSharp.School")    
Dim p As School.Student
Set p = New School.Student
p.Name = "Bruce Willis"
p.id = 1
    
Call CSharp.Register(p)

[C#]

[Guid("1e54b90a-8909-41f5-abcd-87406c261f90")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface ISchool
{
    Register(object student)
}

Guid("c2d41a00-d000-49e7-abcd-236c6cb9d862")]
[ClassInterface(ClassInterfaceType.None), ProgId("CSharp.School")]
[ComVisible(true)]
public class School : ISchool
{
    public void Register(object student)
    {
        Type myType = student.GetType();
        IList<PropertyInfo> props = new List<PropertyInfo>(student.GetProperties());
        //no property found in the object argument
        Console.WriteLine("Count : " + props.Count.ToString());
    }
}
Nerdynosaur
  • 1,798
  • 9
  • 32
  • 61
  • 1
    You do know that VB6 shipped in 1998 and went out of support in April 2008. The runtime seems to live on forever, but I suspect VB6 test cases went out of fashion more than a decade ago. If I were you, I'd be running this against .NET Framework 3.51 (still in support (I think), but something that dates grom the 2005-2008 time-frame – Flydog57 Aug 15 '22 at 05:11
  • 1
    @Flydog57 there's tons of VB6 code that is still live and has to be maintained. Migrating portions of it to .NET and/or adding new features in .NET is a common upgrade path without converting it all at once. At the moment I'm working on a project doing this against .NET 4.8, no issues. – StayOnTarget Aug 16 '22 at 11:31
  • Does this answer your question? [IntelliSense in custom COM classes in VBA](https://stackoverflow.com/questions/41162601/intellisense-in-custom-com-classes-in-vba) – StayOnTarget Aug 16 '22 at 11:34

1 Answers1

0

I had the same problem, I think it will be difficult to do the way you are trying. In my case the solution was to make a method in VB6 to convert the objects into JSON and pass it to the method as a string. That way in C# it's easy to retrieve the data you need.

In your example it would look something like this:

 {"School":{
  "Student":{
     "Name":"Bruce Willis",
     "id":1
  }   }}