Search code examples
c#vb.netcollectionscominterop

Marshalling Microsoft.VisualBasic.Collection to COM


I have a C# class that has a property(name is List) of type Microsoft.VisualBasic.Collection. We need to exposed this property to COM. For that, I was writing an interface for my class such that the property would marshaled as of UnmanagedType.IDispatch. Something like this:

 [DispId(0x68030000)]
 Collection List { [DispId(0x68030000)] [return: MarshalAs(UnmanagedType.IDispatch)] get; }

This piece of code was earlier in VB and was being used by C++ as tye VT_DISPATCH. However, while building the C# library, I get the following error:

C:\Program Files (x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets(4335,5): error MSB3212: The assembly "Name.dll" could not be converted to a type library. Type library exporter encountered an error while processing 'Namespace.InterfaceName.get_List(#0), ProjectName'. Error: Error loading type library/DLL.

I read through few of the posts online which suggested that such errors might cause because of repetitive GUIDs. But that is not the case. I tried with multiple GUIDs. I don't feel this is an issue with any other attribute that I had set on my Interface since, I am able to marshal other properties and function calls ( some of them use primitive types and others use custom classes) successfully.

This is how it is consumed in the consuming C++ application:

LPDISPATCH result;
InvokeHelper(0x68030000, DISPATCH_PROPERTYGET, VT_DISPATCH, (void*)&result, NULL);
return result;

This has become a go live issue with the client and I really do not have much time to continue investigation since this is due tomorrow.

Appreciate any help!


Solution

  • I don't think Microsoft.VisualBasic.Collection is COM-visible. Therefore, you cannot use this type as a return value or parameter in a COM class or interface. However, ICollection (which Microsoft.VisualBasic.Collection implements) is COM visible. If that would suit your purposes, use that as the type of your property rather than Microsoft.VisualBasic.Collection.