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

六月ゝ 毕业季﹏ 提交于 2019-12-04 01:24:19

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14584952/how-to-get-the-array-of-struct-with-byte-array-from-winrt-c-to-c-sharp-in-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!