首页 > 解决方案 > 为什么 C++/WinRT IDL 文件不允许我使用指针作为参数定义方法?

问题描述

我正在使用 C# UWP 项目中的代码在一个短数组中生成数据,将此数据传递给 C++/WinRT 组件项目中的代码(使用指针和数组的长度),修改数据,然后传回一个新数组(使用指针和数组的长度)到 C# 代码。使用下面的代码,我不会立即得到错误。但是,构建 C++/WinRT 项目失败,因为我的 idl 文件收到错误:MIDL2025 [msg]syntax error [context]: Expecting an identifier near "*"。

是否可以将指针从 C# 传递到 C++/WinRT?(如果您发现我的代码或其效率有任何其他问题,请随时告诉我。)

我在 Internet 上搜索了有关 C++/WinRT(不是 C++/CX)和 .idl 文件的所有信息。我认为对于 C++/WinRT 来说,几年前的任何东西都不会是准确的。C++/WinRT 于 2015 年 6 月 23 日发布,在此之前有关 WinRT 的任何内容实际上都只是 C++/CX。我按照此处的示例编写了代码:https ://github.com/microsoft/Windows-appsample-photo-editor/blob/master/PhotoEditor/Photo.idl 。另外,我认为 C++/WinRT 中的 idl 文件实际上是 MIDL 3.0。查看此链接https://docs.microsoft.com/en-us/uwp/midl-3/intro#types,我将 idl 文件更改为接受Int32而不是intInt16*不是short*(如果没有必要,请告诉我) . 我研究了一些关于out关键字,但它似乎不适用于 MIDL 3.0。需要明确的是,我想返回short*指针,而不是short值。

这是我的idl文件:

//.idl file
namespace MyNamespace
{
    [default_interface]
    runtimeclass MyClass
    {
        MyClass();

        void DoStuff(Int32 length, Int16* inbuf, Int16* outbuf);
    }
}

这是我的头文件:

//.h file
#pragma once
#include "MyClass.g.h"

namespace winrt::MyProject::implementation
{
    struct MyClass: MyClassT<MyClass>
    {
        MyClass() = default;

        void DoStuff(int length, short* inbuf, short* outbuf);
    };
}

namespace winrt::MyProject::factory_implementation
{
    struct MyClass: MyClassT<MyClass, implementation::MyClass>
    {
    };
}

这是我的 C++ 文件:

//.cpp file
#include "pch.h"
#include "MyClass.h"
#include "MyClass.g.cpp"

namespace winrt::MyProject::implementation
{
    void MyClass::DoStuff(int length, short* inbuf, short* outbuf)
    {
        //Process data here by editing outbuf
    }
}

先感谢您!

标签: pointersidlc++-winrtmidl

解决方案


推荐阅读