473,544 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 MyUnmanagedClas s::get_data(uns igned 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(&tm p, &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 4568
Hi Markus,
I have an unmanaged C++ class that contains a member function similar to this one:

void MyUnmanagedClas s::get_data(uns igned 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(&tm p, &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 ::InteropServic es;

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

unmanaged_class->get_data(&tm p, &count);

*data = new Byte[count];
Marshal::Copy(I ntPtr(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. "Programmin g 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. "Programmin g 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
6513
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 courses to pass the correct option value and then be displayed at the following URL: http://www.dslextreme.com/users/kevinlyons/selectResults.html ...
188
7061
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 - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
5
7199
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 NameStruct lnames; //This is a structure in C#
3
1460
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 parameter. what is the correct way to do this? Thanks,
8
1074
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 (readSwitches) { string row = readSwitches.ReadToEnd();
3
3662
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 runtime error, " Attempting to call into managed code without transitioning out first. Do not attempt to run managed code inside low-level native...
17
3659
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 compile i get the following error: Attempted to read or write protected memory the C function should not be manipulating the input arra, only...
17
7220
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 to show the array data to the end user. Can I do that? How?
6
3875
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 definined as taking a char * as an argument? For example the C++ header says SDCERR GetCurrentConfig(DWORD *num, char *name); I am using Uint for the *num...
0
7373
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7625
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7389
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7717
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5928
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.