如何获得带有“字节数组"的结构体数组?从WinRT C ++到Windows Store应用中的C#? [英] How to get the array of struct with "byte array" from WinRT C++ to C# in Windows Store app?

查看:190
本文介绍了如何获得带有“字节数组"的结构体数组?从WinRT C ++到Windows Store应用中的C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我有一个带有C ++ WinRT组件的C#Metro应用程序. 我需要在WinRT中做一些事情,例如分配照片的名称/路径,并检索照片的缩略图.

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.

首先,我在WinRT C ++中编写一个值结构并检索结构数组函数,如下所示.

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");
    }
};

然后我在C#按钮中使用TestOutStructArray函数,如下所示.

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);
    }

该功能正常运行,并且在调试窗口中items数组可以看到正确的值.

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

现在,我想在值struct中添加一个字节数组,如下所示.

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:

错误C3987:"byteThumbnail":包含公共成员的签名 原生类型'unsigned char [8096]'

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

错误C3992:"byteThumbnail":包含公共成员的签名 无效类型'unsigned char [8096]'

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

我研究了有关MSDN的值结构,它说值结构不能具有ref类或结构作为成员,所以我想我不能编写上面的代码.

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.

推荐答案

以下数组类型可以跨ABI传递:

The following array types can be passed across the ABI:

  1. const Platform :: Array ^,
  2. Platform :: Array ^ *,
  3. Platform :: WriteOnlyArray
  4. Platform :: Array ^的返回值.

一个值结构或值类可以包含作为字段,仅基本数字类型,枚举类或Platform :: String ^.

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

因此,不能将值结构与数组一起使用.而且您不能使用uint8 []类型的数组.

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

您应该分别或通过使用ref类来传递数组和结构.

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

这篇关于如何获得带有“字节数组"的结构体数组?从WinRT C ++到Windows Store应用中的C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