Tried to add status and statusutf8 reader.

This commit is contained in:
Eugene Wang 2023-04-01 21:39:22 -04:00
parent a73b128f6b
commit e33c786abd
6 changed files with 209 additions and 2 deletions

View File

@ -2488,7 +2488,7 @@ namespace TWAINWorkingGroup
/// Translates the contents of Status into a localized UTF8string.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 2)]
public struct TW_STATUSUTF8
public partial struct TW_STATUSUTF8
{
public TW_STATUS Status;
public uint Size;

View File

@ -1,6 +1,8 @@
using System;
using NTwain;
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace TWAINWorkingGroup
{
@ -600,6 +602,53 @@ namespace TWAINWorkingGroup
}
}
partial struct TW_STATUSUTF8
{
/// <summary>
/// Tries to read the text content and frees the memory.
/// </summary>
/// <param name="session"></param>
/// <returns></returns>
public string? ReadAndFree(TwainSession session)
{
string? val = null;
if (UTF8string != IntPtr.Zero && Size > 0)
{
var locked = session.Lock(UTF8string);
if (locked != IntPtr.Zero)
{
// does this work? who knows.
try
{
#if NETFRAMEWORK
var bytes = new byte[Size];
Marshal.Copy(locked, bytes, 0, bytes.Length);
val = Encoding.UTF8.GetString(bytes);
#else
val = Marshal.PtrToStringUTF8(locked, (int)Size);
#endif
}
finally
{
session.Unlock(UTF8string);
}
}
}
Free(session);
return val;
}
/// <summary>
/// Frees the memory if necessary.
/// </summary>
/// <param name="session"></param>
public void Free(TwainSession session)
{
session.Free(UTF8string);
UTF8string = IntPtr.Zero;
}
}
//partial struct TW_DEVICEEVENT
//{
// public TWDE Event { get { return (TWDE)_event; } }

View File

@ -72,6 +72,7 @@ namespace NTwain.Triplets
/// <summary>
/// Gets the default data source.
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public STS GetDefault(out TW_IDENTITY_LEGACY ds)
{
@ -98,6 +99,7 @@ namespace NTwain.Triplets
/// Gets the first available data source in an enumerating fashion
/// (use <see cref="GetNext"/> for subsequent ones).
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public STS GetFirst(out TW_IDENTITY_LEGACY ds)
{
@ -109,6 +111,7 @@ namespace NTwain.Triplets
/// Gets the next available data source in an enumerating fashion (after using <see cref="GetFirst"/>).
/// Ends when return values is <see cref="STS.ENDOFLIST"/>.
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public STS GetNext(out TW_IDENTITY_LEGACY ds)
{

View File

@ -0,0 +1,89 @@
using System;
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.CONTROL"/> and <see cref="DAT.STATUS"/>.
/// </summary>
public class DATStatus : TripletBase
{
public DATStatus(TwainSession session) : base(session)
{
}
/// <summary>
/// Gets the current status for the DSM.
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public STS GetForDSM(out TW_STATUS status)
{
status = default;
var rc = STS.FAILURE;
if (TwainPlatform.IsWindows)
{
var app = Session.AppIdentity;
if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.WindowsTwain32DsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
else
{
rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
}
else if (TwainPlatform.IsMacOSX)
{
TW_IDENTITY_MACOSX app = Session.AppIdentity;
if (TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.MacosxTwainDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
else
{
rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
}
return rc;
}
/// <summary>
/// Gets the status for the current source.
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public STS GetForDS(out TW_STATUS status)
{
status = default;
var ds = Session.CurrentSource;
var rc = STS.FAILURE;
if (TwainPlatform.IsWindows)
{
var app = Session.AppIdentity;
if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.WindowsTwain32DsmEntryStatus(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
else
{
rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatus(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
}
else if (TwainPlatform.IsMacOSX)
{
TW_IDENTITY_MACOSX app = Session.AppIdentity;
TW_IDENTITY_MACOSX osxds = ds;
if (TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.MacosxTwainDsmEntryStatus(ref app, ref osxds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
else
{
rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatus(ref app, ref osxds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status);
}
}
return rc;
}
}
}

View File

@ -0,0 +1,60 @@
using System;
using System.Text;
using TWAINWorkingGroup;
namespace NTwain.Triplets
{
/// <summary>
/// Contains calls used with <see cref="DG.CONTROL"/> and <see cref="DAT.STATUSUTF8"/>.
/// </summary>
public class DATStatusUtf8 : TripletBase
{
public DATStatusUtf8(TwainSession session) : base(session)
{
}
/// <summary>
/// Gets the extended text info for a previously received <see cref="TW_STATUS"/>.
/// If this is called you should try to extract the string value from it once
/// with <see cref="TW_STATUSUTF8.ReadAndFree"/> or call <see cref="TW_STATUSUTF8.Free"/>
/// so there's no memory leak.
/// </summary>
/// <param name="status"></param>
/// <param name="extendedStatus"></param>
/// <returns></returns>
public STS Get(TW_STATUS status, out TW_STATUSUTF8 extendedStatus)
{
extendedStatus = new TW_STATUSUTF8 { Status = status };
var rc = STS.FAILURE;
if (TwainPlatform.IsWindows)
{
var app = Session.AppIdentity;
var ds = Session.CurrentSource;
if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.WindowsTwain32DsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref extendedStatus);
}
else
{
rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref extendedStatus);
}
}
else if (TwainPlatform.IsMacOSX)
{
TW_IDENTITY_MACOSX app = Session.AppIdentity;
TW_IDENTITY_MACOSX ds = Session.CurrentSource;
if (TwainPlatform.PreferLegacyDSM)
{
rc = (STS)NativeMethods.MacosxTwainDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref extendedStatus);
}
else
{
rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref extendedStatus);
}
}
return rc;
}
}
}

View File

@ -18,5 +18,11 @@
private DATIdentity? _identity;
public DATIdentity Identity => _identity ??= new DATIdentity(Session);
private DATStatus? _status;
public DATStatus Status => _status ??= new DATStatus(Session);
private DATStatusUtf8? _statusUtf8;
public DATStatusUtf8 StatusUtf8 => _statusUtf8 ??= new DATStatusUtf8(Session);
}
}