-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
CustomMarshaler.cs
53 lines (43 loc) · 1.83 KB
/
CustomMarshaler.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
class WrappedString
{
public WrappedString(string str)
{
_str = str;
}
internal string _str;
}
class WrappedStringCustomMarshaler : ICustomMarshaler
{
public void CleanUpManagedData(object ManagedObj) { }
public void CleanUpNativeData(IntPtr pNativeData) { Marshal.ZeroFreeCoTaskMemAnsi(pNativeData); }
public int GetNativeDataSize() => IntPtr.Size;
public IntPtr MarshalManagedToNative(object ManagedObj) => Marshal.StringToCoTaskMemAnsi(((WrappedString)ManagedObj)._str);
public object MarshalNativeToManaged(IntPtr pNativeData) => new WrappedString(Marshal.PtrToStringAnsi(pNativeData));
public static ICustomMarshaler GetInstance(string cookie) => new WrappedStringCustomMarshaler();
}
// Use an ifdef here to give us two separate public API surfaces to call while allowing us to have the same implementation code
// as well as allowing us to share the custom marshaler implementations above.
// If we wanted to add more tests here, we would want to put the public API surface in the namespace and the private
// details and marshalers in the global scope as done above.
#if CUSTOMMARSHALERS2
namespace CustomMarshalers2
#else
namespace CustomMarshalers
#endif
{
public class CustomMarshalerTest
{
[DllImport("CustomMarshalerNative", CharSet = CharSet.Ansi)]
private static extern int NativeParseInt([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(WrappedStringCustomMarshaler))] WrappedString str);
public int ParseInt(string str)
{
return NativeParseInt(new WrappedString(str));
}
}
}