What .NET UnmanagedType is Unicode (UTF-16)?

Posted by Pat on Stack Overflow See other posts from Stack Overflow or by Pat
Published on 2010-06-01T17:00:40Z Indexed on 2010/06/01 17:03 UTC
Read the original article Hit count: 159

Filed under:
|
|
|

I am packing bytes into a struct, and some of them correspond to a Unicode string. The following works fine for an ASCII string:

[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
    public string MyString;
}

I assumed that I could do

[StructLayout(LayoutKind.Sequential)]
private struct PacketBytes
{
    [MarshalAs(UnmanagedType.LPWStr, SizeConst = 32)]
    public string MyString;
}

to make it Unicode, but that didn't work. (Since this field is part of a struct with other fields, which I've omitted for clarity, I can't simply change the CharSet of the containing struct.)

Any idea what I'm doing wrong?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about unicode