Reputation: 333
I have a relatively simple C#class that I would like to marshal into a VB project. It looks like this (I simplified a bit for this post):
[Guid("AAAAAAAA-AAAA-AAAA-AAAA-123456789ABC", ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
[ProgId("MyBiz.MyResponse")
[Serializable]
public class MyResponse
{
public bool Success { get; set; }
public int ID{ get; set; }
public string Location{ get; set; }
public ArrayList Messages { get; set; }
}
Messages contains 0 or more strings. I compile this and create a type library to be used by VB6. Everything work well in terms of data getting passed from the simple types, but the Messages variable, while the VB runtime recognizes it as an ArrayList, does not contain any data in it even when it should. What am I missing, in terms of marshaling the data? I know that generics do not marshal, but I believe an ArrayList does. Am I missing an attribute, or something else?
No need to offer alternative solutions, as I am asking this because I want to know how to do it, not because I don't have alternatives if I can get this to work. Thanks!
Upvotes: 2
Views: 960
Reputation: 1324
One way to handle this is to use a COM SafeArray to pass data back and forth from .NET to COM. I have had better luck with this technique than with a ArrayList. The declaration for your Messages could look like:
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
public string[] Messages
This would be seen in VB6 or similar COM client as
Public Messages() as String
a COM SafeArray of Strings.
Upvotes: 1