Added back some more types and changed most to structs.

This commit is contained in:
Eugene Wang
2018-11-12 20:54:11 -05:00
parent 9e20510455
commit fa177c4193
11 changed files with 2972 additions and 184 deletions

View File

@@ -0,0 +1,58 @@
namespace NTwain.Data
{
/// <summary>
/// Provides identification information about a TWAIN entity. Used to maintain consistent
/// communication between entities.
/// </summary>
public interface ITW_IDENTITY
{
/// <summary>
/// Gets the data functionalities for TWAIN 2 detection.
/// </summary>
/// <value>The data functionalities.</value>
DataFunctionalities DataFunctionalities { get; }
/// <summary>
/// Gets the supported data group. The application will normally set this field to specify which Data
/// Group(s) it wants the Source Manager to sort Sources by when
/// presenting the Select Source dialog, or returning a list of available
/// Sources.
/// </summary>
/// <value>The data group.</value>
DataGroups DataGroup { get; }
/// <summary>
/// String identifying the manufacturer of the application or Source. e.g. "Aldus".
/// </summary>
string Manufacturer { get; }
/// <summary>
/// Tells an application that performs device-specific operations which
/// product family the Source supports. This is useful when a new Source
/// has been released and the application doesn't know about the
/// particular Source but still wants to perform Custom operations with it.
/// e.g. "ScanMan".
/// </summary>
string ProductFamily { get; }
/// <summary>
/// A string uniquely identifying the Source. This is the string that will be
/// displayed to the user at Source select-time. This string must uniquely
/// identify your Source for the user, and should identify the application
/// unambiguously for Sources that care. e.g. "ScanJet IIc".
/// </summary>
string ProductName { get; }
/// <summary>
/// Major number of latest TWAIN version that this element supports.
/// </summary>
short ProtocolMajor { get; }
/// <summary>
/// Minor number of latest TWAIN version that this element supports.
/// </summary>
short ProtocolMinor { get; }
/// <summary>
/// A <see cref="TW_VERSION"/> structure identifying the TWAIN entity.
/// </summary>
TW_VERSION Version { get; }
}
}

View File

@@ -1,67 +0,0 @@
namespace NTwain.Data
{
/// <summary>
/// Contains loose magic values for some TWAIN stuff.
/// </summary>
public static class TwainConst
{
// string lengths are specified here since the actual
// array length doesn't reflect the name :(
/// <summary>
/// Length of an array that holds TW_STR32.
/// </summary>
public const int String32 = 34;
/// <summary>
/// Length of an array that holds TW_STR64.
/// </summary>
public const int String64 = 66;
/// <summary>
/// Length of an array that holds TW_STR128.
/// </summary>
public const int String128 = 130;
/// <summary>
/// Length of an array that holds TW_STR255.
/// </summary>
public const int String255 = 256;
// deprecated
//public const int String1024 = 1026;
/// <summary>
/// Don't care value for 8 bit types.
/// </summary>
public const byte DontCare8 = 0xff;
/// <summary>
/// Don't care value for 16 bit types.
/// </summary>
public const ushort DontCare16 = 0xffff;
/// <summary>
/// Don't care value for 32 bit types.
/// </summary>
public const uint DontCare32 = 0xffffffff;
/// <summary>
/// The major version number of TWAIN supported by this library.
/// </summary>
public const short ProtocolMajor = 2;
/// <summary>
/// The minor version number of TWAIN supported by this library.
/// </summary>
public const short ProtocolMinor = 4;
/// <summary>
/// Value for true where applicable.
/// </summary>
public const ushort True = 1;
/// <summary>
/// Value for false where applicable.
/// </summary>
public const ushort False = 0;
public const ushort IconId = 962;
public const ushort DsmId = 461;
public const ushort DsmCodeId = 63;
}
}

View File

