C++ optional parameters in WinRT

Anand Kumar 41 Reputation points
2022-03-02T12:41:07.033+00:00

Hello,
We have WinRT component referring C++ code.
C++ code has optional parameters.
WinRT code is projected for C#.

Code details:
Current code without proper handling of optional parameters.

example.idl

namespace Test
{
[default_interface]
runtimeclass example
{
void login(String authCode, String emailId);
}
}

example.cpp

namespace winrt::Test::implementation
{
void example::login(hstring const& authCode, hstring const& emailId)
{
//I need to call below C++ API here
//DLLExport void login(std::optional<std::string> authCode, std::string emailId);
}
}

example.h

namespace winrt::Test::implementation
{
struct example: exampleT<example>
{
void login(hstring const& authCode, hstring const& emailId)
}
}

C# projection(cswinrt.exe) is done for this IDL file(winmd)
How can I know "authCode" is optional in C#? so that accordingly I can pass.
How to handle optional parameters across ABI.

Developer technologies Universal Windows Platform (UWP)
Developer technologies C++
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2022-03-09T01:20:19.39+00:00

    Hello,
    Welcome to Microsoft Q&A!

    C++ string optional parameters in WinRT

    I have to say In the Windows Runtime, strings are value types. “null” is treated as an empty string. If an empty string (“”) is not a valid authCode, then you can use an empty string to represent “no authCode”. Otherwise, you will have to express the optionality in some other way, such as a separate Boolean parameter or an overload.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.