Moving a byte array between C# and C++

Hi Guys, hope you can help.

I am currently writing a managed C++ wrapper to interface with some unmanaged C++ code so I can utilise the code in C#. Everything is working absolutely fine except I need to be able to pass in a managed buffer from C# in a C++ function, modify the buffer in the function and pass the results back to C#. I currently have the following:

C++ code:

public : void Play(System::Byte buffer[])
{
char buffer2[44100];

// Fill the unmanaged buffer
pSidplay2->play(buffer2, 44100);

//buffer = buffer2*;

// Copy the unmanaged data into the managed buffer
for (int i = 0; i < 44100; i++)
{
bufferIdea = buffer2Idea;
}
}

C# code:

private void button1_Click(object sender, EventArgs e)
{
sc = new SidClass();
byte[] ba = new Byte[44100];

// Initialise the sound object
sc.Initialise();

// Create binary file for the raw wave data
bi = new BinaryWriter(File.Open(@"C:\seesharp3", FileMode.Create));

// Play ten seconds of sound
for (int a = 0; a < 10; a++)
{
sc.Play(ba);
int b = 1;
bi.Write(ba, 0, 44100);
}
bi.Close();
}

If I step into the C++ code, the information is correctly written into the buffer, the problem I am having is getting that data back into C#. When I compile the following I receive the following errors:

Error 1 The best overloaded method match for 'SidManagedWrapper.SidClass.Play(byte*)' has some invalid arguments C:\Documents and Settings\Damien\My Documents\Visual Studio 2005\SidPlayTester2\Form1.cs 39 17 SidPlayTester2

Error 2 Argument '1': cannot convert from 'byte[]' to 'byte*' C:\Documents and Settings\Damien\My Documents\Visual Studio 2005\SidPlayTester2\Form1.cs 39 25 SidPlayTester2

I know these are because I am not using the correct data types. Can someone show me what changes I need to make

Many thanks,

Damien.




Answer this question

Moving a byte array between C# and C++

  • C&amp;#35;noob

    Your declaration takes a native array argument, not a managed array. Make it look like this:

    void Play(array<Byte>^ buffer);

    For example:

    #include "stdafx.h"

    using namespace System;

    public ref class Test {
    public:
    void Play(array<Byte>^ buffer) {
    unsigned char buffer2[100];
    for (int i = 0; i < 100; ++i) {
    buffer2[ i ] = buffer[ i ];
    }
    }
    };

    int main(array<System::String ^> ^args)
    {
    Test^ t = gcnew Test;
    array<Byte>^ buf = gcnew array<Byte>(100);
    t->Play(buf);
    return 0;
    }




  • AlbinCN

    Please tell me if your C++ application is COM If so I can give you a very good idea which is to use "SAFEARRAY unsigned char valueToPass" And that is the best ever interface method to pass a BYTE array from Unmanaged to Managed code.

    // Code for cpp file of Interface

    STDMETHODIMP CHaberles::PassByteData(SAFEARRAY *palmData)
    {
    BYTE *arrayOfPalmData;

    long lLength; // number of bytes

    // lock access to array data
    SafeArrayAccessData( palmData, (void*)&arrayOfPalmData );

    // get number of elements in array. This is the number of bytes
    lLength = palmData->rgsabound->cElements;
    arrayOfPalmData =new BYTE[lLength];

    memcpy(arrayOfPalmData,palmData->pvData,lLength);
    // release the safearray buffer
    SafeArrayUnaccessData( palmData );

    delete [] arrayOfPalmData; // Pay attention that I have used BYTE Array a local variable here you can do it also by creating the

    // BYTE array outside this functions scope and also delete it there also

    return S_OK;
    }

    // Code for header file of Interface

    STDMETHOD(PassByteData)(/*[in]*/ SAFEARRAY * palmData);

    // Code for idl file of Interface

    [id(1), helpstring("method PassByteData")] HRESULT PassByteData([in] SAFEARRAY (unsigned char) palmData);




  • Moving a byte array between C# and C++