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

Marshal.PtrToStructure fails to split strings in .NET Core 5 #54662

Closed
jlchavez opened this issue Jun 24, 2021 · 3 comments · Fixed by #54695
Closed

Marshal.PtrToStructure fails to split strings in .NET Core 5 #54662

jlchavez opened this issue Jun 24, 2021 · 3 comments · Fixed by #54695

Comments

@jlchavez
Copy link

jlchavez commented Jun 24, 2021

When marshaling a BSTR to a Struct it fails to split the strings as it did in .NET Framework 4.7.2

Edit
Added a test on .NET Fiddle:
Test Marshal.PtrToStructure NET 5

Description

On the following example a string of "012", should be split into "01" a "2" but the strings point to the correct position but doesn't contain the correct ending mark for the first one, so the result would be "012" and "2". Yes it's obvious that a masrsahlling of the bytes directly to the struct would produce this behavior, but I would file it as a bug, not a feature. My string obviously is a 64 character long with 13 fields, this is just a small example of the problem.

using System;
using System.Runtime.InteropServices;
using System.Text;

public class Program
{
    public static void Main()
    {
       string str = "012";
       IntPtr pBuf = Marshal.StringToBSTR(str);
       CEStruct ces = (CEStruct)Marshal.PtrToStructure(pBuf, typeof(CEStruct));
       Console.WriteLine(ces.tipoDocumento);
       Console.WriteLine(ces.tipoContribuyente);
    }
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct CEStruct
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
    public string tipoDocumento;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
    public string tipoContribuyente;
}

Configuration

Visual Studio 2019 16.10.1
.NET Core 5
Windows 10.0.19042.1052
64bit
Not specific to configuration

Regression?

It works fine in .net 4.7.2

@dotnet-issue-labeler dotnet-issue-labeler bot added area-Interop-coreclr untriaged New issue has not been triaged by the area owner labels Jun 24, 2021
@huoyaoyuan
Copy link
Member

On .NET Core 3.1 it outputs "01" and "2" too.

@jlchavez
Copy link
Author

Thanks @huoyaoyuan

I Added a .NET Fiddle to the issue, it runs in .net 5 with all the code that a friend reported to me that failed, built another solution for him, but there it fails.

@jkotas
Copy link
Member

jkotas commented Jun 24, 2021

cc @jkoritzinsky

jkoritzinsky added a commit to jkoritzinsky/runtime that referenced this issue Jun 24, 2021
@AaronRobinsonMSFT AaronRobinsonMSFT removed the untriaged New issue has not been triaged by the area owner label Jun 24, 2021
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 6.0.0 milestone Jun 24, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 24, 2021
jkoritzinsky added a commit that referenced this issue Jun 28, 2021
…alTStr fields (#54695)

* Use string constructor that takes length instead of the one that searches for a null terminator.

Fixes #54662

* Marshal back buffer size or string to first null terminator, whichever is shorter

* Add tests.

* Add unicode test.

* Use the same implementation style for the wstr case case as the cstr case

* Fix accidental deletion from test.
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 28, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jul 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants