From 35b0550c3d22eefbd329f10374b8a99a27443017 Mon Sep 17 00:00:00 2001 From: soukoku Date: Thu, 19 Feb 2015 22:55:09 -0500 Subject: [PATCH] #31 Add support for setting non-int onevalue for capability --- NTwain/Data/TwainTypesExtended.cs | 140 ++++++++++++++++++------------ 1 file changed, 84 insertions(+), 56 deletions(-) diff --git a/NTwain/Data/TwainTypesExtended.cs b/NTwain/Data/TwainTypesExtended.cs index e68fb56..d1ad40e 100644 --- a/NTwain/Data/TwainTypesExtended.cs +++ b/NTwain/Data/TwainTypesExtended.cs @@ -643,18 +643,30 @@ namespace NTwain.Data /// The capability. /// The value. public TWCapability(CapabilityId capability, TWOneValue value) - : this(capability, value, PlatformInfo.Current.MemoryManager) { } - + { + Capability = capability; + SetOneValue(value, PlatformInfo.Current.MemoryManager); + } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The capability. /// The value. - /// The memory manager. - public TWCapability(CapabilityId capability, TWOneValue value, IMemoryManager memoryManager) + /// The type. + public TWCapability(CapabilityId capability, string value, ItemType type) { Capability = capability; - SetOneValue(value, memoryManager); + SetOneValue(value, type, PlatformInfo.Current.MemoryManager); + } + /// + /// Initializes a new instance of the class. + /// + /// The capability. + /// The value. + public TWCapability(CapabilityId capability, TWFrame value) + { + Capability = capability; + SetOneValue(value, PlatformInfo.Current.MemoryManager); } /// @@ -663,18 +675,9 @@ namespace NTwain.Data /// The capability. /// The value. public TWCapability(CapabilityId capability, TWEnumeration value) - : this(capability, value, PlatformInfo.Current.MemoryManager) { } - - /// - /// Initializes a new instance of the class. - /// - /// The capability. - /// The value. - /// The memory manager. - public TWCapability(CapabilityId capability, TWEnumeration value, IMemoryManager memoryManager) { Capability = capability; - SetEnumValue(value, memoryManager); + SetEnumValue(value, PlatformInfo.Current.MemoryManager); } /// @@ -683,18 +686,9 @@ namespace NTwain.Data /// The capability. /// The value. public TWCapability(CapabilityId capability, TWRange value) - : this(capability, value, PlatformInfo.Current.MemoryManager) { } - - /// - /// Initializes a new instance of the class. - /// - /// The capability. - /// The value. - /// The memory manager. - public TWCapability(CapabilityId capability, TWRange value, IMemoryManager memoryManager) { Capability = capability; - SetRangeValue(value, memoryManager); + SetRangeValue(value, PlatformInfo.Current.MemoryManager); } /// @@ -703,18 +697,9 @@ namespace NTwain.Data /// The capability. /// The value. public TWCapability(CapabilityId capability, TWArray value) - : this(capability, value, PlatformInfo.Current.MemoryManager) { } - - /// - /// Initializes a new instance of the class. - /// - /// The capability. - /// The value. - /// The memory manager. - public TWCapability(CapabilityId capability, TWArray value, IMemoryManager memoryManager) { Capability = capability; - SetArrayValue(value, memoryManager); + SetArrayValue(value, PlatformInfo.Current.MemoryManager); } #endregion @@ -737,6 +722,45 @@ namespace NTwain.Data #region value functions + void SetOneValue(string value, ItemType type, IMemoryManager memoryManager) + { + ContainerType = ContainerType.OneValue; + switch (type) + { + case ItemType.String128: + case ItemType.String255: + case ItemType.String32: + case ItemType.String64: + + _hContainer = memoryManager.Allocate((uint)(Marshal.SizeOf(typeof(TWFrame)) + 2)); + if (_hContainer != IntPtr.Zero) + { + IntPtr baseAddr = memoryManager.Lock(_hContainer); + int offset = 0; + baseAddr.WriteValue(ref offset, ItemType.UInt16, type); + baseAddr.WriteValue(ref offset, type, value); + memoryManager.Unlock(_hContainer); + } + break; + default: + throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Type {0} is not valid for string.", type)); + } + } + void SetOneValue(TWFrame value, IMemoryManager memoryManager) + { + ContainerType = ContainerType.OneValue; + + _hContainer = memoryManager.Allocate((uint)(Marshal.SizeOf(typeof(TWFrame)) + 2)); + if (_hContainer != IntPtr.Zero) + { + IntPtr baseAddr = memoryManager.Lock(_hContainer); + int offset = 0; + baseAddr.WriteValue(ref offset, ItemType.UInt16, ItemType.Frame); + baseAddr.WriteValue(ref offset, ItemType.Frame, value); + memoryManager.Unlock(_hContainer); + } + } + void SetOneValue(TWOneValue value, IMemoryManager memoryManager) { if (value == null) { throw new ArgumentNullException("value"); } @@ -762,19 +786,21 @@ namespace NTwain.Data int offset = 0; _hContainer = memoryManager.Allocate((uint)valueSize); - IntPtr baseAddr = memoryManager.Lock(_hContainer); - - // can't safely use StructureToPtr here so write it our own - baseAddr.WriteValue(ref offset, ItemType.UInt16, value.ItemType); - baseAddr.WriteValue(ref offset, ItemType.UInt32, (uint)value.ItemList.Length); - baseAddr.WriteValue(ref offset, ItemType.UInt32, value.CurrentIndex); - baseAddr.WriteValue(ref offset, ItemType.UInt32, value.DefaultIndex); - foreach (var item in value.ItemList) + if (_hContainer != IntPtr.Zero) { - baseAddr.WriteValue(ref offset, value.ItemType, item); + IntPtr baseAddr = memoryManager.Lock(_hContainer); + + // can't safely use StructureToPtr here so write it our own + baseAddr.WriteValue(ref offset, ItemType.UInt16, value.ItemType); + baseAddr.WriteValue(ref offset, ItemType.UInt32, (uint)value.ItemList.Length); + baseAddr.WriteValue(ref offset, ItemType.UInt32, value.CurrentIndex); + baseAddr.WriteValue(ref offset, ItemType.UInt32, value.DefaultIndex); + foreach (var item in value.ItemList) + { + baseAddr.WriteValue(ref offset, value.ItemType, item); + } + memoryManager.Unlock(_hContainer); } - //memoryManager.Unlock(baseAddr); - memoryManager.Unlock(_hContainer); } void SetRangeValue(TWRange value, IMemoryManager memoryManager) @@ -801,17 +827,19 @@ namespace NTwain.Data int offset = 0; _hContainer = memoryManager.Allocate((uint)valueSize); - IntPtr baseAddr = memoryManager.Lock(_hContainer); - - // can't safely use StructureToPtr here so write it our own - baseAddr.WriteValue(ref offset, ItemType.UInt16, value.ItemType); - baseAddr.WriteValue(ref offset, ItemType.UInt32, (uint)value.ItemList.Length); - foreach (var item in value.ItemList) + if (_hContainer != IntPtr.Zero) { - baseAddr.WriteValue(ref offset, value.ItemType, item); + IntPtr baseAddr = memoryManager.Lock(_hContainer); + + // can't safely use StructureToPtr here so write it our own + baseAddr.WriteValue(ref offset, ItemType.UInt16, value.ItemType); + baseAddr.WriteValue(ref offset, ItemType.UInt32, (uint)value.ItemList.Length); + foreach (var item in value.ItemList) + { + baseAddr.WriteValue(ref offset, value.ItemType, item); + } + memoryManager.Unlock(_hContainer); } - memoryManager.Unlock(_hContainer); - //memoryManager.Unlock(baseAddr); } #endregion