【问题标题】:How to get the array of struct with "byte array" from WinRT C++ to C# in Windows Store app?如何在 Windows Store 应用程序中从 WinRT C++ 到 C# 获取带有“字节数组”的结构数组?
【发布时间】:2013-01-13 03:27:43
【问题描述】:

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

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

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 函数单击如下。

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

函数运行正常,项目数组可以通过调试窗口看到值是正确的。

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

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

这将导致以下编译器错误:

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

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

我在 MSDN 中查看了 value struct,它说 value struct 不能有 ref 类或结构作为成员,所以我想我不能像上面那样编写代码。

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

有谁知道如何使用另一种方式来替换值结构?我需要数组里面有“字节数组”。

【问题讨论】:

    标签: c# windows-runtime c++-cx winprt


    【解决方案1】:

    以下array types 可以通过ABI 传递:

    1. const Platform::Array^,
    2. 平台::数组^*,
    3. 平台::WriteOnlyArray,
    4. Platform::Array^ 的返回值。

    value struct 或值类只能包含基本数字类型、枚举类或 Platform::String^ 作为字段。

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

    您应该单独或使用 ref 类传递数组和结构。

    【讨论】:

      猜你喜欢
      • 2013-06-11
      • 2010-09-05
      • 1970-01-01
      • 2016-08-20
      • 2021-12-08
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多