C++WinRT does not correctly handles Chinese characters using Windows.Foundation.Uri

peter li 6 Reputation points
2024-07-08T17:57:07.7366667+00:00

I have a uri with Chinese character and query string that needs to be parsed with Windows.Foundation.Uri. Using C++WinRT, it always returns me an empty result. But equivalent C# code does returns me expected result.

I have tried changing the file encoding to either UTF-8 and UTF-8 BOM, nothing worked.

With C++WinRT, create a C++WinRT console application:

    winrt::Windows::Foundation::Uri uri{ LR"(myapp://open?file="C:/我.txt")" };
    winrt::Windows::Foundation::WwwFormUrlDecoder parsed{ uri.Query() };
    for (auto entry : parsed)
    {
        std::wcout << entry.Name().data() << L'\t' << entry.Value().data() << L'\n';
    }

With C#, create a UWP project:

            var uri = new Uri("myapp://open?file=\"C:/我.txt\"");
            var parsed = new WwwFormUrlDecoder(uri.Query);
            foreach (var entry in parsed)
            {
                Debug.WriteLine(entry);
            }

C# gives me expected result:

enter image description here

C++ gives me empty result:

enter image description here

Repro code here

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,890 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,711 questions
{count} votes

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.