Add method to set filexfer options on xferready event.

This commit is contained in:
Eugene Wang
2025-02-15 18:39:13 -05:00
parent b2fe1c1bcd
commit e3dfa8f4dd
4 changed files with 91 additions and 70 deletions

View File

@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<!--change these in each release--> <!--change these in each release-->
<VersionPrefix>4.0.0.0</VersionPrefix> <VersionPrefix>4.0.0.0</VersionPrefix>
<VersionSuffix>alpha.5</VersionSuffix> <VersionSuffix>alpha.6</VersionSuffix>
<!--keep it the same until major # changes--> <!--keep it the same until major # changes-->
<AssemblyVersion>4.0.0.0</AssemblyVersion> <AssemblyVersion>4.0.0.0</AssemblyVersion>

View File

@@ -8,11 +8,11 @@ namespace NTwain
/// </summary> /// </summary>
public class TransferReadyEventArgs : EventArgs public class TransferReadyEventArgs : EventArgs
{ {
public TransferReadyEventArgs(TWSX imgXferMech, TWSX audXferMech, int pendingCount, TWEJ endOfJobFlag) public TransferReadyEventArgs(TwainAppSession twain, TWSX imgXferMech, TWSX audXferMech, int pendingCount, TWEJ endOfJobFlag)
{ {
_twain = twain;
ImgXferMech = imgXferMech; ImgXferMech = imgXferMech;
AudXferMech = audXferMech; AudXferMech = audXferMech;
//_twain = twain;
PendingCount = pendingCount; PendingCount = pendingCount;
EndOfJobFlag = endOfJobFlag; EndOfJobFlag = endOfJobFlag;
} }
@@ -43,9 +43,20 @@ namespace NTwain
/// </summary> /// </summary>
public int PendingCount { get; private set; } public int PendingCount { get; private set; }
//TW_IMAGEINFO? _imgInfo; private readonly TwainAppSession _twain;
//private readonly TwainAppSession _twain;
/// <summary>
/// If the transfer mech is file-related,
/// setup the file transfer options here.
/// </summary>
/// <param name="fileXfer"></param>
/// <returns></returns>
public STS SetupFileTransfer(ref TW_SETUPFILEXFER fileXfer)
{
return _twain.SetFileXfer(ref fileXfer);
}
//TW_IMAGEINFO? _imgInfo;
///// <summary> ///// <summary>
///// Gets the tentative image information for the current transfer if applicable. ///// Gets the tentative image information for the current transfer if applicable.
///// This may differ from the final image depending on the transfer mode used (mostly when doing mem xfer). ///// This may differ from the final image depending on the transfer mode used (mostly when doing mem xfer).

View File

@@ -3,45 +3,45 @@ using NTwain.Triplets;
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace NTwain namespace NTwain;
{
// this file contains property and event definitions
partial class TwainAppSession // this file contains property and event definitions
{
partial class TwainAppSession
{
/// <summary> /// <summary>
/// Gets the app identity. /// Gets the app identity.
/// </summary> /// </summary>
public TW_IDENTITY_LEGACY AppIdentity public TW_IDENTITY_LEGACY AppIdentity
{ {
get => _appIdentity; get => _appIdentity;
} }
TW_IDENTITY_LEGACY _appIdentity; internal TW_IDENTITY_LEGACY _appIdentity;
/// <summary> /// <summary>
/// Gets the current (opened) data source. /// Gets the current (opened) data source.
/// </summary> /// </summary>
public TW_IDENTITY_LEGACY CurrentSource public TW_IDENTITY_LEGACY CurrentSource
{ {
get => _currentDS; get => _currentDS;
protected set protected set
{
_currentDS = value;
try
{ {
CurrentSourceChanged?.Invoke(this, value); _currentDS = value;
try
{
CurrentSourceChanged?.Invoke(this, value);
}
catch { }
} }
catch { }
}
} }
TW_IDENTITY_LEGACY _currentDS; internal TW_IDENTITY_LEGACY _currentDS;
/// <summary> /// <summary>
/// Gets/sets the default data source. /// Gets/sets the default data source.
/// </summary> /// </summary>
public TW_IDENTITY_LEGACY DefaultSource public TW_IDENTITY_LEGACY DefaultSource
{ {
get => _defaultDS; get => _defaultDS;
} }
TW_IDENTITY_LEGACY _defaultDS; TW_IDENTITY_LEGACY _defaultDS;
@@ -51,19 +51,19 @@ namespace NTwain
/// </summary> /// </summary>
public STATE State public STATE State
{ {
get => _state; get => _state;
protected set protected set
{
if (_state != value)
{ {
_state = value; if (_state != value)
try {
{ _state = value;
StateChanged?.Invoke(this, value); try
} {
catch { } StateChanged?.Invoke(this, value);
}
catch { }
}
} }
}
} }
STATE _state = STATE.S1; STATE _state = STATE.S1;
@@ -74,49 +74,49 @@ namespace NTwain
/// </summary> /// </summary>
public byte[]? CustomDsData public byte[]? CustomDsData
{ {
get get
{
var rc = DGControl.CustomDsData.Get(ref _appIdentity, ref _currentDS, out TW_CUSTOMDSDATA data);
if (rc == TWRC.SUCCESS)
{ {
if (data.hData != IntPtr.Zero && data.InfoLength > 0) var rc = DGControl.CustomDsData.Get(ref _appIdentity, ref _currentDS, out TW_CUSTOMDSDATA data);
{ if (rc == TWRC.SUCCESS)
{
if (data.hData != IntPtr.Zero && data.InfoLength > 0)
{
try
{
var lockedPtr = Lock(data.hData);
var bytes = new byte[data.InfoLength];
Marshal.Copy(lockedPtr, bytes, 0, bytes.Length);
}
finally
{
Unlock(data.hData);
Free(data.hData);
}
}
//return Array.Empty<byte>();
}
return null;
}
set
{
if (value == null || value.Length == 0) return;
TW_CUSTOMDSDATA data = default;
data.InfoLength = (uint)value.Length;
data.hData = Alloc(data.InfoLength);
try try
{ {
var lockedPtr = Lock(data.hData); var lockedPtr = Lock(data.hData);
var bytes = new byte[data.InfoLength]; Marshal.Copy(value, 0, lockedPtr, value.Length);
Marshal.Copy(lockedPtr, bytes, 0, bytes.Length); Unlock(data.hData);
var rc = DGControl.CustomDsData.Set(ref _appIdentity, ref _currentDS, ref data);
} }
finally finally
{ {
Unlock(data.hData); // should be freed already if no error but just in case
Free(data.hData); if (data.hData != IntPtr.Zero) Free(data.hData);
} }
}
//return Array.Empty<byte>();
} }
return null;
}
set
{
if (value == null || value.Length == 0) return;
TW_CUSTOMDSDATA data = default;
data.InfoLength = (uint)value.Length;
data.hData = Alloc(data.InfoLength);
try
{
var lockedPtr = Lock(data.hData);
Marshal.Copy(value, 0, lockedPtr, value.Length);
Unlock(data.hData);
var rc = DGControl.CustomDsData.Set(ref _appIdentity, ref _currentDS, ref data);
}
finally
{
// should be freed already if no error but just in case
if (data.hData != IntPtr.Zero) Free(data.hData);
}
}
} }
@@ -166,5 +166,4 @@ namespace NTwain
/// This is NOT raised on the UI thread for reasons. /// This is NOT raised on the UI thread for reasons.
/// </summary> /// </summary>
public event TwainEventDelegate<TransferredEventArgs>? Transferred; public event TwainEventDelegate<TransferredEventArgs>? Transferred;
}
} }

View File

@@ -23,6 +23,17 @@ namespace NTwain
return WrapInSTS(DGImage.ExtImageInfo.Get(ref _appIdentity, ref _currentDS, ref container)); return WrapInSTS(DGImage.ExtImageInfo.Get(ref _appIdentity, ref _currentDS, ref container));
} }
/// <summary>
/// Can only be called in state 6, so it's here and only exposed
/// in transfer ready event.
/// </summary>
/// <param name="settings"></param>
/// <returns></returns>
internal STS SetFileXfer(ref TW_SETUPFILEXFER settings)
{
return WrapInSTS(DGControl.SetupFileXfer.Set(ref _appIdentity, ref _currentDS, ref settings));
}
/// <summary> /// <summary>
/// Start the transfer loop. /// Start the transfer loop.
@@ -66,7 +77,7 @@ namespace NTwain
{ {
do do
{ {
var readyArgs = new TransferReadyEventArgs(imgXferMech, audXferMech, pending.Count, (TWEJ)pending.EOJ); var readyArgs = new TransferReadyEventArgs(this, imgXferMech, audXferMech, pending.Count, (TWEJ)pending.EOJ);
try try
{ {
TransferReady?.Invoke(this, readyArgs); TransferReady?.Invoke(this, readyArgs);