473,387 Members | 1,585 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,387 software developers and data experts.

pass array from managed code to COM object

Dear all,
I have written a C++ com object:
STDMETHOD(myfunc)(INT* array)

Now I need to pass an array from c# to com. I generated a COM interop and it
the signature of the function is

myfunc(ref int)

in C# I have an array of ints.

If I call

myfunc(array[0])

and try to access from COM object the array[1],2.... they are incorrect.
Only array[0] is correct. What do I do wrong?

Thanks a lot,

Boni
Mar 16 '06 #1
3 2276

"Boni" <no****@parampam.pam> wrote in message
news:u1**************@tk2msftngp13.phx.gbl...
| Dear all,
| I have written a C++ com object:
| STDMETHOD(myfunc)(INT* array)
|

Well, you declared the function to pass a pointer to an int.

|
|
| Now I need to pass an array from c# to com. I generated a COM interop and
it
| the signature of the function is
|
| myfunc(ref int)
|

See, ref int, is a pointer to an int.

| in C# I have an array of ints.
|
| If I call
|
| myfunc(array[0])
|

Here you pass the adress of the first element in the array of int's.

| and try to access from COM object the array[1],2.... they are incorrect.
| Only array[0] is correct. What do I do wrong?
|

The first element is correct, that's exactly what you have asked for, right?

You need to declare the argument as an array of int's in your C++ code and
pass the array reference from C#.

Willy.
Mar 16 '06 #2
> You need to declare the argument as an array of int's in your C++ code and
pass the array reference from C#.

Can you give a pseudocode on C++ and C#side? I am not sure, how should I do
that.
I don't know size of an array on C++ side, it is passed thru other function,
and I think INT[] array will make no difference with pointer.
Mar 16 '06 #3

"Boni" <no****@parampam.pam> wrote in message
news:ee**************@TK2MSFTNGP09.phx.gbl...
|> You need to declare the argument as an array of int's in your C++ code
and
| > pass the array reference from C#.
| Can you give a pseudocode on C++ and C#side? I am not sure, how should I
do
| that.
| I don't know size of an array on C++ side, it is passed thru other
function,
| and I think INT[] array will make no difference with pointer.
|
|

Well, there different ways to pass an array to/from COM.
If you want to pass a self describing array you need to use the SAFEARRAY
type, this is also the default used by the COM marshaler.

Say you have the following in IDL:
.... HRESULT PassStringArray([in] SAFEARRAY(BSTR) arr);

Your implementation should look something like this:

STDMETHODIMP CTest::PassStringArray(SAFEARRAY * arr)
{
BSTR* temp;
SafeArrayAccessData(arr, (void**)&temp);
long ubound;
SafeArrayGetUBound(arr, 1, &ubound);
for(int i = 0; i <= ubound; i++)
printf_s("%S\n", temp[i]);
SafeArrayUnaccessData(arr);
return S_OK;
}

And you call it from C# like:
....
string[] arr = new string[] { "Hello", "World" };
obj.PassStringArray(arr);

Another option is to pass native C style arrays,
Please consult "Default Marshaling for Arrays" in MSDN for more info.

Willy.
Mar 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: wbaccay | last post by:
I have a byte of binary data received from a NetworkStream (C# code) that I need to pass to the IWMWriter object in a DLL written in Managed extensions for C++ (since the Windows Media SDK is not...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
5
by: apm | last post by:
Any and all: Is there an efficient way to pass a large array from .NET to COM? Can references (or pointer) be passed from COM to NET and NET to COM without the object it refers to being copied?...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
3
by: Mark Dunmill | last post by:
I can't create a Constant/Read-only array field in managed C++ classes - doesn't allow the keyword const pointer to const object on array fields in managed C++ classes. e.g. Want to define a...
9
by: mupe | last post by:
Hi, i have a problem with a Type Library, which is written in C++. I am developing an application in C#.NET and have to use functions from this COM-Type Library. When I use these functions in...
1
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.