mirror of
https://github.com/soukoku/ntwain.git
synced 2025-11-07 18:14:46 +08:00
Refactoring xfer logic from twainsession out.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using NTwain.Properties;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace NTwain.Data
|
||||
@@ -55,7 +56,7 @@ namespace NTwain.Data
|
||||
ContainerType = capability.ContainerType,
|
||||
}.ReadRangeValue(baseAddr);
|
||||
default:
|
||||
throw new ArgumentException(string.Format(Resources.CapHasBadContainer, capability.ContainerType), "capability");
|
||||
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.CapHasBadContainer, capability.ContainerType), "capability");
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -234,7 +234,7 @@ namespace NTwain.Data
|
||||
|
||||
object IConvertible.ToType(Type conversionType, IFormatProvider provider)
|
||||
{
|
||||
return Convert.ChangeType((float)this, conversionType);
|
||||
return Convert.ChangeType((float)this, conversionType, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
ushort IConvertible.ToUInt16(IFormatProvider provider)
|
||||
@@ -632,7 +632,6 @@ namespace NTwain.Data
|
||||
/// </summary>
|
||||
/// <param name="capability">The capability.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
public TWCapability(CapabilityId capability, TWOneValue value)
|
||||
{
|
||||
Capability = capability;
|
||||
@@ -644,7 +643,6 @@ namespace NTwain.Data
|
||||
/// </summary>
|
||||
/// <param name="capability">The capability.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
public TWCapability(CapabilityId capability, TWEnumeration value)
|
||||
{
|
||||
Capability = capability;
|
||||
@@ -656,7 +654,6 @@ namespace NTwain.Data
|
||||
/// </summary>
|
||||
/// <param name="capability">The capability.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
public TWCapability(CapabilityId capability, TWRange value)
|
||||
{
|
||||
Capability = capability;
|
||||
@@ -684,14 +681,13 @@ namespace NTwain.Data
|
||||
|
||||
#region value functions
|
||||
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
void SetOneValue(TWOneValue value)
|
||||
{
|
||||
if (value == null) { throw new ArgumentNullException("value"); }
|
||||
ContainerType = ContainerType.OneValue;
|
||||
|
||||
// since one value can only house UInt32 we will not allow type size > 4
|
||||
if (TypeReader.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(Resources.BadValueType, "TWOneValue")); }
|
||||
if (TypeReader.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.BadValueType, "TWOneValue")); }
|
||||
|
||||
_hContainer = Platform.MemoryManager.Allocate((uint)Marshal.SizeOf(value));
|
||||
if (_hContainer != IntPtr.Zero)
|
||||
@@ -700,7 +696,6 @@ namespace NTwain.Data
|
||||
}
|
||||
}
|
||||
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
void SetEnumValue(TWEnumeration value)
|
||||
{
|
||||
if (value == null) { throw new ArgumentNullException("value"); }
|
||||
@@ -726,14 +721,13 @@ namespace NTwain.Data
|
||||
}
|
||||
|
||||
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
void SetRangeValue(TWRange value)
|
||||
{
|
||||
if (value == null) { throw new ArgumentNullException("value"); }
|
||||
ContainerType = ContainerType.Range;
|
||||
|
||||
// since range value can only house UInt32 we will not allow type size > 4
|
||||
if (TypeReader.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(Resources.BadValueType, "TWRange")); }
|
||||
if (TypeReader.GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.BadValueType, "TWRange")); }
|
||||
|
||||
_hContainer = Platform.MemoryManager.Allocate((uint)Marshal.SizeOf(value));
|
||||
if (_hContainer != IntPtr.Zero)
|
||||
@@ -752,23 +746,22 @@ namespace NTwain.Data
|
||||
/// <param name="offset"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="value"></param>
|
||||
[EnvironmentPermissionAttribute(SecurityAction.LinkDemand)]
|
||||
void WriteValue(IntPtr baseAddr, ref int offset, ItemType type, object value)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case ItemType.Int8:
|
||||
case ItemType.UInt8:
|
||||
Marshal.WriteByte(baseAddr, offset, Convert.ToByte(value));// (byte)value);
|
||||
Marshal.WriteByte(baseAddr, offset, Convert.ToByte(value, CultureInfo.InvariantCulture));// (byte)value);
|
||||
break;
|
||||
case ItemType.Bool:
|
||||
case ItemType.Int16:
|
||||
case ItemType.UInt16:
|
||||
Marshal.WriteInt16(baseAddr, offset, Convert.ToInt16(value));//(short)value);
|
||||
Marshal.WriteInt16(baseAddr, offset, Convert.ToInt16(value, CultureInfo.InvariantCulture));//(short)value);
|
||||
break;
|
||||
case ItemType.UInt32:
|
||||
case ItemType.Int32:
|
||||
Marshal.WriteInt32(baseAddr, offset, Convert.ToInt32(value));//(int)value);
|
||||
Marshal.WriteInt32(baseAddr, offset, Convert.ToInt32(value, CultureInfo.InvariantCulture));//(int)value);
|
||||
break;
|
||||
case ItemType.Fix32:
|
||||
TWFix32 f32 = (TWFix32)value;
|
||||
@@ -2188,15 +2181,24 @@ namespace NTwain.Data
|
||||
/// </summary>
|
||||
public partial class TWStatus
|
||||
{
|
||||
public TWStatus()
|
||||
{
|
||||
|
||||
}
|
||||
internal TWStatus(ushort code, ushort data)
|
||||
{
|
||||
_conditionCode = code;
|
||||
_data = data;
|
||||
}
|
||||
/// <summary>
|
||||
/// Condition Code describing the status.
|
||||
/// </summary>
|
||||
public ConditionCode ConditionCode { get { return (ConditionCode)_conditionCode; } internal set { _conditionCode = (ushort)value; } }
|
||||
public ConditionCode ConditionCode { get { return (ConditionCode)_conditionCode; } }
|
||||
/// <summary>
|
||||
/// Valid for TWAIN 2.1 and later. This field contains additional
|
||||
/// scanner-specific data. If there is no data, then this value must be zero.
|
||||
/// </summary>
|
||||
public ushort Data { get { return _data; } internal set { _data = Data; } }
|
||||
public ushort Data { get { return _data; } }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -2210,7 +2212,7 @@ namespace NTwain.Data
|
||||
/// </summary>
|
||||
public TWStatus Status
|
||||
{
|
||||
get { return new TWStatus { ConditionCode = (ConditionCode)_conditionCode, Data = _data }; }
|
||||
get { return new TWStatus(_conditionCode, _data); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1467,7 +1467,7 @@ namespace NTwain.Data
|
||||
/// <summary>
|
||||
/// Corresponds to DG_*.
|
||||
/// </summary>
|
||||
[Flags]
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2217:DoNotMarkEnumsWithFlags"), Flags]
|
||||
public enum DataGroups : uint
|
||||
{
|
||||
None = 0,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace NTwain.Data
|
||||
@@ -36,7 +37,7 @@ namespace NTwain.Data
|
||||
var rawType = Enum.GetUnderlyingType(returnType);
|
||||
if (typeof(ushort).IsAssignableFrom(rawType))
|
||||
{
|
||||
var intVal = Convert.ToUInt32(value);
|
||||
var intVal = Convert.ToUInt32(value, CultureInfo.InvariantCulture);
|
||||
var enumVal = GetLowerWord(intVal);
|
||||
if (!Enum.IsDefined(returnType, enumVal))
|
||||
{
|
||||
@@ -52,7 +53,7 @@ namespace NTwain.Data
|
||||
else if (typeof(IConvertible).IsAssignableFrom(returnType))
|
||||
{
|
||||
// for regular integers and whatnot
|
||||
return (T)Convert.ChangeType(value, returnType);
|
||||
return (T)Convert.ChangeType(value, returnType, CultureInfo.InvariantCulture);
|
||||
}
|
||||
// return as-is from cap. if caller made a mistake then there should be exceptions
|
||||
return (T)value;
|
||||
@@ -78,7 +79,7 @@ namespace NTwain.Data
|
||||
{
|
||||
return (TWFix32)value;
|
||||
}
|
||||
return (TWFix32)Convert.ToSingle(value);
|
||||
return (TWFix32)Convert.ToSingle(value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user