Testing new twain session ctor.

This commit is contained in:
Eugene Wang 2023-04-01 07:26:59 -04:00
parent 299e74621b
commit 003edcccce
13 changed files with 15392 additions and 15164 deletions

View File

@ -22,6 +22,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_twain-doc", "_twain-doc",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTwain", "src\NTwain\NTwain.csproj", "{3C8A3CF9-A60D-4F21-B866-D291A7AABD4A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NTwain-temp", "src\NTwain-temp\NTwain-temp.csproj", "{A7020B90-5CE4-43EF-A75D-5E1F9B501CAC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Console32", "samples\Console32\Console32.csproj", "{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -32,10 +36,20 @@ Global
{3C8A3CF9-A60D-4F21-B866-D291A7AABD4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C8A3CF9-A60D-4F21-B866-D291A7AABD4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C8A3CF9-A60D-4F21-B866-D291A7AABD4A}.Release|Any CPU.Build.0 = Release|Any CPU
{A7020B90-5CE4-43EF-A75D-5E1F9B501CAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7020B90-5CE4-43EF-A75D-5E1F9B501CAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7020B90-5CE4-43EF-A75D-5E1F9B501CAC}.Release|Any CPU.Build.0 = Release|Any CPU
{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{AAF9FE1B-B695-4AFD-A014-CA4DFDD06A79} = {707B4313-8EF8-4D0F-A95E-590783422187}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7459323B-44F6-4E07-8574-E1B4B525086B}
EndGlobalSection

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PlatformTarget>x86</PlatformTarget>
<RootNamespace>SampleConsole</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\NTwain\NTwain.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,13 @@
using NTwain;
using System.Reflection;
namespace SampleConsole
{
internal class Program
{
static void Main(string[] args)
{
var twain = new TwainSession(Environment.ProcessPath ?? Assembly.GetExecutingAssembly().Location);
}
}
}

View File

@ -37,6 +37,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
</Project>

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<PackageId>NTwain</PackageId>
<Description>Library containing the TWAIN API for dotnet.</Description>
<TargetFrameworks>net462;netcoreapp3.1;net5.0;net6.0;net7.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net462;net6.0;net7.0;netstandard2.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>11</LangVersion>
</PropertyGroup>

View File

@ -29,22 +29,22 @@ namespace NTwain
object Invoke(Delegate work, params object[] args);
}
///// <summary>
///// Async calls are marshalled to threadpool thread.
///// Should only be used in non-UI apps.
///// </summary>
//public class ThreadPoolMarshaller : IThreadMarshaller
//{
// public bool InvokeRequired => throw new NotImplementedException();
/// <summary>
/// Async calls are marshalled to threadpool thread.
/// Should only be used in non-UI apps.
/// </summary>
public class ThreadPoolMarshaller : IThreadMarshaller
{
public bool InvokeRequired => throw new NotImplementedException();
// public void BeginInvoke(Delegate work, params object[] args)
// {
// Task.Run(() => work.DynamicInvoke(args));
// }
public void BeginInvoke(Delegate work, params object[] args)
{
Task.Run(() => work.DynamicInvoke(args));
}
// public object Invoke(Delegate work, params object[] args)
// {
// return work.DynamicInvoke(args);
// }
//}
public object Invoke(Delegate work, params object[] args)
{
return work.DynamicInvoke(args);
}
}
}

View File

@ -1,4 +1,6 @@
using System;
// NOTE this is derived from the TWAIN.cs in twaincs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

View File

@ -10,12 +10,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.Security.Permissions" Version="6.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
</ItemGroup>
<!--<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>-->
</Project>

File diff suppressed because it is too large Load Diff

View File

