PonWer
PonWer

Reputation: 87

COM Object - Array as Object instead of Type

Im developing a COM object in C#, VS 2010, .Net 3.5, x86

I used to have a array of structs in the COM Object, which in VBA showed up fine with all the fields and everything.

I switched to class since It created some issues. Now how ever I cant access the properties in the array, since the elements in the array show up as object instead of type.

[Guid("8b65079f-5d98-41e7-9579-1ee384948e4c")]
[ComVisible(true)]
public interface IContact
{
    string Test1 { get; set; }
    string[] Array1 { get; set; }
}

[Guid("8b65089f-5d98-41e7-9579-1ee384948e4c")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Contact : IContact //Used To Be a struct
{
    //[MarshalAs(UnmanagedType.BStr)]
    public string Test1 { get; set; }
    public string[] Array1 { get; set; }
}

public class InContainer
{

        public Contact[] Contacts { get;set;}
        public string[] strings { get; set; }
}

In the debugger I see when viewing the field:

Container.Contacts() -> (0 To 4) As Object

instead of

Container.Contacts() -> (0 To 4) As Contact

What am i missing? Thanks!

Upvotes: 0

Views: 564

Answers (1)

Dirk Vollmar
Dirk Vollmar

Reputation: 176169

Here are some things you can check:

  1. Make sure your container class is correctly declared (I guess it is already COM-visible, but that's not shown in your sample:

    [Guid("EA34C9D6-3EAA-4D44-A8BA-81CC2E79090B")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class InContainer
    
  2. Correctly register your assembly using the correct version of regasm (probably you need the 32-bit one) with the /codebase switch:

    regasm /codebase <myassembly.dll>
    
  3. In VBA, instantiate your container, e.g. using late-binding:

    Sub Test()
        Dim a As Object
        Set a = CreateObject("Issue40977311.InContainer")
    End Sub
    

    This results in the correct types being shown:

    enter image description here

    Alternatively, you can also create a type library using the /tlb switch of regasm:

    regasm /codebase <myassembly.dll> /tlb
    

    and the add the reference to the created .tlb file in VBA via Tools > References > Browse. You can then instantiate your object as follows:

    Sub Test()
        Dim a As Object
        Set a = New Issue40977311.InContainer
    End Sub
    

Upvotes: 2

Related Questions