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 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.
> 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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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?...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
| |