Skip to content

Commit 6d66daa

Browse files
authored
Added additional EvtVariantTypes
1 parent 816d72f commit 6d66daa

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/EventLogExpert.Eventing.Tests/Helpers/EventMethodsTests.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,48 @@ public void ConvertVariant_WhenByte_ShouldReturnByte()
108108
Assert.Equal(expectedValue, result);
109109
}
110110

111+
[Fact]
112+
public void ConvertVariant_WhenByteArray_ShouldReturnByteArray()
113+
{
114+
// Arrange
115+
byte[] expectedBytes = [10, 20, 30, 40, 50];
116+
IntPtr arrayPtr = Marshal.AllocHGlobal(expectedBytes.Length);
117+
118+
try
119+
{
120+
Marshal.Copy(expectedBytes, 0, arrayPtr, expectedBytes.Length);
121+
122+
var variant = CreateVariantWithCount(EvtVariantType.ByteArray, arrayPtr, (uint)expectedBytes.Length);
123+
124+
// Act
125+
var result = EventMethods.ConvertVariant(variant);
126+
127+
// Assert
128+
Assert.NotNull(result);
129+
Assert.IsType<byte[]>(result);
130+
Assert.Equal(expectedBytes, (byte[])result);
131+
}
132+
finally
133+
{
134+
Marshal.FreeHGlobal(arrayPtr);
135+
}
136+
}
137+
138+
[Fact]
139+
public void ConvertVariant_WhenByteArrayEmpty_ShouldReturnEmptyArray()
140+
{
141+
// Arrange
142+
var variant = CreateVariantWithCount(EvtVariantType.ByteArray, IntPtr.Zero, 0);
143+
144+
// Act
145+
var result = EventMethods.ConvertVariant(variant);
146+
147+
// Assert
148+
Assert.NotNull(result);
149+
Assert.IsType<byte[]>(result);
150+
Assert.Empty((byte[])result);
151+
}
152+
111153
[Fact]
112154
public void ConvertVariant_WhenDouble_ShouldReturnDouble()
113155
{
@@ -487,6 +529,52 @@ public void ConvertVariant_WhenUInt32_ShouldReturnUInt32()
487529
Assert.Equal(expectedValue, result);
488530
}
489531

532+
[Fact]
533+
public void ConvertVariant_WhenUInt32Array_ShouldReturnUInt32Array()
534+
{
535+
// Arrange
536+
uint[] expectedValues = [100, 200, 300, 400];
537+
IntPtr arrayPtr = Marshal.AllocHGlobal(sizeof(uint) * expectedValues.Length);
538+
539+
try
540+
{
541+
unsafe
542+
{
543+
var dest = new Span<uint>((void*)arrayPtr, expectedValues.Length);
544+
expectedValues.CopyTo(dest);
545+
}
546+
547+
var variant = CreateVariantWithCount(EvtVariantType.UInt32Array, arrayPtr, (uint)expectedValues.Length);
548+
549+
// Act
550+
var result = EventMethods.ConvertVariant(variant);
551+
552+
// Assert
553+
Assert.NotNull(result);
554+
Assert.IsType<uint[]>(result);
555+
Assert.Equal(expectedValues, (uint[])result);
556+
}
557+
finally
558+
{
559+
Marshal.FreeHGlobal(arrayPtr);
560+
}
561+
}
562+
563+
[Fact]
564+
public void ConvertVariant_WhenUInt32ArrayEmpty_ShouldReturnEmptyArray()
565+
{
566+
// Arrange
567+
var variant = CreateVariantWithCount(EvtVariantType.UInt32Array, IntPtr.Zero, 0);
568+
569+
// Act
570+
var result = EventMethods.ConvertVariant(variant);
571+
572+
// Assert
573+
Assert.NotNull(result);
574+
Assert.IsType<uint[]>(result);
575+
Assert.Empty((uint[])result);
576+
}
577+
490578
[Fact]
491579
public void ConvertVariant_WhenUInt64_ShouldReturnUInt64()
492580
{
@@ -697,6 +785,8 @@ private static EvtVariant CreateVariantWithCount(EvtVariantType type, object? va
697785
case EvtVariantType.SysTime:
698786
case EvtVariantType.Binary:
699787
case EvtVariantType.StringArray:
788+
case EvtVariantType.ByteArray:
789+
case EvtVariantType.UInt32Array:
700790
*(nint*)buffer = (IntPtr)value;
701791
break;
702792
case EvtVariantType.SByte:

src/EventLogExpert.Eventing/Helpers/EventMethods.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ internal enum EvtVariantType
166166
Handle,
167167
Xml,
168168

169-
// Custom types
170-
StringArray = 129 // String masked with EVT_VARIANT_TYPE_ARRAY
169+
// Array types (base type | EVT_VARIANT_TYPE_ARRAY)
170+
StringArray = 129,
171+
ByteArray = 132,
172+
UInt32Array = 136
171173
}
172174

173175
[Flags]
@@ -265,6 +267,31 @@ internal static partial class EventMethods
265267
}
266268

267269
return stringArray;
270+
case (int)EvtVariantType.ByteArray:
271+
if (variant.Count == 0)
272+
{
273+
return Array.Empty<byte>();
274+
}
275+
276+
var byteArray = new byte[variant.Count];
277+
Marshal.Copy(variant.Reference, byteArray, 0, byteArray.Length);
278+
279+
return byteArray;
280+
case (int)EvtVariantType.UInt32Array:
281+
if (variant.Count == 0)
282+
{
283+
return Array.Empty<uint>();
284+
}
285+
286+
var uintArray = new uint[variant.Count];
287+
288+
unsafe
289+
{
290+
var source = new ReadOnlySpan<uint>((void*)variant.Reference, (int)variant.Count);
291+
source.CopyTo(uintArray);
292+
}
293+
294+
return uintArray;
268295
default:
269296
throw new InvalidDataException($"Invalid {nameof(EvtVariantType)}: {variant.Type}");
270297
}

0 commit comments

Comments
 (0)