PkmnLibSharp/PkmnLibSharp/Utilities/MarshalHelper.cs

44 lines
1.2 KiB
C#
Raw Normal View History

2020-05-02 20:58:08 +00:00
using System;
using System.Linq;
2020-05-02 20:58:08 +00:00
using System.Runtime.InteropServices;
namespace PkmnLibSharp.Utilities
{
internal static class MarshalHelper
{
internal static IntPtr ToPtr(this string? s)
2020-05-02 20:58:08 +00:00
{
2020-07-19 10:33:22 +00:00
if (s == null) return IntPtr.Zero;
return Marshal.StringToHGlobalAnsi(s);
2020-05-02 20:58:08 +00:00
}
internal static string? PtrString(this IntPtr i)
2020-05-02 20:58:08 +00:00
{
2020-08-14 14:15:19 +00:00
return i == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(i);
2020-05-02 20:58:08 +00:00
}
internal static IntPtr ArrayPtr(this IntPtr[] a)
{
return Marshal.UnsafeAddrOfPinnedArrayElement(a, 0);
}
internal static IntPtr ArrayPtr<T>(this T[] a) where T : struct, IConvertible
2020-05-02 20:58:08 +00:00
{
return Marshal.UnsafeAddrOfPinnedArrayElement(a, 0);
}
internal static IntPtr ArrayPtr(this PointerWrapper?[] a)
{
return Marshal.UnsafeAddrOfPinnedArrayElement(a.Select(x => x?.Ptr ?? IntPtr.Zero).ToArray(), 0);
}
2020-05-03 09:38:49 +00:00
internal const byte True = 1;
internal const byte False = 0;
internal static byte ToNative(this bool b)
{
return b ? True : False;
}
2020-05-02 20:58:08 +00:00
}
}