Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the array of struct with "byte array" from WinRT C++ to C# in Windows Store app?

Here I have a C# metro app with a C++ WinRT component. I need to do something in WinRT, like assign a photo's name/path, and retrieve photo's thumbnail.

First, I write a value struct and retrieve struct array function in WinRT C++ as below.

public value struct Item
{
    String^ strName;
    String^ strPath;
};
public ref class CTestWinRT sealed
{
public:
    CTestWinRT();
    void TestOutStructArray(Platform::WriteOnlyArray<Item>^ intOutArray)
    {
        intOutArray->Data[0].strName = ref new String(L"test1.jpg");
        intOutArray->Data[0].strPath = ref new String(L"c:\\temp");
        intOutArray->Data[1].strName = ref new String(L"test2.jpg");
        intOutArray->Data[1].strPath = ref new String(L"c:\\temp");
    }
};

Then I use TestOutStructArray function in C# button click as below.

    CTestWinRT myNative = new CTestWinRT();
    private void btnTestClick(object sender, RoutedEventArgs e)
    {
        Item[] items = new Item[2];
        myNative.TestOutStructArray(items);
    }

The function is working ok and the items array can see the values are correct by debug window.

Now, I want to add a byte array in value struct as below.

public value struct Item
{
    String^ strName;
    String^ strPath;
    uint8 byteThumbnail[8096];
};

This will cause compiler error below:

error C3987: 'byteThumbnail': signature of public member contains native type 'unsigned char [8096]'

error C3992: 'byteThumbnail': signature of public member contains invalid type 'unsigned char [8096]'

I look into MSDN about value struct, it said value struct cannot have a ref class or struct as a member, so I think I cannot write the code like above.

http://msdn.microsoft.com/en-us/library/windows/apps/hh699861.aspx

Does anyone know how to use another way to replace value struct? I need the array to have "byte array" inside.

like image 948
ppcrong Avatar asked Jan 29 '13 14:01

ppcrong


1 Answers

The following array types can be passed across the ABI:

  1. const Platform::Array^,
  2. Platform::Array^*,
  3. Platform::WriteOnlyArray,
  4. return value of Platform::Array^.

A value struct or value class can contain as fields only fundamental numeric types, enum classes, or Platform::String^.

So you cannot use a value struct with arrays. And you cannot use arrays of uint8[] type.

You should pass arrays and structs separately or by using a ref class.

like image 149
maxim pg Avatar answered Sep 21 '22 14:09

maxim pg