473,241 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 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 3489
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
6
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
3
by: Stephen Travis | last post by:
I'm trying to write a subroutine that will fill an array of some type with several objects of that type. The subroutine works fine if I directly reference the array but if I pass the array as an...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.