diff --git a/samples/Console32/Program.cs b/samples/Console32/Program.cs index b200b7a..05439c8 100644 --- a/samples/Console32/Program.cs +++ b/samples/Console32/Program.cs @@ -15,18 +15,18 @@ namespace SampleConsole var twain = new TwainSession(Assembly.GetExecutingAssembly().Location); twain.StateChanged += Twain_StateChanged; - var hwnd = IntPtr.Zero; + var hwnd = IntPtr.Zero; // required for windows var rc = twain.DGControl.Parent.OpenDSM(ref hwnd); Debug.WriteLine($"OpenDSM={rc}"); - if (rc == TWAINWorkingGroup.STS.SUCCESS) + if (rc == STS.SUCCESS) { Debug.WriteLine($"CloseDSM={rc}"); rc = twain.DGControl.Parent.CloseDSM(ref hwnd); } } - private static void Twain_StateChanged(TwainSession session, TWAINWorkingGroup.STATE state) + private static void Twain_StateChanged(TwainSession session, STATE state) { Console.WriteLine($"State changed to {state}"); } diff --git a/samples/WinForm32/Form1.cs b/samples/WinForm32/Form1.cs index 6c568c9..5e0aaaa 100644 --- a/samples/WinForm32/Form1.cs +++ b/samples/WinForm32/Form1.cs @@ -1,5 +1,6 @@ using NTwain; using System; +using System.ComponentModel; using System.Diagnostics; using System.Reflection; using System.Windows.Forms; @@ -21,7 +22,7 @@ namespace WinForm32 twain.StateChanged += Twain_StateChanged; } - private static void Twain_StateChanged(TwainSession session, TWAINWorkingGroup.STATE state) + private static void Twain_StateChanged(TwainSession session, STATE state) { Console.WriteLine($"State changed to {state}"); } @@ -34,12 +35,14 @@ namespace WinForm32 var hwnd = this.Handle; var rc = twain.DGControl.Parent.OpenDSM(ref hwnd); Debug.WriteLine($"OpenDSM={rc}"); + } - if (rc == TWAINWorkingGroup.STS.SUCCESS) - { - Debug.WriteLine($"CloseDSM={rc}"); - rc = twain.DGControl.Parent.CloseDSM(ref hwnd); - } + protected override void OnClosing(CancelEventArgs e) + { + var finalState = twain.TryStepdown(STATE.S2); + Debug.WriteLine($"Stepdown result state={finalState}"); + + base.OnClosing(e); } } } \ No newline at end of file diff --git a/src/NTwain/Triplets/DATParent.cs b/src/NTwain/Triplets/DATParent.cs index cd0a9d9..778817d 100644 --- a/src/NTwain/Triplets/DATParent.cs +++ b/src/NTwain/Triplets/DATParent.cs @@ -22,6 +22,7 @@ namespace NTwain.Triplets STS rc; if ((rc = DoIt(MSG.OPENDSM, ref hwnd)) == STS.SUCCESS) { + Session._hwnd = hwnd; Session.State = STATE.S3; // determine memory mgmt routines used if ((((DG)Session._appIdentityLegacy.SupportedGroups) & DG.DSM2) == DG.DSM2) @@ -46,8 +47,9 @@ namespace NTwain.Triplets STS rc; if ((rc = DoIt(MSG.CLOSEDSM, ref hwnd)) == STS.SUCCESS) { - Session.State = STATE.S2; + Session._hwnd = IntPtr.Zero; Session._entryPoint = default; + Session.State = STATE.S2; } return rc; } diff --git a/src/NTwain/TwainSession.cs b/src/NTwain/TwainSession.cs index 5a2c877..1e1033e 100644 --- a/src/NTwain/TwainSession.cs +++ b/src/NTwain/TwainSession.cs @@ -6,7 +6,7 @@ using TWAINWorkingGroup; namespace NTwain { - // this file contains initialization-related things. + // this file contains initialization/cleanup things. public partial class TwainSession { @@ -83,5 +83,22 @@ namespace NTwain DGImage = new DGImage(this); DGAudio = new DGAudio(this); } + + internal IntPtr _hwnd; + + /// + /// Tries to bring the TWAIN session down to some state. + /// + /// + /// The final state. + public STATE TryStepdown(STATE targetState) + { + if (State > targetState) + { + // shouldn't care about handle when closing really + DGControl.Parent.CloseDSM(ref _hwnd); + } + return State; + } } }