Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best get a byte array from C# to a C++ WinRT component

We have a WinRT component with business logic that internally massages a C++ unsigned char buffer. We now want to feed that buffer from a C# byte[]. What would the perfect boundary look like, i.e., what would the signature of the SomeWinRTFunction function below be?

void SomeWinRTFunction(something containing bytes from managed land)
{
    IVector<unsigned char> something using the bytes given from managed land;
}

This kind of issue seems too new still for search engines to find it...

like image 878
Johann Gerell Avatar asked Jun 08 '12 12:06

Johann Gerell


2 Answers

In the C++ part, the method should accept a platform array of uint8 (the equivalent of C# byte).

public ref class Class1 sealed
{
public:
    Class1();
    //readonly array
    void TestArray(const Platform::Array<uint8>^ intArray)
    {

    }
    //writeonly array
    void TestOutArray(Platform::WriteOnlyArray<uint8>^ intOutArray)
    {

    }

};

In the C# part pass the byte array:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Byte[] b = new Byte[2];
        b[0] = 1;
        var c = new Class1();
        c.TestArray(b);
        c.TestOutArray(b); 

    }
like image 186
Sofian Hnaide Avatar answered Sep 30 '22 08:09

Sofian Hnaide


In WinRT IVector is projected as IList, i'm not sure about byte -> unsigned char but i suspect is too.

C#

byte[] array;
SomeWinRTFunction(array);

C++

void SomeWinRTFunction(IVector<unsigned char> bytes) 
{ 
    IVector<unsigned char> something using the bytes given from managed land; 
} 

This whitepaper might shed some more light.

like image 25
Slugart Avatar answered Sep 30 '22 09:09

Slugart