mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
1 changed files with 784842 additions and 0 deletions
30
Brutal.Interop/InteropNullableStringMarshaller.cs
Normal file
30
Brutal.Interop/InteropNullableStringMarshaller.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.InteropServices.Marshalling;
|
||||
|
||||
namespace Brutal.Interop;
|
||||
|
||||
/// <summary>
|
||||
/// Marshaller for UTF-8 strings whose out direction preserves a NULL native
|
||||
/// pointer as <see langword="null"/>. The null-preserving sibling of
|
||||
/// <see cref="InteropStringMarshaller"/>, for native APIs that use a NULL
|
||||
/// return as a failure or absence sentinel.
|
||||
/// </summary>
|
||||
[CustomMarshaller(
|
||||
typeof(string),
|
||||
MarshalMode.ManagedToUnmanagedIn,
|
||||
typeof(InteropStringMarshaller.ManagedToUnmanagedIn)
|
||||
)] // string parameter, shared with the non-nullable sibling
|
||||
[CustomMarshaller(typeof(string), MarshalMode.ManagedToUnmanagedOut, typeof(ManagedToUnmanagedOut))] // return string?
|
||||
public static unsafe class InteropNullableStringMarshaller
|
||||
{
|
||||
public static class ManagedToUnmanagedOut
|
||||
{
|
||||
public static string? ConvertToManaged(byte* unmanaged)
|
||||
{
|
||||
if (unmanaged == null)
|
||||
return null;
|
||||
|
||||
return Marshal.PtrToStringUTF8((IntPtr)unmanaged);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue