question

wangzheng-7333 avatar image
wangzheng-7333 asked ·

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

I followed the instruction at

https://docs.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


windows-uwpwindows-uwp-runtime
error1.png (43.6 KiB)
error2.png (73.6 KiB)
3 comments
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Do you have "using namespace winrt;" in there somewhere?

0 Votes 0 · ·

Hi, does the following method help? Have you solved your issue? @wangzheng-7333

0 Votes 0 · ·

@wangzheng-7333 Have you checked Fay's answer? Does it help?

0 Votes 0 · ·

1 Answer

FayWang-MSFT avatar image
FayWang-MSFT answered ·

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.

Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.