Attempt to resurrect my old triplet ideas with new data types and native methods.

This commit is contained in:
Eugene Wang 2023-04-01 12:57:07 -04:00
parent 260ae9fdef
commit e68525775d
17 changed files with 479 additions and 10 deletions

View File

@ -26,6 +26,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTwain-temp", "src\NTwain-t
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Console32", "samples\Console32\Console32.csproj", "{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinForm32", "samples\WinForm32\WinForm32.csproj", "{7792A94E-D0B4-440D-8BD5-CA1CA548782C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -43,12 +45,17 @@ Global
{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79}.Release|Any CPU.Build.0 = Release|Any CPU
{7792A94E-D0B4-440D-8BD5-CA1CA548782C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7792A94E-D0B4-440D-8BD5-CA1CA548782C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7792A94E-D0B4-440D-8BD5-CA1CA548782C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7792A94E-D0B4-440D-8BD5-CA1CA548782C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79} = {707B4313-8EF8-4D0F-A95E-590783422187}
{7792A94E-D0B4-440D-8BD5-CA1CA548782C} = {707B4313-8EF8-4D0F-A95E-590783422187}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7459323B-44F6-4E07-8574-E1B4B525086B}

View File

@ -2,11 +2,11 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<TargetFrameworks>net462;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<PlatformTarget>x86</PlatformTarget>
<RootNamespace>SampleConsole</RootNamespace>
<LangVersion>11</LangVersion>
</PropertyGroup>
<ItemGroup>

View File

@ -1,5 +1,8 @@
using NTwain;
using System;
using System.Diagnostics;
using System.Reflection;
using TWAINWorkingGroup;
namespace SampleConsole
{
@ -7,8 +10,20 @@ namespace SampleConsole
{
static void Main(string[] args)
{
var twain = new TwainSession(Environment.ProcessPath ?? Assembly.GetExecutingAssembly().Location);
TwainPlatform.PreferLegacyDSM = true;
var twain = new TwainSession(Assembly.GetExecutingAssembly().Location);
twain.StateChanged += Twain_StateChanged;
var hwnd = IntPtr.Zero;
var rc = twain.DGControl.Parent.OpenDSM(ref hwnd);
Debug.WriteLine($"OpenDSM={rc}");
if (rc == TWAINWorkingGroup.STS.SUCCESS)
{
Debug.WriteLine($"CloseDSM={rc}");
rc = twain.DGControl.Parent.CloseDSM(ref hwnd);
}
}
private static void Twain_StateChanged(TwainSession session, TWAINWorkingGroup.STATE state)

39
samples/WinForm32/Form1.Designer.cs generated Normal file
View File

@ -0,0 +1,39 @@
namespace WinForm32
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
}

View File

@ -0,0 +1,51 @@
using NTwain;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using TWAINWorkingGroup;
namespace WinForm32
{
public partial class Form1 : Form
{
private TwainSession twain;
public Form1()
{
InitializeComponent();
InitTwain();
}
private void InitTwain()
{
TwainPlatform.PreferLegacyDSM = true;
twain = new TwainSession(Assembly.GetExecutingAssembly().Location);
twain.StateChanged += Twain_StateChanged;
}
private static void Twain_StateChanged(TwainSession session, TWAINWorkingGroup.STATE state)
{
Console.WriteLine($"State changed to {state}");
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
var hwnd = this.Handle;
var rc = twain.DGControl.Parent.OpenDSM(ref hwnd);
Debug.WriteLine($"OpenDSM={rc}");
if (rc == TWAINWorkingGroup.STS.SUCCESS)
{
Debug.WriteLine($"CloseDSM={rc}");
rc = twain.DGControl.Parent.CloseDSM(ref hwnd);
}
}
}
}

View File

@ -0,0 +1,120 @@
<?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>
</root>

View File

@ -0,0 +1,20 @@
using System;
using System.Windows.Forms;
namespace WinForm32
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\NTwain\NTwain.csproj" />
</ItemGroup>
</Project>

View File

@ -14,7 +14,7 @@ namespace TWAINWorkingGroup
{
static TwainPlatform()
{
Is64bit = IntPtr.Size == 8;
Is32bit = IntPtr.Size == 4;
#if NETFRAMEWORK
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
@ -64,7 +64,14 @@ namespace TWAINWorkingGroup
/// <summary>
/// Whether the code is running in 64bit or 32bit.
/// </summary>
public static bool Is64bit { get; }
public static bool Is32bit { get; }
/// <summary>
/// Whether to use the older DSM lib on Windows and Mac.
/// On Windows it only takes effect when running in 32bit.
/// Defaults to false.
/// </summary>
public static bool PreferLegacyDSM { get; set; }
}
/// <summary>

View File

@ -0,0 +1,85 @@
using System;
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.CONTROL"/> and <see cref="DAT.PARENT"/>.
/// </summary>
public class DATParent : TripletBase
{
public DATParent(TwainSession session) : base(session)
{
}
/// <summary>
/// Loads and opens the DSM.
/// </summary>
/// <param name="hwnd">Required if on Windows.</param>
/// <returns></returns>
public STS OpenDSM(ref IntPtr hwnd)
{
STS rc;
if ((rc = DoIt(MSG.OPENDSM, ref hwnd)) == STS.SUCCESS)
{
Session.State = STATE.S3;
}
return rc;
}
/// <summary>
/// Closes the DSM.
/// </summary>
/// <param name="hwnd">Required if on Windows.</param>
/// <returns></returns>
public STS CloseDSM(ref IntPtr hwnd)
{
STS rc;
if ((rc = DoIt(MSG.CLOSEDSM, ref hwnd)) == STS.SUCCESS)
{
Session.State = STATE.S2;
}
return rc;
}
STS DoIt(MSG msg, ref IntPtr hwnd)
{
var rc = STS.FAILURE;
if (TwainPlatform.IsWindows)
{
var app = Session._appIdentityLegacy;
if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.WindowsTwain32DsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
else
{
rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
if (rc == STS.SUCCESS) Session._appIdentity = app;
}
else if (TwainPlatform.IsLinux)
{
var app = Session._appIdentityLegacy;
rc = (STS)NativeMethods.LinuxDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
if (rc == STS.SUCCESS) Session._appIdentity = app;
}
else if (TwainPlatform.IsMacOSX)
{
var app = Session._appIdentityOSX;
if (TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.MacosxTwainDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
else
{
rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryParent(ref app, IntPtr.Zero, DG.CONTROL, DAT.PARENT, msg, ref hwnd);
}
if (rc == STS.SUCCESS) Session._appIdentityOSX = app;
}
return rc;
}
}
}

View File

@ -0,0 +1,14 @@
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.AUDIO"/>.
/// </summary>
public class DGAudio : TripletBase
{
public DGAudio(TwainSession session) : base(session)
{
}
}
}

View File

@ -0,0 +1,16 @@
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.CONTROL"/>.
/// </summary>
public class DGControl : TripletBase
{
public DGControl(TwainSession session) : base(session)
{
}
private DATParent? _parent;
public DATParent Parent => _parent ??= new DATParent(Session);
}
}

View File

@ -0,0 +1,14 @@
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.IMAGE"/>.
/// </summary>
public class DGImage : TripletBase
{
public DGImage(TwainSession session) : base(session)
{
}
}
}

View File

@ -0,0 +1,22 @@
All TWAIN operations are done through the a combination of
Data Group (DG), Data Argument Type (DAT), and Message (MSG)
triplets. Rather than letting consumers of this lib deal
with all the combinations themselves and risk passing the
wrong thing, all valid triplet combinations are simply
made available under this namespace.
Example:
To get the status of the DS, just use the
"Get" method (represents MSG), in the
"Status" property (represnts DAT), in the
"DGControl" class (represents DG).
or better explained in code:
DGControl.Status.Get(...)
Only triplets usable by the
application-side are defined here.
Due to the number of things involved, the properties are
lazy-loaded until used.

View File

@ -0,0 +1,25 @@
using System;
namespace NTwain.Triplets
{
/// <summary>
/// Base class for grouping triplet operations messages.
/// </summary>
public abstract class TripletBase
{
/// <summary>
/// Initializes a new instance of the <see cref="TripletBase" /> class.
/// </summary>
/// <param name="session">The session.</param>
/// <exception cref="System.ArgumentNullException"></exception>
public TripletBase(TwainSession session)
{
Session = session ?? throw new ArgumentNullException("session");
}
/// <summary>
/// Gets the twain session.
/// </summary>
public TwainSession Session { get; }
}
}

View File

@ -1,4 +1,5 @@
using System;
using NTwain.Triplets;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@ -42,7 +43,7 @@ namespace NTwain
public STATE State
{
get => _state;
private set
internal set
{
if (_state != value)
{
@ -53,8 +54,21 @@ namespace NTwain
}
/// <summary>
/// Fired when state changes.
/// Fired when <see cref="State"/> changes.
/// </summary>
public event Action<TwainSession, STATE>? StateChanged;
/// <summary>
/// TWAIN triplet API calls with <see cref="DG.CONTROL"/>.
/// </summary>
public DGControl DGControl { get; }
/// <summary>
/// TWAIN triplet API calls with <see cref="DG.IMAGE"/>.
/// </summary>
public DGImage DGImage { get; }
/// <summary>
/// TWAIN triplet API calls with <see cref="DG.AUDIO"/>.
/// </summary>
public DGAudio DGAudio { get; }
}
}

View File

@ -1,4 +1,5 @@
using System;
using NTwain.Triplets;
using System;
using System.Diagnostics;
using System.Text;
using TWAINWorkingGroup;
@ -62,7 +63,7 @@ namespace NTwain
ProductName = productName,
ProtocolMajor = (ushort)TWON_PROTOCOL.MAJOR,
ProtocolMinor = (ushort)TWON_PROTOCOL.MINOR,
SupportedGroups = (uint)(supportedTypes | DG.APP2),
SupportedGroups = (uint)(supportedTypes | DG.CONTROL | DG.APP2),
Version = new TW_VERSION
{
Country = appCountry,
@ -74,6 +75,10 @@ namespace NTwain
};
if (TwainPlatform.IsLinux) _appIdentity = _appIdentityLegacy;
else if (TwainPlatform.IsMacOSX) _appIdentityOSX = _appIdentityLegacy;
DGControl = new DGControl(this);
DGImage = new DGImage(this);
DGAudio = new DGAudio(this);
}
}
}