3

When I marshal my function returning a string[] as UnmanagedType.Struct with SafeArraySubType = VarEnum.VT_ARRAY as in

namespace StackOverflow { [ComVisible(true)] [Guid("4BDC43D4-8FD7-4F58-BEE5-E57C3C144C1B")] public class Array { [return: MarshalAs(UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY)] public string[] StringArray() { return new string[] { "foo", "bar" }; } } } 

I was expecting to get a variant (UnmanagedType Enumeration)

Struct
A VARIANT, which is used to marshal managed formatted classes and value types.

However the VBScript code

WScript.Echo TypeName(CreateObject("StackOverflow.Array").StringArray) 

reports String() (which is not a Variant() and therefore I will get type missmatch errors later when accesing the array).

When I change my code to either

public object[] ObjectArray() { return new object[] { "foo", "bar" }; } public object PlainObject() { return new object[] { "foo", "bar" }; } 

the VBScript code seems to works fine, however I would like to know why I cannot return a string[] and manually marshall it to a variant containing a safearray of variants.

It seems that the SafeArraySubType has no effect. What is my mistake?

1 Answer 1

2

Thats because the SafeArraySubType only applies to SafeArrays.

MarshalAs (UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)] 
Sign up to request clarification or add additional context in comments.

2 Comments

I find that unlikely given stackoverflow.com/questions/5079200/… uses UnmanagedType.Struct, SafeArraySubType = VarEnum.VT_ARRAY.
That's because the SafeArraySubType is ignored for structs. Structs by the nature are already a "variant array" (msdn.microsoft.com/en-us/library/vstudio/…) because they have their properties layout in sequence.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.