From 94f2720de4fdc9a06cf2d160b954e655755ee02f Mon Sep 17 00:00:00 2001 From: soukoku Date: Sat, 5 Apr 2014 18:33:21 -0400 Subject: [PATCH] More progress. --- NTwain/Data/TypesExtended.cs | 30 +- NTwain/IMemoryManager.cs | 16 +- NTwain/ITwainState.cs | 14 +- NTwain/MemoryManager.cs | 16 +- .../Triplets/DGAudio/DGAudio.AudioFileXfer.cs | 2 +- NTwain/Triplets/DGAudio/DGAudio.AudioInfo.cs | 2 +- .../DGAudio/DGAudio.AudioNativeXfer.cs | 2 +- .../Triplets/DGControl/DGControl.Callback.cs | 2 +- .../Triplets/DGControl/DGControl.Callback2.cs | 2 +- .../DGControl/DGControl.Capability.cs | 22 +- .../DGControl/DGControl.CustomDSData.cs | 4 +- .../DGControl/DGControl.DeviceEvent.cs | 2 +- .../DGControl/DGControl.EntryPoint.cs | 2 +- NTwain/Triplets/DGControl/DGControl.Event.cs | 2 +- .../DGControl/DGControl.FileSystem.cs | 22 +- .../Triplets/DGControl/DGControl.Identity.cs | 14 +- NTwain/Triplets/DGControl/DGControl.Parent.cs | 8 +- .../Triplets/DGControl/DGControl.PassThru.cs | 2 +- .../DGControl/DGControl.PendingXfers.cs | 8 +- .../DGControl/DGControl.SetupFileXfer.cs | 8 +- .../DGControl/DGControl.SetupMemXfer.cs | 2 +- NTwain/Triplets/DGControl/DGControl.Status.cs | 4 +- .../DGControl/DGControl.StatusUtf8.cs | 2 +- .../DGControl/DGControl.UserInterface.cs | 6 +- .../Triplets/DGControl/DGControl.XferGroup.cs | 4 +- NTwain/Triplets/DGImage/DGImage.CieColor.cs | 2 +- .../Triplets/DGImage/DGImage.ExtImageInfo.cs | 2 +- NTwain/Triplets/DGImage/DGImage.Filter.cs | 8 +- .../Triplets/DGImage/DGImage.GrayResponse.cs | 4 +- NTwain/Triplets/DGImage/DGImage.IccProfile.cs | 2 +- .../Triplets/DGImage/DGImage.ImageFileXfer.cs | 2 +- NTwain/Triplets/DGImage/DGImage.ImageInfo.cs | 2 +- .../Triplets/DGImage/DGImage.ImageLayout.cs | 8 +- .../DGImage/DGImage.ImageMemFileXfer.cs | 2 +- .../Triplets/DGImage/DGImage.ImageMemXfer.cs | 2 +- .../DGImage/DGImage.ImageNativeXfer.cs | 2 +- .../DGImage/DGImage.JpegCompression.cs | 8 +- NTwain/Triplets/DGImage/DGImage.Palette8.cs | 8 +- .../Triplets/DGImage/DGImage.RgbResponse.cs | 4 +- NTwain/TwainSession.cs | 35 +- NTwain/TwainSessionBase.cs | 306 +++++++++++++++++- NTwain/Values/DataValues.cs | 2 +- Tests/Tester.Winform/TestForm.cs | 2 +- 43 files changed, 438 insertions(+), 161 deletions(-) diff --git a/NTwain/Data/TypesExtended.cs b/NTwain/Data/TypesExtended.cs index 48e8dc0..40164fc 100644 --- a/NTwain/Data/TypesExtended.cs +++ b/NTwain/Data/TypesExtended.cs @@ -665,7 +665,7 @@ namespace NTwain.Data var value = new TWOneValue(); if (_hContainer != IntPtr.Zero) { - IntPtr baseAddr = MemoryManager.Instance.MemLock(_hContainer); + IntPtr baseAddr = MemoryManager.Instance.Lock(_hContainer); try { int offset = 0; @@ -674,7 +674,7 @@ namespace NTwain.Data value.Item = (uint)ReadValue(baseAddr, ref offset, ItemType.UInt32); } catch { } - MemoryManager.Instance.MemUnlock(_hContainer); + MemoryManager.Instance.Unlock(_hContainer); } return value; } @@ -688,7 +688,7 @@ namespace NTwain.Data // since one value can only house UInt32 we will not allow type size > 4 if (GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException("Invalid one value type"); } - _hContainer = MemoryManager.Instance.MemAllocate((uint)Marshal.SizeOf(value)); + _hContainer = MemoryManager.Instance.Allocate((uint)Marshal.SizeOf(value)); if (_hContainer != IntPtr.Zero) { Marshal.StructureToPtr(value, _hContainer, false); @@ -707,7 +707,7 @@ namespace NTwain.Data var value = new TWArray(); if (_hContainer != IntPtr.Zero) { - IntPtr baseAddr = MemoryManager.Instance.MemLock(_hContainer); + IntPtr baseAddr = MemoryManager.Instance.Lock(_hContainer); int offset = 0; value.ItemType = (ItemType)(ushort)Marshal.ReadInt16(baseAddr, offset); offset += 2; @@ -721,7 +721,7 @@ namespace NTwain.Data value.ItemList[i] = ReadValue(baseAddr, ref offset, value.ItemType); } } - MemoryManager.Instance.MemUnlock(_hContainer); + MemoryManager.Instance.Unlock(_hContainer); } return value; } @@ -738,7 +738,7 @@ namespace NTwain.Data var value = new TWEnumeration(); if (_hContainer != IntPtr.Zero) { - IntPtr baseAddr = MemoryManager.Instance.MemLock(_hContainer); + IntPtr baseAddr = MemoryManager.Instance.Lock(_hContainer); int offset = 0; value.ItemType = (ItemType)(ushort)Marshal.ReadInt16(baseAddr, offset); offset += 2; @@ -756,7 +756,7 @@ namespace NTwain.Data value.ItemList[i] = ReadValue(baseAddr, ref offset, value.ItemType); } } - MemoryManager.Instance.MemUnlock(_hContainer); + MemoryManager.Instance.Unlock(_hContainer); } return value; } @@ -770,8 +770,8 @@ namespace NTwain.Data Int32 valueSize = value.ItemOffset + value.ItemList.Length * GetItemTypeSize(value.ItemType); int offset = 0; - _hContainer = MemoryManager.Instance.MemAllocate((uint)valueSize); - IntPtr baseAddr = MemoryManager.Instance.MemLock(_hContainer); + _hContainer = MemoryManager.Instance.Allocate((uint)valueSize); + IntPtr baseAddr = MemoryManager.Instance.Lock(_hContainer); // can't safely use StructureToPtr here so write it our own WriteValue(baseAddr, ref offset, ItemType.UInt16, value.ItemType); @@ -782,7 +782,7 @@ namespace NTwain.Data { WriteValue(baseAddr, ref offset, value.ItemType, item); } - MemoryManager.Instance.MemUnlock(_hContainer); + MemoryManager.Instance.Unlock(_hContainer); } /// @@ -797,7 +797,7 @@ namespace NTwain.Data var value = new TWRange(); if (_hContainer != IntPtr.Zero) { - IntPtr baseAddr = MemoryManager.Instance.MemLock(_hContainer); + IntPtr baseAddr = MemoryManager.Instance.Lock(_hContainer); int offset = 0; value.ItemType = (ItemType)(ushort)Marshal.ReadInt16(baseAddr, offset); @@ -812,7 +812,7 @@ namespace NTwain.Data offset += 4; value.CurrentValue = (uint)Marshal.ReadInt32(baseAddr, offset); - MemoryManager.Instance.MemUnlock(_hContainer); + MemoryManager.Instance.Unlock(_hContainer); } return value; } @@ -825,7 +825,7 @@ namespace NTwain.Data // since range value can only house UInt32 we will not allow type size > 4 if (GetItemTypeSize(value.ItemType) > 4) { throw new ArgumentException("Invalid range value type"); } - _hContainer = MemoryManager.Instance.MemAllocate((uint)Marshal.SizeOf(value)); + _hContainer = MemoryManager.Instance.Allocate((uint)Marshal.SizeOf(value)); if (_hContainer != IntPtr.Zero) { Marshal.StructureToPtr(value, _hContainer, false); @@ -1125,7 +1125,7 @@ namespace NTwain.Data { if (_hContainer != IntPtr.Zero) { - MemoryManager.Instance.MemFree(_hContainer); + MemoryManager.Instance.Free(_hContainer); _hContainer = IntPtr.Zero; } GC.SuppressFinalize(this); @@ -1138,7 +1138,7 @@ namespace NTwain.Data { if (_hContainer != IntPtr.Zero) { - MemoryManager.Instance.MemFree(_hContainer); + MemoryManager.Instance.Free(_hContainer); _hContainer = IntPtr.Zero; } } diff --git a/NTwain/IMemoryManager.cs b/NTwain/IMemoryManager.cs index ae4e745..e055270 100644 --- a/NTwain/IMemoryManager.cs +++ b/NTwain/IMemoryManager.cs @@ -7,29 +7,29 @@ namespace NTwain public interface IMemoryManager { /// - /// Function to allocate memory. Calls to this must be coupled with later. + /// Function to allocate memory. Calls to this must be coupled with later. /// /// The size in bytes. /// Handle to the allocated memory. - IntPtr MemAllocate(uint size); + IntPtr Allocate(uint size); /// /// Function to free memory. /// - /// The handle from . - void MemFree(IntPtr handle); + /// The handle from . + void Free(IntPtr handle); /// - /// Function to lock some memory. Calls to this must be coupled with later. + /// Function to lock some memory. Calls to this must be coupled with later. /// /// The handle to allocated memory. /// Handle to the lock. - IntPtr MemLock(IntPtr handle); + IntPtr Lock(IntPtr handle); /// /// Function to unlock a previously locked memory region. /// - /// The handle from . - void MemUnlock(IntPtr handle); + /// The handle from . + void Unlock(IntPtr handle); } } diff --git a/NTwain/ITwainState.cs b/NTwain/ITwainState.cs index 23a0fc8..6710ef9 100644 --- a/NTwain/ITwainState.cs +++ b/NTwain/ITwainState.cs @@ -8,13 +8,7 @@ namespace NTwain /// Interface for keeping track of current TWAIN state with current app and source ids. /// public interface ITwainState : INotifyPropertyChanged - { - /// - /// Gets the app id used for the session. - /// - /// The app id. - TWIdentity AppId { get; } - + { /// /// Gets the source id used for the session. /// @@ -34,6 +28,12 @@ namespace NTwain /// interface ITwainStateInternal : ITwainState { + /// + /// Gets the app id used for the session. + /// + /// + TWIdentity GetAppId(); + /// /// Gets or sets a value indicating whether calls to triplets will verify the current twain session state. /// diff --git a/NTwain/MemoryManager.cs b/NTwain/MemoryManager.cs index 18f146f..17da4d7 100644 --- a/NTwain/MemoryManager.cs +++ b/NTwain/MemoryManager.cs @@ -32,11 +32,11 @@ namespace NTwain TWEntryPoint _twain2Entry; /// - /// Function to allocate memory. Calls to this must be coupled with later. + /// Function to allocate memory. Calls to this must be coupled with later. /// /// The size in bytes. /// Handle to the allocated memory. - public IntPtr MemAllocate(uint size) + public IntPtr Allocate(uint size) { if (_twain2Entry != null && _twain2Entry.AllocateFunction != null) { @@ -52,8 +52,8 @@ namespace NTwain /// /// Function to free memory. /// - /// The handle from . - public void MemFree(IntPtr handle) + /// The handle from . + public void Free(IntPtr handle) { if (_twain2Entry != null && _twain2Entry.FreeFunction != null) { @@ -66,11 +66,11 @@ namespace NTwain } /// - /// Function to lock some memory. Calls to this must be coupled with later. + /// Function to lock some memory. Calls to this must be coupled with later. /// /// The handle to allocated memory. /// Handle to the lock. - public IntPtr MemLock(IntPtr handle) + public IntPtr Lock(IntPtr handle) { if (_twain2Entry != null && _twain2Entry.LockFunction != null) { @@ -85,8 +85,8 @@ namespace NTwain /// /// Function to unlock a previously locked memory region. /// - /// The handle from . - public void MemUnlock(IntPtr handle) + /// The handle from . + public void Unlock(IntPtr handle) { if (_twain2Entry != null && _twain2Entry.UnlockFunction != null) { diff --git a/NTwain/Triplets/DGAudio/DGAudio.AudioFileXfer.cs b/NTwain/Triplets/DGAudio/DGAudio.AudioFileXfer.cs index e469c6f..c35eeaf 100644 --- a/NTwain/Triplets/DGAudio/DGAudio.AudioFileXfer.cs +++ b/NTwain/Triplets/DGAudio/DGAudio.AudioFileXfer.cs @@ -18,7 +18,7 @@ namespace NTwain.Triplets { Session.VerifyState(6, 6, DataGroups.Audio, DataArgumentType.AudioFileXfer, Message.Get); IntPtr z = IntPtr.Zero; - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, DataGroups.Audio, DataArgumentType.AudioFileXfer, Message.Get, ref z); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, DataGroups.Audio, DataArgumentType.AudioFileXfer, Message.Get, ref z); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGAudio/DGAudio.AudioInfo.cs b/NTwain/Triplets/DGAudio/DGAudio.AudioInfo.cs index 8ceed88..0b6642d 100644 --- a/NTwain/Triplets/DGAudio/DGAudio.AudioInfo.cs +++ b/NTwain/Triplets/DGAudio/DGAudio.AudioInfo.cs @@ -18,7 +18,7 @@ namespace NTwain.Triplets { Session.VerifyState(6, 7, DataGroups.Audio, DataArgumentType.AudioInfo, Message.Get); info = new TWAudioInfo(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, info); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, info); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGAudio/DGAudio.AudioNativeXfer.cs b/NTwain/Triplets/DGAudio/DGAudio.AudioNativeXfer.cs index c8d181e..ede109e 100644 --- a/NTwain/Triplets/DGAudio/DGAudio.AudioNativeXfer.cs +++ b/NTwain/Triplets/DGAudio/DGAudio.AudioNativeXfer.cs @@ -21,7 +21,7 @@ namespace NTwain.Triplets public ReturnCode Get(ref IntPtr handle) { Session.VerifyState(6, 6, DataGroups.Audio, DataArgumentType.AudioNativeXfer, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, DataGroups.Audio, DataArgumentType.AudioNativeXfer, Message.Get, ref handle); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, DataGroups.Audio, DataArgumentType.AudioNativeXfer, Message.Get, ref handle); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.Callback.cs b/NTwain/Triplets/DGControl/DGControl.Callback.cs index c276c70..b186ae0 100644 --- a/NTwain/Triplets/DGControl/DGControl.Callback.cs +++ b/NTwain/Triplets/DGControl/DGControl.Callback.cs @@ -17,7 +17,7 @@ namespace NTwain.Triplets public ReturnCode RegisterCallback(TWCallback callback) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Callback, Message.RegisterCallback); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.RegisterCallback, callback); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.RegisterCallback, callback); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.Callback2.cs b/NTwain/Triplets/DGControl/DGControl.Callback2.cs index 020f128..be1249c 100644 --- a/NTwain/Triplets/DGControl/DGControl.Callback2.cs +++ b/NTwain/Triplets/DGControl/DGControl.Callback2.cs @@ -17,7 +17,7 @@ namespace NTwain.Triplets public ReturnCode RegisterCallback(TWCallback2 callback) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Callback, Message.RegisterCallback); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.RegisterCallback, callback); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.RegisterCallback, callback); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.Capability.cs b/NTwain/Triplets/DGControl/DGControl.Capability.cs index 011e558..ee36545 100644 --- a/NTwain/Triplets/DGControl/DGControl.Capability.cs +++ b/NTwain/Triplets/DGControl/DGControl.Capability.cs @@ -18,7 +18,7 @@ namespace NTwain.Triplets public ReturnCode Get(TWCapability capability) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.Capability, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, capability); } /// @@ -29,7 +29,7 @@ namespace NTwain.Triplets public ReturnCode GetCurrent(TWCapability capability) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.Capability, Message.GetCurrent); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetCurrent, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetCurrent, capability); } /// @@ -40,7 +40,7 @@ namespace NTwain.Triplets public ReturnCode GetDefault(TWCapability capability) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.Capability, Message.GetDefault); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetDefault, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetDefault, capability); } /// @@ -52,7 +52,7 @@ namespace NTwain.Triplets public ReturnCode GetHelp(TWCapability capability) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Capability, Message.GetHelp); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetHelp, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetHelp, capability); } /// @@ -64,7 +64,7 @@ namespace NTwain.Triplets public ReturnCode GetLabel(TWCapability capability) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Capability, Message.GetLabel); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetLabel, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetLabel, capability); } /// @@ -76,7 +76,7 @@ namespace NTwain.Triplets public ReturnCode GetLabelEnum(TWCapability capability) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Capability, Message.GetLabelEnum); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetLabelEnum, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetLabelEnum, capability); } /// @@ -87,7 +87,7 @@ namespace NTwain.Triplets public ReturnCode QuerySupport(TWCapability capability) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.Capability, Message.QuerySupport); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.QuerySupport, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.QuerySupport, capability); } /// @@ -99,7 +99,7 @@ namespace NTwain.Triplets public ReturnCode Reset(TWCapability capability) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Capability, Message.Reset); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, capability); } /// @@ -111,7 +111,7 @@ namespace NTwain.Triplets public ReturnCode ResetAll(TWCapability capability) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Capability, Message.ResetAll); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.ResetAll, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.ResetAll, capability); } /// @@ -126,7 +126,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWCapability capability) { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.Capability, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, capability); } /// @@ -140,7 +140,7 @@ namespace NTwain.Triplets public ReturnCode SetConstraint(TWCapability capability) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.Capability, Message.SetConstraint); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.SetConstraint, capability); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.SetConstraint, capability); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.CustomDSData.cs b/NTwain/Triplets/DGControl/DGControl.CustomDSData.cs index df25f93..971e12c 100644 --- a/NTwain/Triplets/DGControl/DGControl.CustomDSData.cs +++ b/NTwain/Triplets/DGControl/DGControl.CustomDSData.cs @@ -21,7 +21,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.CustomDSData, Message.Get); customData = new TWCustomDSData(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, customData); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, customData); } /// @@ -34,7 +34,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWCustomDSData customData) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.CustomDSData, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, customData); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, customData); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.DeviceEvent.cs b/NTwain/Triplets/DGControl/DGControl.DeviceEvent.cs index bf07099..74217d9 100644 --- a/NTwain/Triplets/DGControl/DGControl.DeviceEvent.cs +++ b/NTwain/Triplets/DGControl/DGControl.DeviceEvent.cs @@ -14,7 +14,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.DeviceEvent, Message.Get); sourceDeviceEvent = new TWDeviceEvent(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, sourceDeviceEvent); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, sourceDeviceEvent); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.EntryPoint.cs b/NTwain/Triplets/DGControl/DGControl.EntryPoint.cs index e23946e..c533d2c 100644 --- a/NTwain/Triplets/DGControl/DGControl.EntryPoint.cs +++ b/NTwain/Triplets/DGControl/DGControl.EntryPoint.cs @@ -19,7 +19,7 @@ namespace NTwain.Triplets { Session.VerifyState(3, 3, DataGroups.Control, DataArgumentType.EntryPoint, Message.Get); entryPoint = new TWEntryPoint(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, entryPoint); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, entryPoint); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.Event.cs b/NTwain/Triplets/DGControl/DGControl.Event.cs index 4143958..61bd0d4 100644 --- a/NTwain/Triplets/DGControl/DGControl.Event.cs +++ b/NTwain/Triplets/DGControl/DGControl.Event.cs @@ -23,7 +23,7 @@ namespace NTwain.Triplets public ReturnCode ProcessEvent(TWEvent theEvent) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.Event, Message.ProcessEvent); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.ProcessEvent, theEvent); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.ProcessEvent, theEvent); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.FileSystem.cs b/NTwain/Triplets/DGControl/DGControl.FileSystem.cs index ddbc140..bd62400 100644 --- a/NTwain/Triplets/DGControl/DGControl.FileSystem.cs +++ b/NTwain/Triplets/DGControl/DGControl.FileSystem.cs @@ -22,7 +22,7 @@ namespace NTwain.Triplets public ReturnCode AutomaticCaptureDirectory(TWFileSystem fileSystem) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.FileSystem, Message.AutomaticCaptureDirectory); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.AutomaticCaptureDirectory, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.AutomaticCaptureDirectory, fileSystem); } /// @@ -37,7 +37,7 @@ namespace NTwain.Triplets public ReturnCode ChangeDirectory(TWFileSystem fileSystem) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.FileSystem, Message.ChangeDirectory); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.ChangeDirectory, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.ChangeDirectory, fileSystem); } /// @@ -53,7 +53,7 @@ namespace NTwain.Triplets public ReturnCode Copy(TWFileSystem fileSystem) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.FileSystem, Message.Copy); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Copy, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Copy, fileSystem); } /// @@ -65,7 +65,7 @@ namespace NTwain.Triplets public ReturnCode CreateDirectory(TWFileSystem fileSystem) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.FileSystem, Message.CreateDirectory); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.CreateDirectory, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.CreateDirectory, fileSystem); } /// @@ -78,7 +78,7 @@ namespace NTwain.Triplets public ReturnCode Delete(TWFileSystem fileSystem) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.FileSystem, Message.Delete); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Delete, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Delete, fileSystem); } /// @@ -90,7 +90,7 @@ namespace NTwain.Triplets public ReturnCode FormatMedia(TWFileSystem fileSystem) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.FileSystem, Message.FormatMedia); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.FormatMedia, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.FormatMedia, fileSystem); } /// @@ -103,7 +103,7 @@ namespace NTwain.Triplets public ReturnCode GetClose(TWFileSystem fileSystem) { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.FileSystem, Message.GetClose); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetClose, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetClose, fileSystem); } /// @@ -115,7 +115,7 @@ namespace NTwain.Triplets public ReturnCode GetFirstFile(TWFileSystem fileSystem) { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.FileSystem, Message.GetFirstFile); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetFirstFile, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetFirstFile, fileSystem); } /// @@ -128,7 +128,7 @@ namespace NTwain.Triplets public ReturnCode GetInfo(TWFileSystem fileSystem) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.FileSystem, Message.GetInfo); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetInfo, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetInfo, fileSystem); } /// @@ -140,7 +140,7 @@ namespace NTwain.Triplets public ReturnCode GetNextFile(TWFileSystem fileSystem) { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.FileSystem, Message.GetNextFile); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetNextFile, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetNextFile, fileSystem); } /// @@ -156,7 +156,7 @@ namespace NTwain.Triplets public ReturnCode Rename(TWFileSystem fileSystem) { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.FileSystem, Message.Rename); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Rename, fileSystem); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Rename, fileSystem); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.Identity.cs b/NTwain/Triplets/DGControl/DGControl.Identity.cs index c702725..5cc1fac 100644 --- a/NTwain/Triplets/DGControl/DGControl.Identity.cs +++ b/NTwain/Triplets/DGControl/DGControl.Identity.cs @@ -20,7 +20,7 @@ namespace NTwain.Triplets internal ReturnCode CloseDS() { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.Identity, Message.CloseDS); - var rc = PInvoke.DsmEntry(Session.AppId, Message.CloseDS, Session.SourceId); + var rc = PInvoke.DsmEntry(Session.GetAppId(), Message.CloseDS, Session.SourceId); if (rc == ReturnCode.Success) { Session.ChangeSourceId(null); @@ -38,7 +38,7 @@ namespace NTwain.Triplets { Session.VerifyState(3, 7, DataGroups.Control, DataArgumentType.Identity, Message.GetDefault); source = new TWIdentity(); - return PInvoke.DsmEntry(Session.AppId, Message.GetDefault, source); + return PInvoke.DsmEntry(Session.GetAppId(), Message.GetDefault, source); } @@ -52,7 +52,7 @@ namespace NTwain.Triplets { Session.VerifyState(3, 7, DataGroups.Control, DataArgumentType.Identity, Message.GetFirst); source = new TWIdentity(); - return PInvoke.DsmEntry(Session.AppId, Message.GetFirst, source); + return PInvoke.DsmEntry(Session.GetAppId(), Message.GetFirst, source); } /// @@ -65,7 +65,7 @@ namespace NTwain.Triplets { Session.VerifyState(3, 7, DataGroups.Control, DataArgumentType.Identity, Message.GetNext); source = new TWIdentity(); - return PInvoke.DsmEntry(Session.AppId, Message.GetNext, source); + return PInvoke.DsmEntry(Session.GetAppId(), Message.GetNext, source); } /// @@ -76,7 +76,7 @@ namespace NTwain.Triplets internal ReturnCode OpenDS(TWIdentity source) { Session.VerifyState(3, 3, DataGroups.Control, DataArgumentType.Identity, Message.OpenDS); - var rc = PInvoke.DsmEntry(Session.AppId, Message.OpenDS, source); + var rc = PInvoke.DsmEntry(Session.GetAppId(), Message.OpenDS, source); if (rc == ReturnCode.Success) { Session.ChangeSourceId(source); @@ -95,7 +95,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWIdentity source) { Session.VerifyState(3, 3, DataGroups.Control, DataArgumentType.Identity, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Message.Set, source); + return PInvoke.DsmEntry(Session.GetAppId(), Message.Set, source); } /// @@ -108,7 +108,7 @@ namespace NTwain.Triplets { Session.VerifyState(3, 7, DataGroups.Control, DataArgumentType.Identity, Message.UserSelect); source = new TWIdentity(); - return PInvoke.DsmEntry(Session.AppId, Message.UserSelect, source); + return PInvoke.DsmEntry(Session.GetAppId(), Message.UserSelect, source); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.Parent.cs b/NTwain/Triplets/DGControl/DGControl.Parent.cs index ae5e53c..339acef 100644 --- a/NTwain/Triplets/DGControl/DGControl.Parent.cs +++ b/NTwain/Triplets/DGControl/DGControl.Parent.cs @@ -20,10 +20,10 @@ namespace NTwain.Triplets /// The handle. On Windows = points to the window handle (hWnd) that will act as the Source’s /// "parent". On Macintosh = should be a NULL value. /// - public ReturnCode CloseDsm(ref IntPtr handle) + public ReturnCode CloseDsm(IntPtr handle) { Session.VerifyState(3, 3, DataGroups.Control, DataArgumentType.Parent, Message.CloseDsm); - var rc = PInvoke.DsmEntry(Session.AppId, null, DataGroups.Control, DataArgumentType.Parent, Message.CloseDsm, ref handle); + var rc = PInvoke.DsmEntry(Session.GetAppId(), null, DataGroups.Control, DataArgumentType.Parent, Message.CloseDsm, ref handle); if (rc == ReturnCode.Success) { Session.ChangeState(2, true); @@ -38,10 +38,10 @@ namespace NTwain.Triplets /// The handle. On Windows = points to the window handle (hWnd) that will act as the Source’s /// "parent". On Macintosh = should be a NULL value. /// - public ReturnCode OpenDsm(ref IntPtr handle) + public ReturnCode OpenDsm(IntPtr handle) { Session.VerifyState(1, 2, DataGroups.Control, DataArgumentType.Parent, Message.OpenDsm); - var rc = PInvoke.DsmEntry(Session.AppId, null, DataGroups.Control, DataArgumentType.Parent, Message.OpenDsm, ref handle); + var rc = PInvoke.DsmEntry(Session.GetAppId(), null, DataGroups.Control, DataArgumentType.Parent, Message.OpenDsm, ref handle); if (rc == ReturnCode.Success) { Session.ChangeState(3, true); diff --git a/NTwain/Triplets/DGControl/DGControl.PassThru.cs b/NTwain/Triplets/DGControl/DGControl.PassThru.cs index 39e2ba8..7351af4 100644 --- a/NTwain/Triplets/DGControl/DGControl.PassThru.cs +++ b/NTwain/Triplets/DGControl/DGControl.PassThru.cs @@ -19,7 +19,7 @@ namespace NTwain.Triplets public ReturnCode PassThrough(TWPassThru sourcePassThru) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.PassThru, Message.PassThru); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.PassThru, sourcePassThru); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.PassThru, sourcePassThru); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.PendingXfers.cs b/NTwain/Triplets/DGControl/DGControl.PendingXfers.cs index 18e85ce..760a770 100644 --- a/NTwain/Triplets/DGControl/DGControl.PendingXfers.cs +++ b/NTwain/Triplets/DGControl/DGControl.PendingXfers.cs @@ -21,7 +21,7 @@ namespace NTwain.Triplets internal ReturnCode EndXfer(TWPendingXfers pendingXfers) { Session.VerifyState(6, 7, DataGroups.Control, DataArgumentType.PendingXfers, Message.EndXfer); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.EndXfer, pendingXfers); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.EndXfer, pendingXfers); } /// @@ -35,7 +35,7 @@ namespace NTwain.Triplets public ReturnCode Get(TWPendingXfers pendingXfers) { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.PendingXfers, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, pendingXfers); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, pendingXfers); } /// @@ -46,7 +46,7 @@ namespace NTwain.Triplets internal ReturnCode Reset(TWPendingXfers pendingXfers) { Session.VerifyState(6, 6, DataGroups.Control, DataArgumentType.PendingXfers, Message.Reset); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, pendingXfers); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, pendingXfers); } /// @@ -58,7 +58,7 @@ namespace NTwain.Triplets public ReturnCode StopFeeder(TWPendingXfers pendingXfers) { Session.VerifyState(6, 6, DataGroups.Control, DataArgumentType.PendingXfers, Message.StopFeeder); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.StopFeeder, pendingXfers); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.StopFeeder, pendingXfers); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.SetupFileXfer.cs b/NTwain/Triplets/DGControl/DGControl.SetupFileXfer.cs index 643244b..14efb2b 100644 --- a/NTwain/Triplets/DGControl/DGControl.SetupFileXfer.cs +++ b/NTwain/Triplets/DGControl/DGControl.SetupFileXfer.cs @@ -20,7 +20,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.SetupFileXfer, Message.Get); setupFileXfer = new TWSetupFileXfer(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, setupFileXfer); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, setupFileXfer); } /// @@ -32,7 +32,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.SetupFileXfer, Message.GetDefault); setupFileXfer = new TWSetupFileXfer(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetDefault, setupFileXfer); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetDefault, setupFileXfer); } /// @@ -45,7 +45,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.SetupFileXfer, Message.Reset); setupFileXfer = new TWSetupFileXfer(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, setupFileXfer); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, setupFileXfer); } /// @@ -60,7 +60,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWSetupFileXfer setupFileXfer) { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.SetupFileXfer, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, setupFileXfer); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, setupFileXfer); } } diff --git a/NTwain/Triplets/DGControl/DGControl.SetupMemXfer.cs b/NTwain/Triplets/DGControl/DGControl.SetupMemXfer.cs index e19bbc9..7fc865a 100644 --- a/NTwain/Triplets/DGControl/DGControl.SetupMemXfer.cs +++ b/NTwain/Triplets/DGControl/DGControl.SetupMemXfer.cs @@ -20,7 +20,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.SetupMemXfer, Message.Get); setupMemXfer = new TWSetupMemXfer(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, setupMemXfer); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, setupMemXfer); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.Status.cs b/NTwain/Triplets/DGControl/DGControl.Status.cs index 94b087c..adee49d 100644 --- a/NTwain/Triplets/DGControl/DGControl.Status.cs +++ b/NTwain/Triplets/DGControl/DGControl.Status.cs @@ -19,7 +19,7 @@ namespace NTwain.Triplets { Session.VerifyState(2, 7, DataGroups.Control, DataArgumentType.Status, Message.Get); status = new TWStatus(); - return PInvoke.DsmEntry(Session.AppId, null, Message.Get, status); + return PInvoke.DsmEntry(Session.GetAppId(), null, Message.Get, status); } /// @@ -31,7 +31,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 7, DataGroups.Control, DataArgumentType.Status, Message.Get); status = new TWStatus(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, status); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, status); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.StatusUtf8.cs b/NTwain/Triplets/DGControl/DGControl.StatusUtf8.cs index 04c2c73..4e86cb2 100644 --- a/NTwain/Triplets/DGControl/DGControl.StatusUtf8.cs +++ b/NTwain/Triplets/DGControl/DGControl.StatusUtf8.cs @@ -19,7 +19,7 @@ namespace NTwain.Triplets public ReturnCode Get(TWStatusUtf8 status) { Session.VerifyState(3, 7, DataGroups.Control, DataArgumentType.StatusUtf8, Message.Get); - return PInvoke.DsmEntry(Session.AppId, null, Message.Get, status); + return PInvoke.DsmEntry(Session.GetAppId(), null, Message.Get, status); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGControl/DGControl.UserInterface.cs b/NTwain/Triplets/DGControl/DGControl.UserInterface.cs index 9360f05..cdc70ab 100644 --- a/NTwain/Triplets/DGControl/DGControl.UserInterface.cs +++ b/NTwain/Triplets/DGControl/DGControl.UserInterface.cs @@ -23,7 +23,7 @@ namespace NTwain.Triplets public ReturnCode DisableDS(TWUserInterface userInterface) { Session.VerifyState(5, 5, DataGroups.Control, DataArgumentType.UserInterface, Message.DisableDS); - var rc = PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.DisableDS, userInterface); + var rc = PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.DisableDS, userInterface); if (rc == ReturnCode.Success) { Session.ChangeState(4, true); @@ -41,7 +41,7 @@ namespace NTwain.Triplets Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.UserInterface, Message.EnableDS); using (var pending = Session.GetPendingStateChanger(5)) { - var rc = PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.EnableDS, userInterface); + var rc = PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.EnableDS, userInterface); if (rc == ReturnCode.Success || (!userInterface.ShowUI && rc == ReturnCode.CheckStatus)) { @@ -63,7 +63,7 @@ namespace NTwain.Triplets Session.VerifyState(4, 4, DataGroups.Control, DataArgumentType.UserInterface, Message.EnableDSUIOnly); using (var pending = Session.GetPendingStateChanger(5)) { - var rc = PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.EnableDSUIOnly, userInterface); + var rc = PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.EnableDSUIOnly, userInterface); if (rc == ReturnCode.Success) { pending.Commit(); diff --git a/NTwain/Triplets/DGControl/DGControl.XferGroup.cs b/NTwain/Triplets/DGControl/DGControl.XferGroup.cs index b2cf210..f3a0e11 100644 --- a/NTwain/Triplets/DGControl/DGControl.XferGroup.cs +++ b/NTwain/Triplets/DGControl/DGControl.XferGroup.cs @@ -20,7 +20,7 @@ namespace NTwain.Triplets public ReturnCode Get(ref uint value) { Session.VerifyState(4, 6, DataGroups.Control, DataArgumentType.XferGroup, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, DataGroups.Control, DataArgumentType.XferGroup, Message.Get, ref value); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, DataGroups.Control, DataArgumentType.XferGroup, Message.Get, ref value); } /// @@ -33,7 +33,7 @@ namespace NTwain.Triplets public ReturnCode Set(uint value) { Session.VerifyState(6, 6, DataGroups.Control, DataArgumentType.XferGroup, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, DataGroups.Control, DataArgumentType.XferGroup, Message.Set, ref value); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, DataGroups.Control, DataArgumentType.XferGroup, Message.Set, ref value); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.CieColor.cs b/NTwain/Triplets/DGImage/DGImage.CieColor.cs index 964390c..4e2e303 100644 --- a/NTwain/Triplets/DGImage/DGImage.CieColor.cs +++ b/NTwain/Triplets/DGImage/DGImage.CieColor.cs @@ -21,7 +21,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.CieColor, Message.Get); cieColor = new TWCieColor(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, cieColor); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, cieColor); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.ExtImageInfo.cs b/NTwain/Triplets/DGImage/DGImage.ExtImageInfo.cs index e5b25dc..bffedb4 100644 --- a/NTwain/Triplets/DGImage/DGImage.ExtImageInfo.cs +++ b/NTwain/Triplets/DGImage/DGImage.ExtImageInfo.cs @@ -14,7 +14,7 @@ namespace NTwain.Triplets public ReturnCode Get(TWExtImageInfo info) { Session.VerifyState(7, 7, DataGroups.Image, DataArgumentType.ExtImageInfo, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, info); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, info); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.Filter.cs b/NTwain/Triplets/DGImage/DGImage.Filter.cs index e5bdfa6..caf54ea 100644 --- a/NTwain/Triplets/DGImage/DGImage.Filter.cs +++ b/NTwain/Triplets/DGImage/DGImage.Filter.cs @@ -22,7 +22,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.Filter, Message.Get); filter = new TWFilter(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, filter); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, filter); } /// @@ -34,7 +34,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.Filter, Message.GetDefault); filter = new TWFilter(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetDefault, filter); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetDefault, filter); } /// @@ -46,7 +46,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWFilter filter) { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.Filter, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, filter); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, filter); } /// @@ -58,7 +58,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.Filter, Message.Reset); filter = new TWFilter(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, filter); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, filter); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.GrayResponse.cs b/NTwain/Triplets/DGImage/DGImage.GrayResponse.cs index 031c7aa..46352a9 100644 --- a/NTwain/Triplets/DGImage/DGImage.GrayResponse.cs +++ b/NTwain/Triplets/DGImage/DGImage.GrayResponse.cs @@ -21,7 +21,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.GrayResponse, Message.Reset); response = new TWGrayResponse(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, response); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, response); } /// @@ -33,7 +33,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWGrayResponse response) { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.GrayResponse, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, response); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, response); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.IccProfile.cs b/NTwain/Triplets/DGImage/DGImage.IccProfile.cs index aacbcc7..88c6041 100644 --- a/NTwain/Triplets/DGImage/DGImage.IccProfile.cs +++ b/NTwain/Triplets/DGImage/DGImage.IccProfile.cs @@ -21,7 +21,7 @@ namespace NTwain.Triplets { Session.VerifyState(6, 7, DataGroups.Image, DataArgumentType.IccProfile, Message.Get); profile = new TWMemory(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, DataArgumentType.IccProfile, Message.Get, ref profile); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, DataArgumentType.IccProfile, Message.Get, ref profile); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.ImageFileXfer.cs b/NTwain/Triplets/DGImage/DGImage.ImageFileXfer.cs index c5e35e5..aed567f 100644 --- a/NTwain/Triplets/DGImage/DGImage.ImageFileXfer.cs +++ b/NTwain/Triplets/DGImage/DGImage.ImageFileXfer.cs @@ -20,7 +20,7 @@ namespace NTwain.Triplets { Session.VerifyState(6, 6, DataGroups.Image, DataArgumentType.ImageFileXfer, Message.Get); IntPtr z = IntPtr.Zero; - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, DataGroups.Image, DataArgumentType.ImageFileXfer, Message.Get, ref z); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, DataGroups.Image, DataArgumentType.ImageFileXfer, Message.Get, ref z); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.ImageInfo.cs b/NTwain/Triplets/DGImage/DGImage.ImageInfo.cs index 2d1e09d..dab8fda 100644 --- a/NTwain/Triplets/DGImage/DGImage.ImageInfo.cs +++ b/NTwain/Triplets/DGImage/DGImage.ImageInfo.cs @@ -15,7 +15,7 @@ namespace NTwain.Triplets { Session.VerifyState(6, 7, DataGroups.Image, DataArgumentType.ImageInfo, Message.Get); info = new TWImageInfo(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, info); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, info); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.ImageLayout.cs b/NTwain/Triplets/DGImage/DGImage.ImageLayout.cs index 1607b06..c3804dd 100644 --- a/NTwain/Triplets/DGImage/DGImage.ImageLayout.cs +++ b/NTwain/Triplets/DGImage/DGImage.ImageLayout.cs @@ -15,27 +15,27 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.ImageLayout, Message.Get); layout = new TWImageLayout(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, layout); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, layout); } public ReturnCode GetDefault(out TWImageLayout layout) { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.ImageLayout, Message.GetDefault); layout = new TWImageLayout(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetDefault, layout); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetDefault, layout); } public ReturnCode Reset(out TWImageLayout layout) { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.ImageLayout, Message.Reset); layout = new TWImageLayout(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, layout); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, layout); } public ReturnCode Set(TWImageLayout layout) { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.ImageLayout, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, layout); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, layout); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.ImageMemFileXfer.cs b/NTwain/Triplets/DGImage/DGImage.ImageMemFileXfer.cs index 3634a03..120c77d 100644 --- a/NTwain/Triplets/DGImage/DGImage.ImageMemFileXfer.cs +++ b/NTwain/Triplets/DGImage/DGImage.ImageMemFileXfer.cs @@ -17,7 +17,7 @@ namespace NTwain.Triplets public ReturnCode Get(TWImageMemXfer xfer) { Session.VerifyState(6, 6, DataGroups.Image, DataArgumentType.ImageMemFileXfer, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, xfer); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, xfer); } } diff --git a/NTwain/Triplets/DGImage/DGImage.ImageMemXfer.cs b/NTwain/Triplets/DGImage/DGImage.ImageMemXfer.cs index 05e1779..094c025 100644 --- a/NTwain/Triplets/DGImage/DGImage.ImageMemXfer.cs +++ b/NTwain/Triplets/DGImage/DGImage.ImageMemXfer.cs @@ -17,7 +17,7 @@ namespace NTwain.Triplets public ReturnCode Get(TWImageMemXfer xfer) { Session.VerifyState(6, 7, DataGroups.Image, DataArgumentType.ImageMemXfer, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, xfer); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, xfer); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.ImageNativeXfer.cs b/NTwain/Triplets/DGImage/DGImage.ImageNativeXfer.cs index efefe8d..552d15e 100644 --- a/NTwain/Triplets/DGImage/DGImage.ImageNativeXfer.cs +++ b/NTwain/Triplets/DGImage/DGImage.ImageNativeXfer.cs @@ -20,7 +20,7 @@ namespace NTwain.Triplets public ReturnCode Get(ref IntPtr handle) { Session.VerifyState(6, 6, DataGroups.Image, DataArgumentType.ImageNativeXfer, Message.Get); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, DataGroups.Image, DataArgumentType.ImageNativeXfer, Message.Get, ref handle); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, DataGroups.Image, DataArgumentType.ImageNativeXfer, Message.Get, ref handle); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.JpegCompression.cs b/NTwain/Triplets/DGImage/DGImage.JpegCompression.cs index 421bc09..bf0a8a2 100644 --- a/NTwain/Triplets/DGImage/DGImage.JpegCompression.cs +++ b/NTwain/Triplets/DGImage/DGImage.JpegCompression.cs @@ -21,7 +21,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.JpegCompression, Message.Get); compression = new TWJpegCompression(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, compression); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, compression); } /// @@ -34,7 +34,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.JpegCompression, Message.GetDefault); compression = new TWJpegCompression(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetDefault, compression); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetDefault, compression); } /// @@ -46,7 +46,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.JpegCompression, Message.Reset); compression = new TWJpegCompression(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, compression); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, compression); } /// @@ -59,7 +59,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWJpegCompression compression) { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.JpegCompression, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, compression); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, compression); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.Palette8.cs b/NTwain/Triplets/DGImage/DGImage.Palette8.cs index aa38a58..4d87dbb 100644 --- a/NTwain/Triplets/DGImage/DGImage.Palette8.cs +++ b/NTwain/Triplets/DGImage/DGImage.Palette8.cs @@ -20,7 +20,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.Palette8, Message.Get); palette = new TWPalette8(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Get, palette); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Get, palette); } /// @@ -32,7 +32,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 6, DataGroups.Image, DataArgumentType.Palette8, Message.GetDefault); palette = new TWPalette8(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.GetDefault, palette); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.GetDefault, palette); } /// @@ -45,7 +45,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.Palette8, Message.Reset); palette = new TWPalette8(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, palette); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, palette); } /// @@ -59,7 +59,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWPalette8 palette) { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.Palette8, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, palette); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, palette); } } } \ No newline at end of file diff --git a/NTwain/Triplets/DGImage/DGImage.RgbResponse.cs b/NTwain/Triplets/DGImage/DGImage.RgbResponse.cs index 63a0947..5370aea 100644 --- a/NTwain/Triplets/DGImage/DGImage.RgbResponse.cs +++ b/NTwain/Triplets/DGImage/DGImage.RgbResponse.cs @@ -22,7 +22,7 @@ namespace NTwain.Triplets { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.RgbResponse, Message.Reset); response = new TWRgbResponse(); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Reset, response); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Reset, response); } /// @@ -34,7 +34,7 @@ namespace NTwain.Triplets public ReturnCode Set(TWRgbResponse response) { Session.VerifyState(4, 4, DataGroups.Image, DataArgumentType.RgbResponse, Message.Set); - return PInvoke.DsmEntry(Session.AppId, Session.SourceId, Message.Set, response); + return PInvoke.DsmEntry(Session.GetAppId(), Session.SourceId, Message.Set, response); } } } \ No newline at end of file diff --git a/NTwain/TwainSession.cs b/NTwain/TwainSession.cs index 0f22a8e..f81c59e 100644 --- a/NTwain/TwainSession.cs +++ b/NTwain/TwainSession.cs @@ -29,7 +29,7 @@ namespace NTwain public TwainSession(TWIdentity appId) { if (appId == null) { throw new ArgumentNullException("appId"); } - AppId = appId; + _appId = appId; State = 1; EnforceState = true; } @@ -38,13 +38,14 @@ namespace NTwain object _callbackObj; SynchronizationContext _syncer; + TWIdentity _appId; /// /// Gets the app id used for the session. /// /// The app id. - public TWIdentity AppId { get; private set; } + TWIdentity ITwainStateInternal.GetAppId() { return _appId; } /// /// Gets the source id used for the session. @@ -177,12 +178,11 @@ namespace NTwain Debug.WriteLine(string.Format("Thread {0}: OpenManager.", Thread.CurrentThread.ManagedThreadId)); _parentHandle = handle; - var hand = handle.Handle; - var rc = DGControl.Parent.OpenDsm(ref hand); + var rc = DGControl.Parent.OpenDsm(handle.Handle); if (rc == ReturnCode.Success) { // if twain2 then get mem management stuff - if ((AppId.DataFunctionalities & DataFunctionalities.Dsm2) == DataFunctionalities.Dsm2) + if ((_appId.DataFunctionalities & DataFunctionalities.Dsm2) == DataFunctionalities.Dsm2) { TWEntryPoint entry; rc = DGControl.EntryPoint.Get(out entry); @@ -208,8 +208,7 @@ namespace NTwain { Debug.WriteLine(string.Format("Thread {0}: CloseManager.", Thread.CurrentThread.ManagedThreadId)); - var hand = _parentHandle.Handle; - var rc = DGControl.Parent.CloseDsm(ref hand); + var rc = DGControl.Parent.CloseDsm(_parentHandle.Handle); if (rc == ReturnCode.Success) { _parentHandle = default(HandleRef); @@ -244,7 +243,6 @@ namespace NTwain var rc = DGControl.Identity.OpenDS(sourceId); if (rc == ReturnCode.Success) { - SourceId = sourceId; SupportedCaps = this.GetCapabilities(); // TODO: does it work? @@ -253,9 +251,9 @@ namespace NTwain if (!DisableCallback) { // app v2.2 or higher uses callback2 - if (AppId.ProtocolMajor >= 2 && AppId.ProtocolMinor >= 2) + if (_appId.ProtocolMajor >= 2 && _appId.ProtocolMinor >= 2) { - var cb = new TWCallback2(new CallbackDelegate(CallbackHandler)); + var cb = new TWCallback2(CallbackHandler); var rc2 = DGControl.Callback2.RegisterCallback(cb); if (rc2 == ReturnCode.Success) @@ -266,7 +264,7 @@ namespace NTwain } else { - var cb = new TWCallback(new CallbackDelegate(CallbackHandler)); + var cb = new TWCallback(CallbackHandler); var rc2 = DGControl.Callback.RegisterCallback(cb); @@ -319,7 +317,6 @@ namespace NTwain var rc = DGControl.Identity.CloseDS(); if (rc == ReturnCode.Success) { - SourceId = null; _callbackObj = null; SupportedCaps = null; } @@ -504,7 +501,7 @@ namespace NTwain { if (dataPtr != IntPtr.Zero) { - lockedPtr = MemoryManager.Instance.MemLock(dataPtr); + lockedPtr = MemoryManager.Instance.Lock(dataPtr); } dtHand(this, new DataTransferredEventArgs(lockedPtr, file)); } @@ -537,12 +534,12 @@ namespace NTwain // data here is allocated by source so needs to use shared mem calls if (lockedPtr != IntPtr.Zero) { - MemoryManager.Instance.MemUnlock(lockedPtr); + MemoryManager.Instance.Unlock(lockedPtr); lockedPtr = IntPtr.Zero; } if (dataPtr != IntPtr.Zero) { - MemoryManager.Instance.MemFree(dataPtr); + MemoryManager.Instance.Free(dataPtr); dataPtr = IntPtr.Zero; } } @@ -718,14 +715,14 @@ namespace NTwain try { // no need to lock for marshal alloc - msgPtr = MemoryManager.Instance.MemAllocate((uint)Marshal.SizeOf(winmsg)); + msgPtr = MemoryManager.Instance.Allocate((uint)Marshal.SizeOf(winmsg)); Marshal.StructureToPtr(winmsg, msgPtr, false); return HandleLoopMsgEvent(ref msgPtr); } finally { if (msgPtr != IntPtr.Zero) - MemoryManager.Instance.MemFree(msgPtr); + MemoryManager.Instance.Free(msgPtr); } } return false; @@ -757,14 +754,14 @@ namespace NTwain try { // no need to lock for marshal alloc - msgPtr = MemoryManager.Instance.MemAllocate((uint)Marshal.SizeOf(winmsg)); + msgPtr = MemoryManager.Instance.Allocate((uint)Marshal.SizeOf(winmsg)); Marshal.StructureToPtr(winmsg, msgPtr, false); handled = HandleLoopMsgEvent(ref msgPtr); } finally { if (msgPtr != IntPtr.Zero) - MemoryManager.Instance.MemFree(msgPtr); + MemoryManager.Instance.Free(msgPtr); } } return IntPtr.Zero; diff --git a/NTwain/TwainSessionBase.cs b/NTwain/TwainSessionBase.cs index 329fdd4..6889c28 100644 --- a/NTwain/TwainSessionBase.cs +++ b/NTwain/TwainSessionBase.cs @@ -1,13 +1,20 @@ using NTwain.Data; using NTwain.Triplets; +using NTwain.Values; using System; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Linq; +using System.Runtime.InteropServices; using System.Text; +using System.Threading; namespace NTwain { + /// + /// Base working class for interfacing with TWAIN. + /// public class TwainSessionBase : ITwainStateInternal, ITwainOperationInternal { /// @@ -18,14 +25,25 @@ namespace NTwain public TwainSessionBase(TWIdentity appId) { if (appId == null) { throw new ArgumentNullException("appId"); } - AppId = appId; + _appId = appId; State = 1; EnforceState = true; } + TWIdentity _appId; + HandleRef _appHandle; + SynchronizationContext _syncer; + object _callbackObj; // kept around so it doesn't get gc'ed + TWUserInterface _twui; #region ITwainStateInternal Members + /// + /// Gets the app id used for the session. + /// + /// The app id. + TWIdentity ITwainStateInternal.GetAppId() { return _appId; } + /// /// Gets or sets a value indicating whether calls to triplets will verify the current twain session state. /// @@ -60,14 +78,6 @@ namespace NTwain #region ITwainState Members - /// - /// Gets the app id used for the session. - /// - /// - /// The app id. - /// - public TWIdentity AppId { get; private set; } - /// /// Gets the source id used for the session. /// @@ -129,10 +139,6 @@ namespace NTwain #endregion - #region ITwainOperationInternal Members - - #endregion - #region INotifyPropertyChanged Members /// @@ -152,6 +158,230 @@ namespace NTwain #endregion + #region privileged calls that causes state change in TWAIN + + /// + /// Opens the data source manager. This must be the first method used + /// before using other TWAIN functions. Calls to this must be followed by when done. + /// + /// On Windows = points to the window handle (hWnd) that will act as the Source’s + /// "parent". On Macintosh = should be a NULL value. + /// + public ReturnCode OpenManager(HandleRef appHandle) + { + Debug.WriteLine(string.Format("Thread {0}: OpenManager.", Thread.CurrentThread.ManagedThreadId)); + + var rc = DGControl.Parent.OpenDsm(appHandle.Handle); + if (rc == ReturnCode.Success) + { + _appHandle = appHandle; + // if twain2 then get memory management functions + if ((_appId.DataFunctionalities & DataFunctionalities.Dsm2) == DataFunctionalities.Dsm2) + { + TWEntryPoint entry; + rc = DGControl.EntryPoint.Get(out entry); + if (rc == ReturnCode.Success) + { + MemoryManager.Instance.UpdateEntryPoint(entry); + Debug.WriteLine("Using TWAIN2 memory functions."); + } + else + { + CloseManager(); + } + } + } + return rc; + } + + /// + /// Closes the data source manager. + /// + /// + public ReturnCode CloseManager() + { + Debug.WriteLine(string.Format("Thread {0}: CloseManager.", Thread.CurrentThread.ManagedThreadId)); + + var rc = DGControl.Parent.CloseDsm(_appHandle.Handle); + if (rc == ReturnCode.Success) + { + _appHandle = default(HandleRef); + } + return rc; + } + + /// + /// Loads the specified source into main memory and causes its initialization. + /// Calls to this must be followed by + /// when done. + /// + /// Name of the source. + /// + /// Source name is required.;sourceProductName + public ReturnCode OpenSource(string sourceProductName) + { + if (string.IsNullOrEmpty(sourceProductName)) { throw new ArgumentException("Source name is required.", "sourceProductName"); } + + Debug.WriteLine(string.Format("Thread {0}: OpenSource.", Thread.CurrentThread.ManagedThreadId)); + + var source = new TWIdentity(); + source.ProductName = sourceProductName; + + var rc = DGControl.Identity.OpenDS(source); + if (rc == ReturnCode.Success) + { + } + return rc; + } + + /// + /// When an application is finished with a Source, it must formally close the session between them + /// using this operation. This is necessary in case the Source only supports connection with a single + /// application (many desktop scanners will behave this way). A Source such as this cannot be + /// accessed by other applications until its current session is terminated + /// + /// + public ReturnCode CloseSource() + { + Debug.WriteLine(string.Format("Thread {0}: CloseSource.", Thread.CurrentThread.ManagedThreadId)); + + var rc = DGControl.Identity.CloseDS(); + if (rc == ReturnCode.Success) + { + _callbackObj = null; + } + return rc; + } + + /// + /// Enables the source. + /// + /// The mode. + /// if set to true any driver UI will display as modal. + /// The window handle if modal. + /// The + /// that is required for certain operations. + /// It is recommended you call this method in a UI thread and pass in + /// + /// if you do not have a custom one setup. + /// + /// context + public ReturnCode EnableSource(SourceEnableMode mode, bool modal, HandleRef windowHandle, SynchronizationContext context) + { + if (context == null) { throw new ArgumentNullException("context"); } + Debug.WriteLine(string.Format("Thread {0}: EnableSource.", Thread.CurrentThread.ManagedThreadId)); + + _syncer = context; + + // app v2.2 or higher uses callback2 + if (_appId.ProtocolMajor >= 2 && _appId.ProtocolMinor >= 2) + { + var cb = new TWCallback2(CallbackHandler); + var rc2 = DGControl.Callback2.RegisterCallback(cb); + + if (rc2 == ReturnCode.Success) + { + Debug.WriteLine("Registered callback2 OK."); + _callbackObj = cb; + } + } + else + { + var cb = new TWCallback(CallbackHandler); + + var rc2 = DGControl.Callback.RegisterCallback(cb); + + if (rc2 == ReturnCode.Success) + { + Debug.WriteLine("Registered callback OK."); + _callbackObj = cb; + } + } + + _twui = new TWUserInterface(); + _twui.ShowUI = mode == SourceEnableMode.ShowUI; + _twui.ModalUI = modal; + _twui.hParent = windowHandle.Handle; + + if (mode == SourceEnableMode.ShowUIOnly) + { + return DGControl.UserInterface.EnableDSUIOnly(_twui); + } + else + { + return DGControl.UserInterface.EnableDS(_twui); + } + + } + + /// + /// Disables the source to end data acquisition. + /// + /// + ReturnCode DisableSource() + { + Debug.WriteLine(string.Format("Thread {0}: DisableSource.", Thread.CurrentThread.ManagedThreadId)); + + var rc = DGControl.UserInterface.DisableDS(_twui); + if (rc == ReturnCode.Success) + { + OnSourceDisabled(); + } + return rc; + } + + /// + /// Forces the stepping down of an opened source when things gets out of control. + /// Used when session state and source state become out of sync. + /// + /// State of the target. + public void ForceStepDown(int targetState) + { + Debug.WriteLine(string.Format("Thread {0}: ForceStepDown.", Thread.CurrentThread.ManagedThreadId)); + + bool origFlag = EnforceState; + EnforceState = false; + + // From the twain spec + // Stepping Back Down the States + // DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER → state 7 to 6 + // DG_CONTROL / DAT_PENDINGXFERS / MSG_RESET → state 6 to 5 + // DG_CONTROL / DAT_USERINTERFACE / MSG_DISABLEDS → state 5 to 4 + // DG_CONTROL / DAT_IDENTITY / MSG_CLOSEDS → state 4 to 3 + // Ignore the status returns from the calls prior to the one yielding the desired state. For instance, if a + // call during scanning returns TWCC_SEQERROR and the desire is to return to state 5, then use the + // following commands. + // DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER → state 7 to 6 + // DG_CONTROL / DAT_PENDINGXFERS / MSG_RESET → state 6 to 5 + // Being sure to confirm that DG_CONTROL / DAT_PENDINGXFERS / MSG_RESET returned + // success, the return status from DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER may + // be ignored. + + if (targetState < 7) + { + DGControl.PendingXfers.EndXfer(new TWPendingXfers()); + } + if (targetState < 6) + { + DGControl.PendingXfers.Reset(new TWPendingXfers()); + } + if (targetState < 5) + { + DisableSource(); + } + if (targetState < 4) + { + CloseSource(); + } + if (targetState < 3) + { + CloseManager(); + } + EnforceState = origFlag; + } + + #endregion + #region custom events and overridables /// @@ -164,6 +394,56 @@ namespace NTwain /// protected virtual void OnSourceIdChanged() { } + /// + /// Called when source has been disabled (back to state 4). + /// + protected virtual void OnSourceDisabled() { } + + #endregion + + #region real TWAIN logic during xfer + + void HandleSourceMsg(TWIdentity origin, TWIdentity destination, DataGroups dg, DataArgumentType dat, Message msg, IntPtr data) + { + if (msg != Message.Null) + { + Debug.WriteLine(string.Format("Thread {0}: HandleSourceMsg at state {1} with DG={2} DAT={3} MSG={4}.", Thread.CurrentThread.ManagedThreadId, State, dg, dat, msg)); + } + + + } + + ReturnCode CallbackHandler(TWIdentity origin, TWIdentity destination, DataGroups dg, DataArgumentType dat, Message msg, IntPtr data) + { + if (origin != null && SourceId != null && origin.Id == SourceId.Id) + { + Debug.WriteLine(string.Format("Thread {0}: CallbackHandler at state {1} with DG={2} DAT={3} MSG={4}.", Thread.CurrentThread.ManagedThreadId, State, dg, dat, msg)); + // spec says we must handle this on the thread that enabled the DS, + // but it's usually already the same thread and doesn't work (failure + seqError) w/o jumping to another thread and back. + // My guess is the DS needs to see the Success return code first before letting transfer happen + // so this is an hack to make it happen. + // TODO: find a better method. + ThreadPool.QueueUserWorkItem(o => + { + var ctx = o as SynchronizationContext; + if (ctx != null) + { + _syncer.Send(blah => + { + HandleSourceMsg(origin, destination, dg, dat, msg, data); + }, null); + } + else + { + // no context? better hope for the best! + HandleSourceMsg(origin, destination, dg, dat, msg, data); + } + }, _syncer); + return ReturnCode.Success; + } + return ReturnCode.Failure; + } + #endregion } diff --git a/NTwain/Values/DataValues.cs b/NTwain/Values/DataValues.cs index 6564bbf..48364b2 100644 --- a/NTwain/Values/DataValues.cs +++ b/NTwain/Values/DataValues.cs @@ -1487,7 +1487,7 @@ namespace NTwain.Values None = 0, Dsm2 = 0x10000000, App2 = 0x20000000, - DS2 = 0x40000000, + Ds2 = 0x40000000, } /// diff --git a/Tests/Tester.Winform/TestForm.cs b/Tests/Tester.Winform/TestForm.cs index 47d913e..a6e238c 100644 --- a/Tests/Tester.Winform/TestForm.cs +++ b/Tests/Tester.Winform/TestForm.cs @@ -149,7 +149,7 @@ namespace Tester.Winform var curBtn = (sender as ToolStripMenuItem); var src = curBtn.Tag as TWIdentity; - if (_twain.OpenSource(src) == ReturnCode.Success) + if (_twain.OpenSource(src.ProductName) == ReturnCode.Success) { curBtn.Checked = true; btnStartCapture.Enabled = true;