c++ - handle single-dimensional array in Microsoft Interface Definition Language

wang zheng 21 Reputation points
2020-09-05T14:32:08.677+00:00

I followed the instruction at

https://learn.microsoft.com/en-us/uwp/midl-3/intro

try to use single-dimensional array in c++ winRT component

Class.idl

namespace MyClass  
{  
    [default_interface]  
    runtimeclass Class  
    {  
        Class();  
        String[] aaa(String[] v);  
      
    }  
}  

Class.h

#pragma once  
  
#include "Class.g.h"  
  
namespace winrt::MyClass::implementation  
{  
    struct Class : ClassT<Class>  
    {  
        Class() = default;  
  
        hstring[] aaa(hstring[]  s);  
   
    };  
}  
namespace winrt::MyClass::factory_implementation  
{  
    struct Class : ClassT<Class, implementation::Class>  
    {  
    };  
}  

Class.cpp

#include "pch.h"  
#include "Class.h"  
#include "Class.g.cpp"  
  
namespace winrt::MyClass::implementation  
{  
  
  hstring[] Class::aaa(hstring[] s)  
    {  
        return null;  
    }  
   
  
}  

but it reports the error as below

22758-error1.png

22759-error2.png

it looks like 'hstring[]' is not correct

Is there any sample code for usage of a single-dimensional array?

thanks

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Fay Wang - MSFT 5,221 Reputation points
    2020-09-07T03:17:49.33+00:00

    Hello,

    Welcome to Microsoft Q&A!

    We usually use com_array<hstring> to pass parameters to and from Windows Runtime APIs instead of hstring[] when a String[] declared in an idl file. In addition, you do not need to manually write the prototype of the aaa method. The midl.exe > cppwinrt.exe tool chain will implement templates including the definition and declaration of aaa method in Generated Files(\Project-Name\RuntimeComponent\Generated Files\sources). For more information, you can refer to this document. You could also check the following steps:

    First: Declare the String[] aaa(String[] v); in Class.idl of the component.

    namespace RuntimeComponent  
    {  
        [default_interface]  
        runtimeclass Class  
        {  
            Class();  
            Int32 MyProperty;  
      
            String[] aaa(String[] v);  
        }  
    }  
    

    Second: Save and build the project.

    Third: Open the folder \Project-Name\RuntimeComponent\Generated Files\sources in your project folder, open the Class.h and Class.cpp files and copy the definition and declaration code of aaa into the component.

    Class.h

    namespace winrt::RuntimeComponent::implementation  
    {  
        struct Class : ClassT<Class>  
        {  
            ……  
      
            com_array<hstring> aaa(array_view<hstring const> v);  
        };  
    }  
    

    Class.cpp

    namespace winrt::RuntimeComponent::implementation  
    {  
        ……  
        com_array<hstring> Class::aaa(array_view<hstring const> v)  
        {  
            throw hresult_not_implemented();  
        }  
    }  
    

    For more information about winrt::com_array struct template, you can refer to the document.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.