471,319 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

passing array from c# to COM (SAFERARRAY)

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/de...gforarrays.asp

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

Can someone help me ?

Thanx for advance

Bob
Feb 16 '06 #1
4 3435
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

Feb 16 '06 #2
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 :
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

Feb 16 '06 #3
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

Feb 16 '06 #4
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 :
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

Feb 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Dave | last post: by
8 posts views Thread by kalinga1234 | last post: by
6 posts views Thread by DeepaK K C | last post: by
3 posts views Thread by Stephen Travis | last post: by
11 posts views Thread by John Pass | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.