From 7e372c55db3130b6e7ecc815a0ecd3c64592ffec Mon Sep 17 00:00:00 2001 From: David Hall Date: Sat, 6 Jan 2018 16:34:28 -0700 Subject: [PATCH] Added LinkedListToIEnum method for pointer interop --- Core/Extensions/InteropExtensions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Core/Extensions/InteropExtensions.cs b/Core/Extensions/InteropExtensions.cs index 675f83f2..67e6b416 100644 --- a/Core/Extensions/InteropExtensions.cs +++ b/Core/Extensions/InteropExtensions.cs @@ -40,6 +40,22 @@ namespace Vanara.Extensions /// true if the specified type is nullable; otherwise, false. public static bool IsNullable(this Type type) => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); + /// Marshals an unmanaged linked list of structures to an of that structure. + /// Type of native structure used by the unmanaged linked list. + /// The pointing to the native array. + /// The expression to be used to fetch the pointer to the next item in the list. + /// An exposing the elements of the linked list. + public static IEnumerable LinkedListToIEnum(this IntPtr ptr, Func next) + { + for (var pCurrent = ptr; pCurrent != IntPtr.Zero; ) + { + var ret = pCurrent.ToStructure(); + yield return ret; + pCurrent = next(ret); + } + yield break; + } + /// Marshals data from a managed list of specified type to a pre-allocated unmanaged block of memory. /// /// A type of the enumerated managed object that holds the data to be marshaled. The object must be a structure or an instance of a formatted class.