| | 1 | | using System; |
| | 2 | | using System.Runtime.InteropServices; |
| | 3 | |
|
| | 4 | | namespace SharpHoundRPC.Shared |
| | 5 | | { |
| | 6 | | public class SharedStructs |
| | 7 | | { |
| | 8 | | [StructLayout(LayoutKind.Sequential)] |
| | 9 | | public struct UnicodeString : IDisposable |
| | 10 | | { |
| | 11 | | private readonly ushort Length; |
| | 12 | | private readonly ushort MaximumLength; |
| | 13 | | private IntPtr Buffer; |
| | 14 | |
|
| | 15 | | public UnicodeString(string s) |
| 0 | 16 | | : this() |
| 0 | 17 | | { |
| 0 | 18 | | if (s == null) return; |
| 0 | 19 | | Length = (ushort) (s.Length * 2); |
| 0 | 20 | | MaximumLength = (ushort) (Length + 2); |
| 0 | 21 | | Buffer = Marshal.StringToHGlobalUni(s); |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Dispose() |
| 0 | 25 | | { |
| 0 | 26 | | if (Buffer == IntPtr.Zero) return; |
| 0 | 27 | | Marshal.FreeHGlobal(Buffer); |
| 0 | 28 | | Buffer = IntPtr.Zero; |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | public override string ToString() |
| 0 | 32 | | { |
| 0 | 33 | | return (Buffer != IntPtr.Zero ? Marshal.PtrToStringUni(Buffer, Length / 2) : null) ?? |
| 0 | 34 | | throw new InvalidOperationException(); |
| 0 | 35 | | } |
| | 36 | | } |
| | 37 | | } |
| | 38 | | } |