@ -2147,7 +2147,7 @@ namespace TWAINWorkingGroup
public TW_STR32 ProductName;
}
[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Ansi)]
public struct TW_IDENTITY_LEGACY
public partial struct TW_IDENTITY_LEGACY
{
public uint Id;
public TW_VERSION Version;
@ -2172,7 +2172,7 @@ namespace TWAINWorkingGroup
public TW_STR32 ProductName;
}
[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Ansi)]
public struct TW_IDENTITY_MACOSX
public partial struct TW_IDENTITY_MACOSX
{
public uint Id;
public TW_VERSION Version;

View File

@ -1,6 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
@ -17,7 +15,7 @@ namespace TWAINWorkingGroup
static TwainPlatform()
{
Is64bit = IntPtr.Size == 8;
#if NETFRAMEWORK
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
@ -72,7 +70,7 @@ namespace TWAINWorkingGroup
/// <summary>
/// Contains value that don't fit into enums nicely.
/// </summary>
public class TwainConst
public static class TwainConst
{
/// <summary>
/// Don't care values...
@ -453,7 +451,7 @@ namespace TWAINWorkingGroup
}
public static implicit operator string(TW_STR32 value) => value.ToString();
public static explicit operator TW_STR32(string value) => new(value);
public static implicit operator TW_STR32(string value) => new(value);
}
@ -472,7 +470,7 @@ namespace TWAINWorkingGroup
}
public static implicit operator string(TW_STR64 value) => value.ToString();
public static explicit operator TW_STR64(string value) => new(value);
public static implicit operator TW_STR64(string value) => new(value);
}
partial struct TW_STR128
@ -490,7 +488,7 @@ namespace TWAINWorkingGroup
}
public static implicit operator string(TW_STR128 value) => value.ToString();
public static explicit operator TW_STR128(string value) => new(value);
public static implicit operator TW_STR128(string value) => new(value);
}
partial struct TW_STR255
@ -508,7 +506,7 @@ namespace TWAINWorkingGroup
}
public static implicit operator string(TW_STR255 value) => value.ToString();
public static explicit operator TW_STR255(string value) => new(value);
public static implicit operator TW_STR255(string value) => new(value);
}
partial struct TW_IDENTITY
@ -518,6 +516,56 @@ namespace TWAINWorkingGroup
return $"{Manufacturer} - {ProductFamily} - {ProductName} {Version} (TWAIN {ProtocolMajor}.{ProtocolMinor})";
}
}
partial struct TW_IDENTITY_MACOSX
{
public override string ToString()
{
return $"{Manufacturer} - {ProductFamily} - {ProductName} {Version} (TWAIN {ProtocolMajor}.{ProtocolMinor})";
}
}
partial struct TW_IDENTITY_LEGACY
{
public override string ToString()
{
return $"{Manufacturer} - {ProductFamily} - {ProductName} {Version} (TWAIN {ProtocolMajor}.{ProtocolMinor})";
}
public static implicit operator TW_IDENTITY(TW_IDENTITY_LEGACY value) => new()
{
Id = value.Id,
Manufacturer = value.Manufacturer,
ProductFamily = value.ProductFamily,
ProductName = value.ProductName,
ProtocolMajor = value.ProtocolMajor,
ProtocolMinor = value.ProtocolMinor,
SupportedGroups = value.SupportedGroups,
Version = new()
{
Country = value.Version.Country,
Info = value.Version.Info,
Language = value.Version.Language,
MajorNum = value.Version.MajorNum,
MinorNum = value.Version.MinorNum,
}
};
public static implicit operator TW_IDENTITY_MACOSX(TW_IDENTITY_LEGACY value) => new()
{
Id = value.Id,
Manufacturer = value.Manufacturer,
ProductFamily = value.ProductFamily,
ProductName = value.ProductName,
ProtocolMajor = value.ProtocolMajor,
ProtocolMinor = value.ProtocolMinor,
SupportedGroups = value.SupportedGroups,
Version = new()
{
Country = value.Version.Country,
Info = value.Version.Info,
Language = value.Version.Language,
MajorNum = value.Version.MajorNum,
MinorNum = value.Version.MinorNum,
}
};
}
partial struct TW_VERSION
{

View File

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TWAINWorkingGroup;
namespace NTwain
{
// this file contains property and event definitions
partial class TwainSession
{
private STATE _state = STATE.S1;
/// <summary>
/// Current TWAIN session state.
/// </summary>
public STATE State
{
get { return _state; }
private set
{
if (_state != value)
{
_state = value;
StateChanged?.Invoke(this, value);
}
}
}
/// <summary>
/// Fired when state changes.
/// </summary>
public event Action<TwainSession, STATE>? StateChanged;
}
}

102
src/NTwain/TwainSession.cs Normal file
View File

@ -0,0 +1,102 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TWAINWorkingGroup;
namespace NTwain
{
// this file contains initialization-related things.
public partial class TwainSession
{
static bool __encodingRegistered;
/// <summary>
/// Creates TWAIN session with app info derived an executable file.
/// </summary>
/// <param name="exeFilePath"></param>
/// <param name="appLanguage"></param>
/// <param name="appCountry"></param>
public TwainSession(string exeFilePath,
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) :
this(FileVersionInfo.GetVersionInfo(exeFilePath),
appLanguage, appCountry)
{ }
/// <summary>
/// Creates TWAIN session with app info derived from a <see cref="FileVersionInfo"/> object.
/// </summary>
/// <param name="appInfo"></param>
/// <param name="appLanguage"></param>
/// <param name="appCountry"></param>
public TwainSession(FileVersionInfo appInfo,
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) :
this(appInfo.CompanyName, appInfo.ProductName, appInfo.ProductName, new Version(appInfo.FileVersion),
appInfo.FileDescription, appLanguage, appCountry)
{ }
/// <summary>
/// Creates TWAIN session with explicit app info.
/// </summary>
/// <param name="companyName"></param>
/// <param name="productFamily"></param>
/// <param name="productName"></param>
/// <param name="productVersion"></param>
/// <param name="productDescription"></param>
/// <param name="appLanguage"></param>
/// <param name="appCountry"></param>
/// <param name="supportedTypes"></param>
public TwainSession(string companyName, string productFamily, string productName,
Version productVersion, string productDescription = "",
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA,
DG supportedTypes = DG.IMAGE)
{
if (!__encodingRegistered)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
__encodingRegistered = true;
}
_appIdentityLegacy = new()
{
Manufacturer = companyName,
ProductFamily = productFamily,
ProductName = productName,
ProtocolMajor = (ushort)TWON_PROTOCOL.MAJOR,
ProtocolMinor = (ushort)TWON_PROTOCOL.MINOR,
SupportedGroups = (uint)(supportedTypes | DG.APP2),
Version = new TW_VERSION
{
Country = appCountry,
Info = productDescription,
Language = appLanguage,
MajorNum = (ushort)productVersion.Major,
MinorNum = (ushort)productVersion.Minor,
}
};
if (TwainPlatform.IsLinux) _appIdentity = _appIdentityLegacy;
if (TwainPlatform.IsMacOSX) _appIdentityOSX = _appIdentityLegacy;
}
// really legacy version is the one to be used (except on mac) or
// until it doesn't work (special linux)
/// <summary>
/// Gets the app identity.
/// </summary>
public TW_IDENTITY_LEGACY AppIdentity => _appIdentityLegacy;
internal TW_IDENTITY_LEGACY _appIdentityLegacy;
internal TW_IDENTITY _appIdentity;
internal TW_IDENTITY_MACOSX _appIdentityOSX;
/// <summary>
/// Gets the current data source.
/// </summary>
public TW_IDENTITY_LEGACY DSIdentity => _dsIdentityLegacy;
internal TW_IDENTITY_LEGACY _dsIdentityLegacy;
internal TW_IDENTITY _dsIdentity;
internal TW_IDENTITY_MACOSX _dsIdentityOSX;
}
}