Conversion of managed and unmanaged arrays 
Author Message
 Conversion of managed and unmanaged arrays

I have an unmanaged array of bytes and I would
like to pass it to a managed method (which expects
managed array of bytes).

How can this be achieved?



Mon, 03 Oct 2005 20:29:00 GMT  
 Conversion of managed and unmanaged arrays
Since unmanaged array is allocated on the stack or unmanaged C++ heap
(unless it's a member of a managed class), there is no other way than to
copy it to the managed array, something like this:

        int arr[] = {1,2,3,4,5};

        int size = sizeof(arr)/sizeof(arr[0]);
        Int32 arr2 [] = new Int32[size];

        for( int i=0; i<size; ++i ) {
                arr2[i] = arr[i];
        }

(arr is an unmanaged array, arr2 is a managed array)

--
Artur Laksberg, Visual C++ Team
This posting is provided AS IS with no warranties, and confers no rights.

Quote:
> I have an unmanaged array of bytes and I would
> like to pass it to a managed method (which expects
> managed array of bytes).

> How can this be achieved?



Sun, 09 Oct 2005 03:00:21 GMT  
 Conversion of managed and unmanaged arrays

Quote:
> Since unmanaged array is allocated on the stack or unmanaged C++ heap
> (unless it's a member of a managed class), there is no other way than to
> copy it to the managed array, something like this:

> int arr[] = {1,2,3,4,5};

> int size = sizeof(arr)/sizeof(arr[0]);
> Int32 arr2 [] = new Int32[size];

> for( int i=0; i<size; ++i ) {
> arr2[i] = arr[i];
> }

> (arr is an unmanaged array, arr2 is a managed array)

Artur is correct here. Notice that one option to doing it this way might be
just to use the Copy() method of the
System::Runtime::InteropServices::Marshal class, instead.

--
Tomas Restrepo



Sun, 09 Oct 2005 07:43:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. conversion from managed System::String to unmanaged UUID?

2. Passing arrays from managed to unmanaged code

3. marshal an array from managed C++ to unmanaged C++

4. return an array from unmanaged C++ to managed C++

5. passing array from managed to unmanaged ????

6. How to Pass Array from managed to unmanaged !!??!!

7. Managed array of managed arrays of char

8. Conversion of native char* to managed array

9. moving arrays from un-managed to managed code.

10. Exception across managed/unmanaged

11. managed C++ wrapper around unmanaged C++ classes: causing StackOverflow exception

12. Managed vs. Unmanaged

 

 
Powered by phpBB® Forum Software