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

Another thread on passing arrays between managed C++ and C#

Hello,

I have an unmanaged C++ class that contains a member function similar to this
one:

void MyUnmanagedClass::get_data(unsigned char **data, int *count) {
*count = 100;
*data = new unsigned char[100];
// ... put some data into data
}

How does the managed C++ wrapper function have to look like so that I can
call it like that from C#:

MyManagedClass x = new MyManagedClass();
byte[] data;

x.get_data(out data);
What I came up with so far is:

using namespace System;

void MyManagedClass::get_data(Byte (&data)[]) {
int count;
unsigned char *tmp;

unmanaged_class->get_data(&tmp, &count);

data = new Byte[count];

for (int i = 0; i < count; i++)
data[i] = tmp[i];

delete tmp;
}

.... but that requires a x.get_data(ref data); in C#, what I want is
an "out" data because I'd like to prevent the error C# gives me if I don't
initialize the ref data to null.

I'm not sure if I've even followed the right track. Whats the right way to do
that?

thanks,
Max
Nov 16 '05 #1
3 4564
Hi Markus,
I have an unmanaged C++ class that contains a member function similar to this one:

void MyUnmanagedClass::get_data(unsigned char **data, int *count) {
*count = 100;
*data = new unsigned char[100];
// ... put some data into data
}

How does the managed C++ wrapper function have to look like so that I can
call it like that from C#:

MyManagedClass x = new MyManagedClass();
byte[] data;

x.get_data(out data);
What I came up with so far is:

using namespace System;

void MyManagedClass::get_data(Byte (&data)[]) {
int count;
unsigned char *tmp;

unmanaged_class->get_data(&tmp, &count);

data = new Byte[count];

for (int i = 0; i < count; i++)
data[i] = tmp[i];

delete tmp;
}

... but that requires a x.get_data(ref data); in C#, what I want is
an "out" data because I'd like to prevent the error C# gives me if I don't
initialize the ref data to null.

I'm not sure if I've even followed the right track. Whats the right way to do that?


You are. You're just missing a couple of things. Try instead:

using namespace System::Runtime::InteropServices;

void get_data([Out] Byte (*data)[]) {
int count;
unsigned char *tmp;

unmanaged_class->get_data(&tmp, &count);

*data = new Byte[count];
Marshal::Copy(IntPtr(tmp), *data, 0, count);
delete tmp;
}

--
Tomas Restrepo
to***@mvps.org
Nov 16 '05 #2
In article <u#**************@tk2msftngp13.phx.gbl>, Tomas Restrepo (MVP) wrote:

Hi Tomas,
void get_data([Out] Byte (*data)[]) {


great, thanks for the hint! I'll have to look up more information about
the Interop. namespace.

Can you recommend some book about C++.net that explains this and other
little details? I've read two fat books from Microsoft so far (Inside C#
and Programming Microsoft Windows with C#), but it seems like I need more
some times. "Programming with Managed Extensions for MSVC++.NETv2003"
looks good (from the table of contents).. any opinions on this one?

thanks,
Max

Nov 16 '05 #3
Hi Markus,
void get_data([Out] Byte (*data)[]) {
great, thanks for the hint! I'll have to look up more information about
the Interop. namespace.


Indeed do! The Marshal class especially has all sorts of useful little
thingies!

Can you recommend some book about C++.net that explains this and other
little details? I've read two fat books from Microsoft so far (Inside C#
and Programming Microsoft Windows with C#), but it seems like I need more
some times. "Programming with Managed Extensions for MSVC++.NETv2003"
looks good (from the table of contents).. any opinions on this one?

Unfortunately, I can't say I recommend any one specially, since I've read
few of them. Wrox "Visual C++ .NET" book has some good chapters, and a few
other ugly ones. I'd probably pick a copy of Richard Grimes VC++.NET book
without any hesitation, though, as his a great author and knows his stuff.
There's also one by some of the VC++ team guys thats probably very nice,
too, although as I say, I haven't read it myself :)

--
Tomas Restrepo
to****@mvps.org
Nov 16 '05 #4

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

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
5
by: GeRmIc | last post by:
Hi, I am doing an interop from unmanaged code to C#. How do i pass an ArrayList pointer from an unmanaged code, (structres are easily passed by between C# and C). //This is the C code ...
3
by: Germic | last post by:
Hi, I want to create an Hashtable in C# and pass an pointer to the hashtable to a C code. Later, the C code could reference the hashtable by passing the pointer to the hashtable as a ref...
8
by: John Salerno | last post by:
private static void InitializeArrays() { FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read); StreamReader readSwitches = new StreamReader(stream); int i = 0; using...
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
17
by: mr.resistor | last post by:
hey i am having a few problems calling a C DLL from C#. i am using a simple function that takes an array of floats and an integer as an input, but i cannot seem to get it to work. when i try to...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.