From 1dd7d811a9c0e9ff9d6343e613e8273ef31b3d09 Mon Sep 17 00:00:00 2001 From: Eugene Wang <8755753+soukoku@users.noreply.github.com> Date: Sat, 1 Apr 2023 22:01:17 -0400 Subject: [PATCH] Added custom ds data and fixed copy paste errors. --- src/NTwain/Triplets/DATCustomDsData.cs | 66 ++++++++++++++++++++++++++ src/NTwain/Triplets/DATEntryPoint.cs | 8 ++-- src/NTwain/Triplets/DATStatus.cs | 16 +++---- src/NTwain/Triplets/DATStatusUtf8.cs | 8 ++-- src/NTwain/Triplets/DGControl.cs | 3 ++ src/NTwain/TwainSession.PropEvents.cs | 54 +++++++++++++++++++++ 6 files changed, 139 insertions(+), 16 deletions(-) create mode 100644 src/NTwain/Triplets/DATCustomDsData.cs diff --git a/src/NTwain/Triplets/DATCustomDsData.cs b/src/NTwain/Triplets/DATCustomDsData.cs new file mode 100644 index 0000000..d38c28a --- /dev/null +++ b/src/NTwain/Triplets/DATCustomDsData.cs @@ -0,0 +1,66 @@ +using TWAINWorkingGroup; + +namespace NTwain.Triplets +{ + /// + /// Contains calls used with and . + /// + public class DATCustomDsData : TripletBase + { + public DATCustomDsData(TwainSession session) : base(session) + { + } + + /// + /// Loads and opens the specified data source. + /// + /// + /// + public STS Get(ref TW_CUSTOMDSDATA data) + { + return DoIt(MSG.GET, ref data); + } + + /// + /// Sets the customs data. + /// + /// + /// + public STS Set(ref TW_CUSTOMDSDATA data) + { + return DoIt(MSG.SET, ref data); + } + + STS DoIt(MSG msg, ref TW_CUSTOMDSDATA data) + { + var rc = STS.FAILURE; + if (TwainPlatform.IsWindows) + { + var app = Session.AppIdentity; + var ds = Session.CurrentSource; + if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) + { + rc = (STS)NativeMethods.WindowsTwain32DsmEntryCustomdsdata(ref app, ref ds, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); + } + else + { + rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryCustomdsdata(ref app, ref ds, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); + } + } + else if (TwainPlatform.IsMacOSX) + { + TW_IDENTITY_MACOSX app = Session.AppIdentity; + TW_IDENTITY_MACOSX ds = Session.CurrentSource; + if (TwainPlatform.PreferLegacyDSM) + { + rc = (STS)NativeMethods.MacosxTwainDsmEntryCustomdsdata(ref app, ref ds, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); + } + else + { + rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryCustomdsdata(ref app, ref ds, DG.CONTROL, DAT.CUSTOMDSDATA, msg, ref data); + } + } + return rc; + } + } +} diff --git a/src/NTwain/Triplets/DATEntryPoint.cs b/src/NTwain/Triplets/DATEntryPoint.cs index cee9687..5de9200 100644 --- a/src/NTwain/Triplets/DATEntryPoint.cs +++ b/src/NTwain/Triplets/DATEntryPoint.cs @@ -55,11 +55,11 @@ namespace NTwain.Triplets TW_IDENTITY_LEGACY dummy = default; if (TwainPlatform.Is32bit && TwainPlatform.PreferLegacyDSM) { - rc = (STS)NativeMethods.WindowsTwain32DsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry); + rc = (STS)NativeMethods.WindowsTwain32DsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); } else { - rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry); + rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); } } //else if (TwainPlatform.IsLinux) @@ -72,11 +72,11 @@ namespace NTwain.Triplets TW_IDENTITY_MACOSX dummy = default; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)NativeMethods.MacosxTwainDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry); + rc = (STS)NativeMethods.MacosxTwainDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); } else { - rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.PARENT, msg, ref entry); + rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryEntrypoint(ref app, ref dummy, DG.CONTROL, DAT.ENTRYPOINT, msg, ref entry); } } return rc; diff --git a/src/NTwain/Triplets/DATStatus.cs b/src/NTwain/Triplets/DATStatus.cs index a768c8e..07f68ae 100644 --- a/src/NTwain/Triplets/DATStatus.cs +++ b/src/NTwain/Triplets/DATStatus.cs @@ -26,11 +26,11 @@ namespace NTwain.Triplets 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); + rc = (STS)NativeMethods.WindowsTwain32DsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } else { - rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status); + rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } } else if (TwainPlatform.IsMacOSX) @@ -38,11 +38,11 @@ namespace NTwain.Triplets 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); + rc = (STS)NativeMethods.MacosxTwainDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } else { - rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status); + rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatusState3(ref app, IntPtr.Zero, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } } return rc; @@ -63,11 +63,11 @@ namespace NTwain.Triplets 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); + rc = (STS)NativeMethods.WindowsTwain32DsmEntryStatus(ref app, ref ds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } else { - rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatus(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status); + rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatus(ref app, ref ds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } } else if (TwainPlatform.IsMacOSX) @@ -76,11 +76,11 @@ namespace NTwain.Triplets TW_IDENTITY_MACOSX osxds = ds; if (TwainPlatform.PreferLegacyDSM) { - rc = (STS)NativeMethods.MacosxTwainDsmEntryStatus(ref app, ref osxds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status); + rc = (STS)NativeMethods.MacosxTwainDsmEntryStatus(ref app, ref osxds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } else { - rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatus(ref app, ref osxds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref status); + rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatus(ref app, ref osxds, DG.CONTROL, DAT.STATUS, MSG.GET, ref status); } } return rc; diff --git a/src/NTwain/Triplets/DATStatusUtf8.cs b/src/NTwain/Triplets/DATStatusUtf8.cs index 398a23d..8c7621f 100644 --- a/src/NTwain/Triplets/DATStatusUtf8.cs +++ b/src/NTwain/Triplets/DATStatusUtf8.cs @@ -33,11 +33,11 @@ namespace NTwain.Triplets 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); + rc = (STS)NativeMethods.WindowsTwain32DsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); } else { - rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref extendedStatus); + rc = (STS)NativeMethods.WindowsTwaindsmDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); } } else if (TwainPlatform.IsMacOSX) @@ -46,11 +46,11 @@ namespace NTwain.Triplets 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); + rc = (STS)NativeMethods.MacosxTwainDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); } else { - rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.IDENTITY, MSG.GET, ref extendedStatus); + rc = (STS)NativeMethods.MacosxTwaindsmDsmEntryStatusutf8(ref app, ref ds, DG.CONTROL, DAT.STATUSUTF8, MSG.GET, ref extendedStatus); } } return rc; diff --git a/src/NTwain/Triplets/DGControl.cs b/src/NTwain/Triplets/DGControl.cs index 50056a5..157357a 100644 --- a/src/NTwain/Triplets/DGControl.cs +++ b/src/NTwain/Triplets/DGControl.cs @@ -24,5 +24,8 @@ private DATStatusUtf8? _statusUtf8; public DATStatusUtf8 StatusUtf8 => _statusUtf8 ??= new DATStatusUtf8(Session); + private DATCustomDsData? _customDsData; + public DATCustomDsData CustomDsData => _customDsData ??= new DATCustomDsData(Session); + } } \ No newline at end of file diff --git a/src/NTwain/TwainSession.PropEvents.cs b/src/NTwain/TwainSession.PropEvents.cs index 64c53da..7085184 100644 --- a/src/NTwain/TwainSession.PropEvents.cs +++ b/src/NTwain/TwainSession.PropEvents.cs @@ -1,5 +1,6 @@ using NTwain.Triplets; using System; +using System.Runtime.InteropServices; using TWAINWorkingGroup; namespace NTwain @@ -68,6 +69,59 @@ namespace NTwain STATE _state = STATE.S1; + /// + /// Gets/sets the current source's settings as opaque data. + /// Returns null if not supported. This is only valid in . + /// + public byte[]? CustomDsData + { + get + { + TW_CUSTOMDSDATA data = default; + var sts = DGControl.CustomDsData.Get(ref data); + if (sts == STS.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(); + } + 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 sts = DGControl.CustomDsData.Set(ref data); + } + finally + { + // should be freed already if no error but just in case + if (data.hData != IntPtr.Zero) Free(data.hData); + } + } + } + + /// /// TWAIN triplet API calls with . ///