passing array from c# to COM (SAFERARRAY)

B

Boblemar

Hi !

I'm getting trubles using a com component with c#. I try calling a
method that take one VARIANT * parameter. In the documentation it is
said that this parameter points to a SAFEARRAY of strings.
I found ont the NET that the corresponding type to VARIANT is object in C#.

I tryed something like this :

string[] tsString = new string[2]
tsString[0] = "foo";
tsString[1] = "foo2";

object objMyTable = (object)tsString;

objMyCOMObject.MyFunc(ref(objMyTable));

There I get a COM exception : Exception de HRESULT : 0x80020005
(DISP_E_TYPEMISMATCH)

Someone told me to look at
http://msdn.microsoft.com/library/d...uide/html/cpcondefaultmarshalingforarrays.asp

.... but I dont anderstand what I have to do with it.

Can someone help me ?

Thanx for advance

Bob
 
J

jenp

Hi Bob

I've had a brief look at the link - so forgive me if this is wrong:)

It seems you need to marshall the managed array (tsString in your
example) as a C-Style array.

eg try calling:

objMyCOMObject.MyFunc([MarshalAs(UnmanagedType.LPARRAY, ArraySubType =
UnmanagedType.LPStr, SizeParamIndex =1)] tsString, 2);

I don't think you need to cast to an Object first either.

Cheers

Phil
 
B

Boblemar

Thanx for your answer...

But c# compiler does not accept the '[' when calling the function.
I think [MarshalAs] token only can be called at function declaration
time... not when calling. I can be wrong... but your sample code doesn't
compile at all.

Bob



jenp a écrit :
 
J

jim4u

HI,

I think you should use the lparray. And yes, the MarshalAs attribute is
applied where we declare the external function. Also be aware that if
you want the com to modify the array and have it available in your
function, you may have to 'pin' the array and pass it.

Regards,
Jim
 
B

Boblemar

OK,
but how can I create and then fill a lparray using c# ?
In fact I just want to pass my array to com, but COM don't have to sent
it back to me... Visual Studio has generated a Wrapper... and so I need
to pass may string array.

Bob

jim4u a écrit :
 

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