Skip to content

Commit 098b5d1

Browse files
committed
Added new features to SafeArrayBuffer.
1 parent 4a35dd0 commit 098b5d1

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

NtApiDotNet/SafeBuffers.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,24 @@ public sealed class SafeIoStatusBuffer : SafeStructureInOutBuffer<IoStatus>
6060
/// </summary>
6161
public int Count { get; }
6262

63-
private static int _element_size = Marshal.SizeOf(typeof(T));
63+
private static readonly int _element_size = Marshal.SizeOf(typeof(T));
6464

6565
private static int GetArraySize(T[] array)
6666
{
6767
return _element_size * array.Length;
6868
}
6969

70+
private SafeArrayBuffer() : base(IntPtr.Zero, 0, false)
71+
{
72+
}
73+
7074
/// <summary>
7175
/// Constructor.
7276
/// </summary>
7377
/// <param name="array">Array of elements.</param>
74-
public SafeArrayBuffer(T[] array)
75-
: base(GetArraySize(array))
78+
/// <param name="additional_size">Additional data to place after the array.</param>
79+
public SafeArrayBuffer(T[] array, int additional_size)
80+
: base(GetArraySize(array) + additional_size)
7681
{
7782
Count = array.Length;
7883
IntPtr ptr = DangerousGetHandle();
@@ -82,6 +87,36 @@ public SafeArrayBuffer(T[] array)
8287
}
8388
}
8489

90+
/// <summary>
91+
/// Constructor.
92+
/// </summary>
93+
/// <param name="array">Array of elements.</param>
94+
public SafeArrayBuffer(T[] array)
95+
: this(array, 0)
96+
{
97+
}
98+
99+
/// <summary>
100+
/// Get a reference to the additional data.
101+
/// </summary>
102+
public SafeHGlobalBuffer Data
103+
{
104+
get
105+
{
106+
if (IsClosed || IsInvalid)
107+
throw new ObjectDisposedException("handle");
108+
109+
int size = Count * _element_size;
110+
int length = Length - size;
111+
return new SafeHGlobalBuffer(handle + size, length < 0 ? 0 : length, false);
112+
}
113+
}
114+
115+
/// <summary>
116+
/// Get a NULL safe array buffer.
117+
/// </summary>
118+
new static public SafeArrayBuffer<T> Null => new SafeArrayBuffer<T>();
119+
85120
/// <summary>
86121
/// Dispose buffer.
87122
/// </summary>
@@ -93,7 +128,6 @@ protected override void Dispose(bool disposing)
93128
{
94129
Marshal.DestroyStructure(ptr + (i * _element_size), typeof(T));
95130
}
96-
97131
base.Dispose(disposing);
98132
}
99133
}

0 commit comments

Comments
 (0)