blob: 60a4ae2b5078996103636264a5acf87aa2302dc7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// Copyright (c) 2017 Eric Mellino
// Copyright (c) 2019 Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace Vulkan {
public static class BindingsHelpers
{
public static unsafe StringHandle StringToHGlobalUtf8(string s)
{
Debug.Assert(s != null);
int byteCount = Encoding.UTF8.GetByteCount(s);
IntPtr retPtr = Marshal.AllocHGlobal(byteCount);
fixed (char* stringPtr = s)
{
Encoding.UTF8.GetBytes(stringPtr, s.Length, (byte*)retPtr.ToPointer(), byteCount);
}
return new StringHandle() { Handle = retPtr };
}
public static void FreeHGlobal(StringHandle ptr)
{
Marshal.FreeHGlobal(ptr.Handle);
}
}
public struct StringHandle
{
public IntPtr Handle;
}
}
|