Update string marshallers to V2 shape#71849
Merged
jkotas merged 8 commits intodotnet:mainfrom Jul 10, 2022
Merged
Conversation
|
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
|
Tagging subscribers to this area: @dotnet/interop-contrib Issue DetailsThis updates the marshallers and the built-in system and NativeAOT's use of them. Generated code examplesUTF-16[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")]
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
public static partial string Strings(string a1, in string a2, ref string a3, out string a4)
{
System.Runtime.CompilerServices.Unsafe.SkipInit(out a4);
ushort* __a2_native = default;
ushort* __a3_native = default;
ushort* __a4_native = default;
string __retVal;
ushort* __retVal_native = default;
try
{
// Marshal - Convert managed data to native data.
__a2_native = global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(a2);
__a3_native = global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToUnmanaged(a3);
// Pin - Pin data in preparation for calling the P/Invoke.
fixed (void* __a1_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(a1))
{
__retVal_native = __PInvoke((ushort*)__a1_native, &__a2_native, &__a3_native, &__a4_native);
}
// Unmarshal - Convert native data to managed data.
__retVal = global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToManaged(__retVal_native);
a4 = global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToManaged(__a4_native);
a3 = global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.ConvertToManaged(__a3_native);
}
finally
{
// Cleanup - Perform required cleanup.
global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__retVal_native);
global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__a2_native);
global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__a3_native);
global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.Free(__a4_native);
}
return __retVal;
// Local P/Invoke
[System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "Strings", ExactSpelling = true)]
static extern unsafe ushort* __PInvoke(ushort* a1, ushort** a2, ushort** a3, ushort** a4);
}UTF-8[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")]
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
public static partial string Strings(string a1, in string a2, ref string a3, out string a4)
{
System.Runtime.CompilerServices.Unsafe.SkipInit(out a4);
byte* __a1_native = default;
byte* __a2_native = default;
byte* __a3_native = default;
byte* __a4_native = default;
string __retVal;
byte* __retVal_native = default;
// Setup - Perform required setup.
global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __a1_native__marshaller = new();
global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn __a2_native__marshaller = new();
try
{
// Marshal - Convert managed data to native data.
byte* __a1_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize];
__a1_native__marshaller.FromManaged(a1, new System.Span<byte>(__a1_native__stackptr, global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize));
byte* __a2_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize];
__a2_native__marshaller.FromManaged(a2, new System.Span<byte>(__a2_native__stackptr, global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ManagedToUnmanagedIn.BufferSize));
__a3_native = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToUnmanaged(a3);
{
// PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned.
__a1_native = __a1_native__marshaller.ToUnmanaged();
__a2_native = __a2_native__marshaller.ToUnmanaged();
__retVal_native = __PInvoke(__a1_native, &__a2_native, &__a3_native, &__a4_native);
}
// Unmarshal - Convert native data to managed data.
__retVal = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToManaged(__retVal_native);
a4 = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToManaged(__a4_native);
a3 = global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.ConvertToManaged(__a3_native);
}
finally
{
// Cleanup - Perform required cleanup.
global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__retVal_native);
__a1_native__marshaller.Free();
__a2_native__marshaller.Free();
global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__a3_native);
global::System.Runtime.InteropServices.Marshalling.Utf8StringMarshaller.Free(__a4_native);
}
return __retVal;
// Local P/Invoke
[System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "Strings", ExactSpelling = true)]
static extern unsafe byte* __PInvoke(byte* a1, byte** a2, byte** a3, byte** a4);
}ANSI[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")]
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
public static partial string Strings(string a1, in string a2, ref string a3, out string a4)
{
System.Runtime.CompilerServices.Unsafe.SkipInit(out a4);
byte* __a1_native = default;
byte* __a2_native = default;
byte* __a3_native = default;
byte* __a4_native = default;
string __retVal;
byte* __retVal_native = default;
// Setup - Perform required setup.
global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn __a1_native__marshaller = new();
global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn __a2_native__marshaller = new();
try
{
// Marshal - Convert managed data to native data.
byte* __a1_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn.BufferSize];
__a1_native__marshaller.FromManaged(a1, new System.Span<byte>(__a1_native__stackptr, global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn.BufferSize));
byte* __a2_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn.BufferSize];
__a2_native__marshaller.FromManaged(a2, new System.Span<byte>(__a2_native__stackptr, global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ManagedToUnmanagedIn.BufferSize));
__a3_native = global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ConvertToUnmanaged(a3);
{
// PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned.
__a1_native = __a1_native__marshaller.ToUnmanaged();
__a2_native = __a2_native__marshaller.ToUnmanaged();
__retVal_native = __PInvoke(__a1_native, &__a2_native, &__a3_native, &__a4_native);
}
// Unmarshal - Convert native data to managed data.
__retVal = global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ConvertToManaged(__retVal_native);
a4 = global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ConvertToManaged(__a4_native);
a3 = global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.ConvertToManaged(__a3_native);
}
finally
{
// Cleanup - Perform required cleanup.
global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.Free(__retVal_native);
__a1_native__marshaller.Free();
__a2_native__marshaller.Free();
global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.Free(__a3_native);
global::System.Runtime.InteropServices.Marshalling.AnsiStringMarshaller.Free(__a4_native);
}
return __retVal;
// Local P/Invoke
[System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "Strings", ExactSpelling = true)]
static extern unsafe byte* __PInvoke(byte* a1, byte** a2, byte** a3, byte** a4);
}BSTR[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "42.42.42.42")]
[System.Runtime.CompilerServices.SkipLocalsInitAttribute]
public static partial string Strings(string a1, in string a2, ref string a3, out string a4)
{
System.Runtime.CompilerServices.Unsafe.SkipInit(out a4);
ushort* __a1_native = default;
ushort* __a2_native = default;
ushort* __a3_native = default;
ushort* __a4_native = default;
string __retVal;
ushort* __retVal_native = default;
// Setup - Perform required setup.
global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ManagedToUnmanagedIn __a1_native__marshaller = new();
global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ManagedToUnmanagedIn __a2_native__marshaller = new();
try
{
// Marshal - Convert managed data to native data.
byte* __a1_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ManagedToUnmanagedIn.BufferSize];
__a1_native__marshaller.FromManaged(a1, new System.Span<byte>(__a1_native__stackptr, global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ManagedToUnmanagedIn.BufferSize));
byte* __a2_native__stackptr = stackalloc byte[global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ManagedToUnmanagedIn.BufferSize];
__a2_native__marshaller.FromManaged(a2, new System.Span<byte>(__a2_native__stackptr, global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ManagedToUnmanagedIn.BufferSize));
__a3_native = global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ConvertToUnmanaged(a3);
{
// PinnedMarshal - Convert managed data to native data that requires the managed data to be pinned.
__a1_native = __a1_native__marshaller.ToUnmanaged();
__a2_native = __a2_native__marshaller.ToUnmanaged();
__retVal_native = __PInvoke(__a1_native, &__a2_native, &__a3_native, &__a4_native);
}
// Unmarshal - Convert native data to managed data.
__retVal = global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ConvertToManaged(__retVal_native);
a4 = global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ConvertToManaged(__a4_native);
a3 = global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.ConvertToManaged(__a3_native);
}
finally
{
// Cleanup - Perform required cleanup.
global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.Free(__retVal_native);
__a1_native__marshaller.Free();
__a2_native__marshaller.Free();
global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.Free(__a3_native);
global::System.Runtime.InteropServices.Marshalling.BstrStringMarshaller.Free(__a4_native);
}
return __retVal;
// Local P/Invoke
[System.Runtime.InteropServices.DllImportAttribute("Microsoft.Interop.Tests.NativeExportsNE", EntryPoint = "Strings", ExactSpelling = true)]
static extern unsafe ushort* __PInvoke(ushort* a1, ushort** a2, ushort** a3, ushort** a4);
}
|
jkotas
reviewed
Jul 8, 2022
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/Utf8StringMarshaller.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Jul 8, 2022
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Jul 8, 2022
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Jul 8, 2022
jkotas
reviewed
Jul 8, 2022
jkotas
reviewed
Jul 8, 2022
Avoid initialization of marshaller when not used. Remove unneccessary unmanaged->managed APIs for In marshallers.
that can be used to load nested classes.
jkotas
reviewed
Jul 9, 2022
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Jul 9, 2022
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Jul 9, 2022
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs
Outdated
Show resolved
Hide resolved
jkotas
reviewed
Jul 9, 2022
...ystem.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/AnsiStringMarshaller.cs
Outdated
Show resolved
Hide resolved
elinor-fung
approved these changes
Jul 10, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This updates the marshallers and the built-in system and NativeAOT's use of them.
/cc @elinor-fung @jkoritzinsky @jkotas
Generated code examples
UTF-16
UTF-8
ANSI
BSTR