From de788ce91d2af09ace958dccc74e670eedb1afe2 Mon Sep 17 00:00:00 2001
From: Eugene Wang <8755753+soukoku@users.noreply.github.com>
Date: Thu, 20 Nov 2025 22:36:36 -0500
Subject: [PATCH] Test reading some values boxed.
---
src/NTwain/Data/TWAINH_EXTRAS.cs | 2 +-
src/NTwain/Data/ValueReader.cs | 223 +++++++++++++++++++++++++++++++
2 files changed, 224 insertions(+), 1 deletion(-)
diff --git a/src/NTwain/Data/TWAINH_EXTRAS.cs b/src/NTwain/Data/TWAINH_EXTRAS.cs
index aadcbc4..af61a6f 100644
--- a/src/NTwain/Data/TWAINH_EXTRAS.cs
+++ b/src/NTwain/Data/TWAINH_EXTRAS.cs
@@ -258,7 +258,7 @@ namespace NTwain.Data
/// A more dotnet-friendly representation of .
///
///
- public class Enumeration where TValue : struct
+ public class Enumeration
{
public int CurrentIndex;
diff --git a/src/NTwain/Data/ValueReader.cs b/src/NTwain/Data/ValueReader.cs
index 759d910..0b5ab63 100644
--- a/src/NTwain/Data/ValueReader.cs
+++ b/src/NTwain/Data/ValueReader.cs
@@ -84,6 +84,52 @@ namespace NTwain.Data
}
+ ///
+ /// Reads a boxed one value out of a cap. This can only be done once if memory is freed.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static object? ReadOneValueBoxed(this ref TW_CAPABILITY cap, IMemoryManager memMgr, bool freeMemory = true)
+ {
+ if (cap.ConType != TWON.ONEVALUE || cap.hContainer == IntPtr.Zero) return default;
+
+ var lockedPtr = memMgr.Lock(cap.hContainer);
+
+ try
+ {
+ TWTY itemType;
+ // Mac has a level of indirection and a different structure (ick)...
+ if (TWPlatform.IsMacOSX)
+ {
+ // Crack the container...
+ var onevalue = MarshalTo(lockedPtr);
+ itemType = (TWTY)onevalue.ItemType;
+ lockedPtr += Marshal.SizeOf(onevalue);
+ }
+ else
+ {
+ // Crack the container...
+ var onevalue = MarshalTo(lockedPtr);
+ itemType = onevalue.ItemType;
+ lockedPtr += Marshal.SizeOf(onevalue);
+ }
+
+ return ReadTWTYDataBoxed(lockedPtr, itemType, 0);
+ }
+ finally
+ {
+ if (lockedPtr != IntPtr.Zero) memMgr.Unlock(cap.hContainer);
+ if (freeMemory)
+ {
+ memMgr.Free(cap.hContainer);
+ cap.hContainer = IntPtr.Zero;
+ }
+ }
+ }
+
///
/// Reads a one value out of a cap. This can only be done once if memory is freed.
///
@@ -130,6 +176,78 @@ namespace NTwain.Data
}
}
+ public static Enumeration