473,324 Members | 2,473 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,324 software developers and data experts.

Converting native arrays to managed arrays

Hi all,

I'm writing C++/CLI code (compiled with /clr) that accepts a native char array
(and its length) and calls a managed routine that expects an array of
System::Byte. I was just wondering if there is any magic that I'm missing that
easily marshals the native array into a managed array, or do I need to write my
own code to allocate the managed array<and loop through it copying the native
data into the managed array?

TIA - Bob
Feb 27 '08 #1
5 6065
"Bob Altman" <rd*@nospam.nospamwrote in message
news:ep**************@TK2MSFTNGP05.phx.gbl...
Hi all,

I'm writing C++/CLI code (compiled with /clr) that accepts a native char
array (and its length) and calls a managed routine that expects an array
of System::Byte. I was just wondering if there is any magic that I'm
missing that easily marshals the native array into a managed array, or do
I need to write my own code to allocate the managed array<and loop
through it copying the native data into the managed array?
You can use System::Runtime::InteropServices::Marshal::Copy(), which does
the "loop through it copying " part for you :)

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
>
TIA - Bob
Feb 27 '08 #2
You can use System::Runtime::InteropServices::Marshal::Copy(), which does the
"loop through it copying " part for you :)

Mark
Thanks Mark!
Feb 27 '08 #3
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in message
news:BD264A84-
You can use System::Runtime::InteropServices::Marshal::Copy(), which does the
"loop through it copying " part for you :)
I've tried several variations on the following code, but it complains that it
can't find an overload for Marshal::Copy that it likes. I suspect that it
doesn't like the boxed handle to an IntPtr that I think I'm trying to feed it
(by way of the gcnew IntPtr() code). How do I cast a char* to an IntPtr?

const char* value = <some array of bytes>
int size = 15;
array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
Marshal::Copy(gcnew IntPtr((void*)value), myArray, 0, size);
Feb 27 '08 #4
"Bob Altman" <rd*@nospam.nospamwrote in message
news:uR**************@TK2MSFTNGP02.phx.gbl...
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in
message news:BD264A84-
>You can use System::Runtime::InteropServices::Marshal::Copy(), which does
the "loop through it copying " part for you :)

I've tried several variations on the following code, but it complains that
it can't find an overload for Marshal::Copy that it likes. I suspect that
it doesn't like the boxed handle to an IntPtr that I think I'm trying to
feed it (by way of the gcnew IntPtr() code). How do I cast a char* to an
IntPtr?

const char* value = <some array of bytes>
int size = 15;
array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
Marshal::Copy(gcnew IntPtr((void*)value), myArray, 0, size);

Maybe try something like this:

const char *value = new char[100];
int size = 15;
array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
Marshal::Copy(IntPtr(const_cast<void*>(static_cast <const void*>(value))),
myArray, 0, size);
delete[] value;

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++

Feb 27 '08 #5
Also note that in my code sample, it ends up using the

Marshal::.Copy Method (IntPtr, array<Byte>[]()[], Int32, Int32)

form of Marshall::Copy() since the array is a Byte (unsigned char) array.

Cheers,
Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
"Bob Altman" <rd*@nospam.nospamwrote in message
news:uR**************@TK2MSFTNGP02.phx.gbl...
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in
message news:BD264A84-
>You can use System::Runtime::InteropServices::Marshal::Copy(), which does
the "loop through it copying " part for you :)

I've tried several variations on the following code, but it complains that
it can't find an overload for Marshal::Copy that it likes. I suspect that
it doesn't like the boxed handle to an IntPtr that I think I'm trying to
feed it (by way of the gcnew IntPtr() code). How do I cast a char* to an
IntPtr?

const char* value = <some array of bytes>
int size = 15;
array<unsigned char>^ myArray = gcnew array<unsigned char>(size);
Marshal::Copy(gcnew IntPtr((void*)value), myArray, 0, size);

Feb 27 '08 #6

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

Similar topics

1
by: rir3760 | last post by:
Since a few days ago I have been working with the program I post below (a school assignment). The purpose of the program is to work with the va_ macros (stdarg.h) and arrays of arrays, hopefully...
6
by: jlea | last post by:
Does anyone know how to pass a managed array to a method and have that method actually create the array if necessary, ie., if the passed array is null? For example: void...
5
by: Chris | last post by:
Hi, 1) I find the notation for managed arrays in C++.NET very confusing : Sometimes is it not necessary to use the pointer notation ==> short pS2 __gc = new short __gc; Sometimes it is !!!...
2
by: The unProfessional | last post by:
Any know how to convert a managed array to an unmanaged array (ptr)? // Managed float f = new float ; // Unmanaged unsafe { float *pArray = f; // No good
8
by: quortex | last post by:
Hi all, I have a native class which has a single instance controlled via the singleton pattern. I need to call this from both native C++ and from mixed mode visual studio 2005 c++ CLI. At...
5
by: dovgani | last post by:
I have an unmanaged MFC project. The output is static lib. I would like to compile using /clr option. The native lib size is 64 megs and with /clr and /O1 options is 940 megs. Is it possibly...
2
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I'm working on a project where all the current code is written in standard compliant C++ with no dependencies on any external libraries except the standard library. The application is used to...
1
by: lallous | last post by:
Hello I am writing managed c++/c++ native code. We have soem buffer passed as char* from native, and we want to pass to a managed MemoryStream object. Is it possible to have a Byte ...
0
by: jensro | last post by:
Hello, 1) I use a structure in a native DLL like the following: typedef struct { int nof_elems; elem_s* elems; } container_s;
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: 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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.