Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marshaling wchar_t on Linux uses wrong size #30906

Closed
lainy opened this issue Sep 17, 2019 · 3 comments
Closed

Marshaling wchar_t on Linux uses wrong size #30906

lainy opened this issue Sep 17, 2019 · 3 comments

Comments

@lainy
Copy link

lainy commented Sep 17, 2019

General

Tested on .NET Core 3.0.100-preview9-014004 on Arch Linux.

The following code should print out 4 on this system, but instead outputs 2.
Printing sizeof(wchar_t) from a C program gives 4.

Discovered when trying to interop with a C library that has a struct with wchar_t[5] inside.

using System;
using System.Runtime.InteropServices;

namespace dotnet_core_wchar_t_size
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        internal struct test
        {
            public char c;
        }

        static void Main(string[] args)
        {
            Console.WriteLine(Marshal.SizeOf(typeof(test)));
        }
    }
}
@scalablecory scalablecory transferred this issue from dotnet/core Sep 19, 2019
@tannergooding
Copy link
Member

CharSet.Unicode is documented to be

Name Value Description
Unicode 3 Marshal strings as Unicode 2-byte character strings.

@jkoritzinsky
Copy link
Member

jkoritzinsky commented Sep 19, 2019

More specifically, CharSet.Unicode is documented to map to a native char16_t on Unix-like platforms: https://docs.microsoft.com/dotnet/standard/native-interop/charset

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@filmor
Copy link

filmor commented Jun 14, 2020

@jkoritzinsky Is there a reasonable alternative, though? To have platform-independent bindings to an API using wchar_t, it would be usefull to have a CharSet.WCharT that is UTF16 on Windows and UTF32 essentially everywhere else. Apart from .NET tooling I have never seen an API that switches between UTF8 and UTF16 like CharSet.Auto does.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants