From db7884b6f59cf30e1b70d2cd262ef1b70952d686 Mon Sep 17 00:00:00 2001 From: dahall Date: Wed, 4 Nov 2020 13:04:42 -0700 Subject: [PATCH] Overloaded IntPtr.LinkedListToEnum extension method to allow for offset func. --- Core/Extensions/InteropExtensions.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Core/Extensions/InteropExtensions.cs b/Core/Extensions/InteropExtensions.cs index d6698c5c..c9f85516 100644 --- a/Core/Extensions/InteropExtensions.cs +++ b/Core/Extensions/InteropExtensions.cs @@ -214,6 +214,25 @@ namespace Vanara.Extensions } } + /// 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 offset from the current pointer to the next item in the list. + /// + /// The number of allocated bytes behind . This value is used to determine when to stop enumerating. + /// + /// An exposing the elements of the linked list. + public static IEnumerable LinkedListToIEnum(this IntPtr ptr, Func nextOffset, SizeT allocatedBytes) + { + var pEnd = ptr.Offset(allocatedBytes); + for (var pCurrent = ptr; pCurrent.ToInt64() < pEnd.ToInt64();) + { + var ret = pCurrent.ToStructure(); + yield return ret; + pCurrent = pCurrent.Offset(nextOffset(ret)); + } + } + /// /// Marshals data from a managed list of objects to an unmanaged block of memory allocated by the method. ///