C# COM server and array of VARIANTs

G

Guest

Hi,

I'm a complete newbie to .NET and C#. In the project that I'm working on, I
have a need to create a C# COM server (and for which I've referred to MSDN
samples). This COM server should pass an array of VARIANTs to a VB 6.0
client. The types of the array elements are not the same, for eg some are
string and others int, etc.

From my preliminary investigation I've found that there is no VARIANT type
in C#, so is there any other way to send across a heterogenous array to the
VB client, keeping marshaling issues in mind?

I would later want to send a 2D array across, so I would want an extendable
solution, if possible.

TIA,
SD
 
N

Nicholas Paldino [.NET/C# MVP]

SD,

If you need to eventually send across a 2D array, you should probably
return object, and then in your method, assign the return value an array of
objects. This way, the signature of the method does not change when you
have to change the output of the method (although I think that you should
have two different methods with different return types, personally).

If you set the return type to object, then COM interop will export that
as a VARIANT return type. You could set the return type to an array of
objects, which will then be exported as an array of variants, but given that
I don't know if your variant array is going to be a jagged array or not, its
safer to just create a second method, or to have the return type be of type
object.

Hope this helps.
 
G

Guest

Yes, I will have 2 separate methods. I'm trying to do them one by one.

Just another doubt. Is this the correct way to pass a VARIANT and a VARIANT
array in the method shown below:

public interface IMyInterface
{
void TestVariantPassing( [In] Object tagVariant,
[Out, MarshalAs(
UnmanagedType.SafeArray,
SafeArraySubType = VarEnum.VT_VARIANT )] out Object[] pVariantArray );
}

TIA,
SD


Nicholas Paldino said:
SD,

If you need to eventually send across a 2D array, you should probably
return object, and then in your method, assign the return value an array of
objects. This way, the signature of the method does not change when you
have to change the output of the method (although I think that you should
have two different methods with different return types, personally).

If you set the return type to object, then COM interop will export that
as a VARIANT return type. You could set the return type to an array of
objects, which will then be exported as an array of variants, but given that
I don't know if your variant array is going to be a jagged array or not, its
safer to just create a second method, or to have the return type be of type
object.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SD said:
Hi,

I'm a complete newbie to .NET and C#. In the project that I'm working on,
I
have a need to create a C# COM server (and for which I've referred to MSDN
samples). This COM server should pass an array of VARIANTs to a VB 6.0
client. The types of the array elements are not the same, for eg some are
string and others int, etc.

From my preliminary investigation I've found that there is no VARIANT
type
in C#, so is there any other way to send across a heterogenous array to
the
VB client, keeping marshaling issues in mind?

I would later want to send a 2D array across, so I would want an
extendable
solution, if possible.

TIA,
SD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top