Reduced ctors for TwainAppSession.

This commit is contained in:
Eugene Wang 2023-04-20 23:33:47 -04:00
parent d68e9800ba
commit f25587cab2
4 changed files with 71 additions and 73 deletions

View File

@ -11,7 +11,7 @@ namespace WinConsole32
var libVer = FileVersionInfo.GetVersionInfo(typeof(TwainAppSession).Assembly.Location).ProductVersion;
Console.WriteLine($"Console sample {(TWPlatform.Is32bit ? " 32bit" : " 64bit")} on NTwain {libVer}");
TwainAppSession session = new TwainAppSession(Environment.ProcessPath!);
TwainAppSession session = new TwainAppSession(new TW_IDENTITY_LEGACY(Environment.ProcessPath!));
session.StateChanged += Session_StateChanged;
session.SourceDisabled += Session_SourceDisabled1;

View File

@ -37,7 +37,7 @@ namespace WinFormSample
TWPlatform.PreferLegacyDSM = false;
twain = new TwainAppSession(Assembly.GetExecutingAssembly().Location);
twain = new TwainAppSession(new TW_IDENTITY_LEGACY(Assembly.GetExecutingAssembly().Location));
twain.StateChanged += Twain_StateChanged;
twain.DefaultSourceChanged += Twain_DefaultSourceChanged;
twain.CurrentSourceChanged += Twain_CurrentSourceChanged;

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace NTwain.Data
{
@ -32,6 +34,7 @@ namespace NTwain.Data
}
#else
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
IsWindows = true;
@ -657,6 +660,68 @@ namespace NTwain.Data
}
partial struct TW_IDENTITY_LEGACY
{
/// <summary>
/// Creates app info derived an executable file.
/// </summary>
/// <param name="exeFilePath"></param>
/// <param name="appLanguage"></param>
/// <param name="appCountry"></param>
public TW_IDENTITY_LEGACY(
string exeFilePath,
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) :
this(FileVersionInfo.GetVersionInfo(exeFilePath), appLanguage, appCountry)
{ }
/// <summary>
/// Creates app info derived from a <see cref="FileVersionInfo"/> object.
/// </summary>
/// <param name="appInfo"></param>
/// <param name="appLanguage"></param>
/// <param name="appCountry"></param>
public TW_IDENTITY_LEGACY(
FileVersionInfo appInfo,
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) :
this(
appInfo.CompanyName ?? "",
appInfo.ProductName ?? "",
appInfo.ProductName ?? "",
new Version(appInfo.FileVersion ?? "1.0"),
appInfo.FileDescription ?? "", appLanguage, appCountry)
{ }
/// <summary>
/// Creates id 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 TW_IDENTITY_LEGACY(
string companyName, string productFamily, string productName,
Version productVersion, string productDescription = "",
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA,
DG supportedTypes = DG.IMAGE)
{
Manufacturer = companyName;
ProductFamily = productFamily;
ProductName = productName;
ProtocolMajor = (ushort)TWON_PROTOCOL.MAJOR;
ProtocolMinor = (ushort)TWON_PROTOCOL.MINOR;
SupportedGroups = (uint)(supportedTypes | DG.CONTROL | DG.APP2);
Version = new TW_VERSION
{
Country = appCountry,
Info = productDescription,
Language = appLanguage,
MajorNum = (ushort)productVersion.Major,
MinorNum = (ushort)productVersion.Minor,
};
}
/// <summary>
/// A simplified check on whether this has valid data from DSM.
/// </summary>

View File

@ -1,13 +1,10 @@
using NTwain.Data;
using NTwain.Triplets;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace NTwain
{
@ -15,81 +12,16 @@ namespace NTwain
public partial class TwainAppSession : IDisposable
{
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 TwainAppSession(
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 TwainAppSession(
FileVersionInfo appInfo,
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA) :
this(
appInfo.CompanyName ?? "",
appInfo.ProductName ?? "",
appInfo.ProductName ?? "",
new Version(appInfo.FileVersion ?? "1.0"),
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 TwainAppSession(
string companyName, string productFamily, string productName,
Version productVersion, string productDescription = "",
TWLG appLanguage = TWLG.ENGLISH_USA, TWCY appCountry = TWCY.USA,
DG supportedTypes = DG.IMAGE)
/// <param name="appId"></param>
public TwainAppSession(TW_IDENTITY_LEGACY appId)
{
#if WINDOWS || NETFRAMEWORK
DSM.DsmLoader.TryLoadCustomDSM();
#endif
// todo: find a better place for this
if (!__encodingRegistered)
{
#if !NETFRAMEWORK
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endif
__encodingRegistered = true;
}
_appIdentity = new()
{
Manufacturer = companyName,
ProductFamily = productFamily,
ProductName = productName,
ProtocolMajor = (ushort)TWON_PROTOCOL.MAJOR,
ProtocolMinor = (ushort)TWON_PROTOCOL.MINOR,
SupportedGroups = (uint)(supportedTypes | DG.CONTROL | DG.APP2),
Version = new TW_VERSION
{
Country = appCountry,
Info = productDescription,
Language = appLanguage,
MajorNum = (ushort)productVersion.Major,
MinorNum = (ushort)productVersion.Minor,
}
};
_appIdentity = appId;
_legacyCallbackDelegate = LegacyCallbackHandler;
_osxCallbackDelegate = OSXCallbackHandler;
@ -97,6 +29,7 @@ namespace NTwain
StartTransferThread();
}
internal IntPtr _hwnd;
internal TW_USERINTERFACE _userInterface; // kept around for disable to use
#if WINDOWS || NETFRAMEWORK