Problem moving byte array from C# to Managed C++ and back  
Author Message
DamienRoot6





PostPosted: Visual C++ Language, Problem moving byte array from C# to Managed C++ and back Top

Hi,

 

I am currently trying to use Managed C++ to create a ,.Net wrapper for some unmanaged C++ code. I'm nearly there, but I have a really annoying issue. My code is as follows:

public : void Play(System::Byte buffer[])
{
            char buffer2[88200];
   
            // Fill the unmanaged buffer
            pSidplay2->play(buffer2, 88200);
          
            //buffer = buffer2*;

            // Copy the unmanaged data into the managed buffer
            for (int a = 0; a < 88200; a++)
            {
                buffer[ a ] = buffer2[ a ];
    
            }
 }

The code simply accepts a managed Byte array from C# and fills it using my unmanaged routine. The code as it stands is fine. However, I would like to limit the amount of data to the size of the buffer received from c#. As you can see I have currently fixed it at 88200 bytes. The problem I am having is I do not know how to access the length property of the input bute array from my C++ function. There is no length property so I have no idea how I can obtain the length of the array. Furthermore, I am unable to initialise the char array with a non fixed value, the compiler complains that a constant must be used to initialise an array.

I really want something like the following:

public : void Play(System::Byte buffer[])
{
            char buffer2[buffer->Length];
   
            // Fill the unmanaged buffer
            pSidplay2->play(buffer2, buffer->Length);
          
            //buffer = buffer2*;

            // Copy the unmanaged data into the managed buffer
            for (int a = 0; a < buffer->Length; a++)
            {
                buffer[ a ] = buffer2[ a ];
    
            }
 }

How do I achieve this Its been driving me nuts as there seems to be no way of doing what I need to do.

Any ideas anyone

Thanks,


Damien,

 



Visual C++14  
 
 
Mattias Sjogren





PostPosted: Visual C++ Language, Problem moving byte array from C# to Managed C++ and back Top

Wouldn't it be easier to just pin the managed array and pass a pointer to it directly to the native function That would remove the need to allocate twice the amount of memory.



 
 
DamienRoot6





PostPosted: Visual C++ Language, Problem moving byte array from C# to Managed C++ and back Top

Could you show me how I would do this, perhaps with an example I tried something along those lines before, but could never get past the problem of being unable to assign a pointer to a managed array.

I am really a C# programmer, C++ is something I've had to deal with recently, and haven't done much with it since the old Dos days.

Many thanks for your help,

Damien.


 
 
Mattias Sjogren





PostPosted: Visual C++ Language, Problem moving byte array from C# to Managed C++ and back Top

See the example with a byte array at http://msdn2.microsoft.com/en-us/library/aa712862(VS.71).aspx



 
 
DamienRoot6





PostPosted: Visual C++ Language, Problem moving byte array from C# to Managed C++ and back Top

Thanks, I will take a look.
 
 
Chuck the Code Monkey





PostPosted: Visual C++ Language, Problem moving byte array from C# to Managed C++ and back Top

If you are using .NET 2.0, then the following should work:

public : void Play(array<System::Byte> ^buffer)
{
            pin_ptr<unsigned char> bufPin = &buffer[0];
            unsigned char *bufPtr = bufPin;
   
            // Fill the unmanaged buffer
            pSidplay2->play(bufPtr, buffer->Length);
 }

 

using pin_ptr tells the GC to leave the array alone for a while and it gives you the unmanaged pointer to the actual memory in the managed array

Take a look at: http://www.codeproject.com/managedcpp/cppcliarrays.asp

There is a section entitled 'Direct manipulation of CLI arrays using native pointers'