@@ -87,7 +87,7 @@ namespace NTwain.Data
[StructLayout(LayoutKind.Sequential, Pack = 2), [StructLayout(LayoutKind.Sequential, Pack = 2),
BestFitMapping(false, ThrowOnUnmappableChar = true)] BestFitMapping(false, ThrowOnUnmappableChar = true)]
partial class TW_AUDIOINFO partial struct TW_AUDIOINFO
{ {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = TwainConst.String255)] [MarshalAs(UnmanagedType.ByValTStr, SizeConst = TwainConst.String255)]
string _name; string _name;
@@ -150,7 +150,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_CUSTOMDSDATA partial struct TW_CUSTOMDSDATA
{ {
TW_UINT32 _infoLength; TW_UINT32 _infoLength;
TW_HANDLE _hData; TW_HANDLE _hData;
@@ -158,7 +158,7 @@ namespace NTwain.Data
[StructLayout(LayoutKind.Sequential, Pack = 2), [StructLayout(LayoutKind.Sequential, Pack = 2),
BestFitMapping(false, ThrowOnUnmappableChar = true)] BestFitMapping(false, ThrowOnUnmappableChar = true)]
partial class TW_DEVICEEVENT partial struct TW_DEVICEEVENT
{ {
TW_UINT32 _event; TW_UINT32 _event;
@@ -198,7 +198,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_EVENT partial struct TW_EVENT
{ {
TW_MEMREF _pEvent; TW_MEMREF _pEvent;
TW_UINT16 _tWMessage; TW_UINT16 _tWMessage;
@@ -217,7 +217,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_EXTIMAGEINFO partial struct TW_EXTIMAGEINFO
{ {
TW_UINT32 _numInfos; TW_UINT32 _numInfos;
@@ -334,8 +334,28 @@ namespace NTwain.Data
string _productName; string _productName;
} }
[StructLayout(LayoutKind.Sequential, Pack = 2),
BestFitMapping(false, ThrowOnUnmappableChar = true)]
partial class TW_IDENTITY_64
{
ulong _id;
TW_VERSION _version;
TW_UINT16 _protocolMajor;
TW_UINT16 _protocolMinor;
TW_UINT32 _supportedGroups;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = TwainConst.String32)]
string _manufacturer;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = TwainConst.String32)]
string _productFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = TwainConst.String32)]
string _productName;
}
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_IMAGEINFO partial struct TW_IMAGEINFO
{ {
TW_FIX32 _xResolution; TW_FIX32 _xResolution;
TW_FIX32 _yResolution; TW_FIX32 _yResolution;
@@ -353,7 +373,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_IMAGELAYOUT partial struct TW_IMAGELAYOUT
{ {
TW_FRAME _frame; TW_FRAME _frame;
TW_UINT32 _documentNumber; TW_UINT32 _documentNumber;
@@ -372,7 +392,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_IMAGEMEMXFER partial struct TW_IMAGEMEMXFER
{ {
// TODO: mac & linux are different? // TODO: mac & linux are different?
@@ -387,7 +407,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_JPEGCOMPRESSION partial struct TW_JPEGCOMPRESSION
{ {
TW_UINT16 _colorSpace; TW_UINT16 _colorSpace;
TW_UINT32 _subSampling; TW_UINT32 _subSampling;
@@ -411,7 +431,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_METRICS partial struct TW_METRICS
{ {
TW_UINT32 _sizeOf; TW_UINT32 _sizeOf;
TW_UINT32 _imageCount; TW_UINT32 _imageCount;
@@ -427,7 +447,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_PALETTE8 partial struct TW_PALETTE8
{ {
TW_UINT16 _numColors; TW_UINT16 _numColors;
TW_UINT16 _paletteType; TW_UINT16 _paletteType;
@@ -437,7 +457,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_PASSTHRU partial struct TW_PASSTHRU
{ {
TW_MEMREF _pCommand; TW_MEMREF _pCommand;
TW_UINT32 _commandBytes; TW_UINT32 _commandBytes;
@@ -448,7 +468,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_PENDINGXFERS partial struct TW_PENDINGXFERS
{ {
TW_UINT16 _count; TW_UINT16 _count;
TW_UINT32 _eOJ; TW_UINT32 _eOJ;
@@ -476,17 +496,17 @@ namespace NTwain.Data
[StructLayout(LayoutKind.Sequential, Pack = 2), [StructLayout(LayoutKind.Sequential, Pack = 2),
BestFitMapping(false, ThrowOnUnmappableChar = true)] BestFitMapping(false, ThrowOnUnmappableChar = true)]
partial class TW_SETUPFILEXFER partial struct TW_SETUPFILEXFER
{ {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = TwainConst.String255)] [MarshalAs(UnmanagedType.ByValTStr, SizeConst = TwainConst.String255)]
string _fileName; string _fileName;
TW_UINT16 _format; TW_UINT16 _format;
TW_INT16 _vRefNum = -1; TW_INT16 _vRefNum;
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_SETUPMEMXFER partial struct TW_SETUPMEMXFER
{ {
TW_UINT32 _minBufSize; TW_UINT32 _minBufSize;
TW_UINT32 _maxBufSize; TW_UINT32 _maxBufSize;
@@ -494,14 +514,14 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_STATUS partial struct TW_STATUS
{ {
TW_UINT16 _conditionCode; TW_UINT16 _conditionCode;
TW_UINT16 _data; TW_UINT16 _data;
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_STATUSUTF8 partial struct TW_STATUSUTF8
{ {
// NOTE: rather than embedding the TWStatus directly I'm using its fields instead // NOTE: rather than embedding the TWStatus directly I'm using its fields instead
// so the TWStatus could become a class object. // so the TWStatus could become a class object.
@@ -512,7 +532,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_TWAINDIRECT partial struct TW_TWAINDIRECT
{ {
TW_UINT32 _SizeOf; TW_UINT32 _SizeOf;
TW_UINT16 _CommunicationManager; TW_UINT16 _CommunicationManager;
@@ -523,7 +543,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_USERINTERFACE partial struct TW_USERINTERFACE
{ {
TW_BOOL _showUI; TW_BOOL _showUI;
TW_BOOL _modalUI; TW_BOOL _modalUI;
@@ -534,7 +554,7 @@ namespace NTwain.Data
DataGroups dg, DataArgumentType dat, Message msg, TW_MEMREF data); DataGroups dg, DataArgumentType dat, Message msg, TW_MEMREF data);
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_ENTRYPOINT partial struct TW_ENTRYPOINT
{ {
// TODO: linux 64 is different? // TODO: linux 64 is different?
TW_UINT32 _size; TW_UINT32 _size;
@@ -561,7 +581,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_FILTER_DESCRIPTOR partial struct TW_FILTER_DESCRIPTOR
{ {
TW_UINT32 _size; TW_UINT32 _size;
TW_UINT32 _hueStart; TW_UINT32 _hueStart;
@@ -574,7 +594,7 @@ namespace NTwain.Data
} }
[StructLayout(LayoutKind.Sequential, Pack = 2)] [StructLayout(LayoutKind.Sequential, Pack = 2)]
partial class TW_FILTER partial struct TW_FILTER
{ {
TW_UINT32 _size; TW_UINT32 _size;
TW_UINT32 _descriptorCount; TW_UINT32 _descriptorCount;

File diff suppressed because it is too large Load Diff

View File

@@ -1354,7 +1354,7 @@ namespace NTwain.Data
Ussr = 7, Ussr = 7,
UAEmirates = 971, UAEmirates = 971,
UnitedKingdom = 44, UnitedKingdom = 44,
Usa = 1, USA = 1,
Uruguay = 598, Uruguay = 598,
Vanuatu = 1041, Vanuatu = 1041,
VaticanCity = 39, VaticanCity = 39,
@@ -1395,7 +1395,7 @@ namespace NTwain.Data
Slovenia = 386, Slovenia = 386,
SouthKorea = 82, SouthKorea = 82,
Ukraine = 380, Ukraine = 380,
USVirginIs = 340, USVirginIslands = 340,
Vietnam = 84, Vietnam = 84,
} }

View File

@@ -0,0 +1,24 @@
using NTwain.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NTwain.Internals
{
static class ValidationExtensions
{
/// <summary>
/// Verifies the string length is under the maximum length
/// and throws <see cref="ArgumentException"/> if violated.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="maxLength">The maximum length.</param>
public static void VerifyLengthUnder(this string value, int maxLength)
{
if (value != null && value.Length > maxLength)
throw new ArgumentException(string.Format(MsgText.MaxStringLengthExceeded, maxLength));
}
}
}

View File

@@ -21,4 +21,23 @@
<Version>4.0.0</Version> <Version>4.0.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="Triplets\" />
</ItemGroup>
<ItemGroup>
<Compile Update="Resources\MsgText.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>MsgText.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Resources\MsgText.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>MsgText.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project> </Project>

72
src/NTwain/Resources/MsgText.Designer.cs generated Normal file
View File

@@ -0,0 +1,72 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace NTwain.Resources {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class MsgText {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal MsgText() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("NTwain.Resources.MsgText", typeof(MsgText).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
/// <summary>
/// Looks up a localized string similar to The string value has exceeded the maximum length of {0}..
/// </summary>
internal static string MaxStringLengthExceeded {
get {
return ResourceManager.GetString("MaxStringLengthExceeded", resourceCulture);
}
}
}
}

View File

@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="MaxStringLengthExceeded" xml:space="preserve">
<value>The string value has exceeded the maximum length of {0}.</value>
</data>
</root>

View File

@@ -15,99 +15,8 @@ namespace NTwain
public bool PreferLegacyDsm { get; internal set; } public bool PreferLegacyDsm { get; internal set; }
public string AppName { get; internal set; } public ITW_IDENTITY App { get; internal set; }
internal DataGroups DataGroup { get; set; }
internal IMemoryManager MemoryManager { get; set; } internal IMemoryManager MemoryManager { get; set; }
} }
public class TwainConfigurationBuilder
{
private bool _legacy;
private string _appName;
private string _companyName;
private DataGroups _dg = DataGroups.Image;
private bool _64bit;
private PlatformID _platform;
/// <summary>
/// Default ctor.
/// </summary>
public TwainConfigurationBuilder()
{
_64bit = IntPtr.Size == 8;
_platform = Environment.OSVersion.Platform;
}
/// <summary>
/// Specifies which DSM to use.
/// </summary>
/// <param name="legacyDsm"></param>
/// <returns></returns>
public TwainConfigurationBuilder UseDsm(bool legacyDsm = false)
{
if (legacyDsm)
{
if (_64bit) throw new InvalidOperationException("Cannot use legacy DSM under 64bit.");
}
_legacy = legacyDsm;
return this;
}
/// <summary>
/// Specifies what kind of data the app can to handle from TWAIN devices.
/// </summary>
/// <param name="image"></param>
/// <param name="audio"></param>
/// <returns></returns>
public TwainConfigurationBuilder HandlesDataType(bool image = true, bool audio = false)
{
DataGroups dg = DataGroups.None;
if (image) dg |= DataGroups.Image;
if (audio) dg |= DataGroups.Audio;
if (dg == DataGroups.None) throw new InvalidOperationException("No data type specified.");
_dg = dg;
return this;
}
/// <summary>
/// Defines the app info that will interface with TWAIN.
/// </summary>
/// <param name="appName"></param>
/// <param name="companyName"></param>
/// <returns></returns>
public TwainConfigurationBuilder DefineApp(string appName, string companyName = null)
{
_appName = appName;
_companyName = companyName;
return this;
}
/// <summary>
/// Defines the app info that will interface with TWAIN.
/// </summary>
/// <param name="appAssembly">Assembly containing the app info.</param>
/// <returns></returns>
public TwainConfigurationBuilder DefineApp(Assembly appAssembly)
{
var info = FileVersionInfo.GetVersionInfo(appAssembly.Location);
return DefineApp(info.ProductName, info.CompanyName);
}
/// <summary>
/// Builds the final configuration object.
/// </summary>
/// <returns></returns>
public TwainConfig Build()
{
return new TwainConfig
{
PreferLegacyDsm = _legacy,
AppName = _appName,
DataGroup = _dg
};
}
}
} }

View File

@@ -0,0 +1,141 @@
using NTwain.Data;
using System;
using System.Diagnostics;
using System.Reflection;
namespace NTwain
{
public class TwainConfigurationBuilder
{
private bool _legacy;
private string _appName;
private Version _version;
private string _companyName;
private Language _lang;
private DataGroups _dg = DataGroups.Image;
private bool _64bit;
private PlatformID _platform;
private Country _country;
/// <summary>
/// Default ctor.
/// </summary>
public TwainConfigurationBuilder()
{
_64bit = IntPtr.Size == 8;
_platform = Environment.OSVersion.Platform;
}
/// <summary>
/// Specifies which DSM to use.
/// </summary>
/// <param name="legacyDsm"></param>
/// <returns></returns>
public TwainConfigurationBuilder UseDsm(bool legacyDsm = false)
{
if (legacyDsm)
{
if (_64bit) throw new InvalidOperationException("Cannot use legacy DSM under 64bit.");
}
_legacy = legacyDsm;
return this;
}
/// <summary>
/// Specifies what kind of data the app can to handle from TWAIN devices.
/// </summary>
/// <param name="image"></param>
/// <param name="audio"></param>
/// <returns></returns>
public TwainConfigurationBuilder HandlesDataType(bool image = true, bool audio = false)
{
DataGroups dg = DataGroups.None;
if (image) dg |= DataGroups.Image;
if (audio) dg |= DataGroups.Audio;
if (dg == DataGroups.None) throw new InvalidOperationException("No data type specified.");
_dg = dg;
return this;
}
/// <summary>
/// Defines the app info that will interface with TWAIN.
/// </summary>
/// <param name="appName"></param>
/// <param name="appVersion"></param>
/// <param name="companyName"></param>
/// <param name="language"></param>
/// <param name="country"></param>
/// <returns></returns>
public TwainConfigurationBuilder DefineApp(string appName,
Version appVersion, string companyName = null,
Language language = Language.EnglishUSA, Country country = Country.USA)
{
_appName = appName;
_version = appVersion;
_companyName = companyName;
_lang = language;
_country = country;
return this;
}
/// <summary>
/// Defines the app info that will interface with TWAIN.
/// </summary>
/// <param name="appAssembly">Assembly containing the app info.</param>
/// <param name="language"></param>
/// <param name="country"></param>
/// <returns></returns>
public TwainConfigurationBuilder DefineApp(Assembly appAssembly,
Language language = Language.EnglishUSA, Country country = Country.USA)
{
var info = FileVersionInfo.GetVersionInfo(appAssembly.Location);
return DefineApp(info.ProductName, appAssembly.GetName().Version, info.CompanyName, language, country);
}
/// <summary>
/// Builds the final configuration object.
/// </summary>
/// <returns></returns>
public TwainConfig Build()
{
ITW_IDENTITY app = null;
// todo: change id based on platform
switch (_platform)
{
case PlatformID.Win32NT:
case PlatformID.Unix:
case PlatformID.MacOSX:
default:
app = new TW_IDENTITY
{
DataFunctionalities = DataFunctionalities.App2,
DataGroup = DataGroups.Control | _dg,
Manufacturer = _companyName ?? "Unknown",
ProductFamily = _appName ?? "Unknown",
ProductName = _appName ?? "Unknown",
ProtocolMajor = TwainConst.ProtocolMajor,
ProtocolMinor = TwainConst.ProtocolMinor,
Version = new TW_VERSION
{
Country = _country,
Language = _lang,
Major = (short)_version.Major,
Minor = (short)_version.Minor,
Info = ""
},
};
break;
}
return new TwainConfig
{
PreferLegacyDsm = _legacy,
App = app
};
}
}
}