mirror of
https://github.com/soukoku/ntwain.git
synced 2025-04-05 20:59:23 +08:00
Tried to add status and statusutf8 reader.
This commit is contained in:
parent
a73b128f6b
commit
e33c786abd
@ -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;
|
||||
|
@ -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; } }
|
||||
|
@ -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)
|
||||
{
|
||||
|
89
src/NTwain/Triplets/DATStatus.cs
Normal file
89
src/NTwain/Triplets/DATStatus.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
60
src/NTwain/Triplets/DATStatusUtf8.cs
Normal file
60
src/NTwain/Triplets/DATStatusUtf8.cs
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user