From bb862bc1071efe4d1ad28eef89e0463276bd8955 Mon Sep 17 00:00:00 2001 From: soukoku Date: Sun, 6 Apr 2014 08:22:59 -0400 Subject: [PATCH] Fixed some CA violations. --- NTwain/Data/TypesExtended.cs | 6 +- NTwain/MemoryManager.cs | 36 ++++------ NTwain/NTwain.csproj | 1 + NTwain/NativeMethods.cs | 30 ++++++++ NTwain/Triplets/PInvoke.Win32.cs | 2 +- NTwain/Triplets/PInvoke.Win64.cs | 2 +- NTwain/Triplets/PInvoke.cs | 120 +++++++++++++++---------------- NTwain/TwainSession.cs | 79 ++++++++++++++++---- NTwain/TwainSessionOld.cs | 12 +--- NTwain/TwainSessionWPF.cs | 6 +- NTwain/TwainSessionWinform.cs | 10 ++- 11 files changed, 182 insertions(+), 122 deletions(-) create mode 100644 NTwain/NativeMethods.cs diff --git a/NTwain/Data/TypesExtended.cs b/NTwain/Data/TypesExtended.cs index 98f051f..25abb27 100644 --- a/NTwain/Data/TypesExtended.cs +++ b/NTwain/Data/TypesExtended.cs @@ -2351,7 +2351,7 @@ namespace NTwain.Data /// uncompressed row in the block to be transferred. An application should /// never allocate a buffer smaller than this. /// - public int MinBufferSize { get { return (int)_minBufSize; } } + public uint MinBufferSize { get { return _minBufSize; } } /// /// The size of the largest transfer buffer, in bytes, that a Source can fill. If a /// Source can fill an arbitrarily large buffer, it might set this field to negative 1 to @@ -2359,7 +2359,7 @@ namespace NTwain.Data /// cord is). Other Sources, such as frame grabbers, cannot fill a buffer larger than /// a certain size. Allocation of a transfer buffer larger than this value is wasteful. /// - public int MaxBufferSize { get { return (int)_maxBufSize; } } + public uint MaxBufferSize { get { return _maxBufSize; } } /// /// The size of the optimum transfer buffer, in bytes. A smart application will /// allocate transfer buffers of this size, if possible. Buffers of this size will @@ -2367,7 +2367,7 @@ namespace NTwain.Data /// reasonable values in this field. Buffers that are 10’s of kbytes will be easier for /// applications to allocate than buffers that are 100’s or 1000’s of kbytes. /// - public int Preferred { get { return (int)_preferred; } } + public uint Preferred { get { return _preferred; } } } /// diff --git a/NTwain/MemoryManager.cs b/NTwain/MemoryManager.cs index 17da4d7..e2e434c 100644 --- a/NTwain/MemoryManager.cs +++ b/NTwain/MemoryManager.cs @@ -38,15 +38,23 @@ namespace NTwain /// Handle to the allocated memory. public IntPtr Allocate(uint size) { + IntPtr retVal = IntPtr.Zero; + if (_twain2Entry != null && _twain2Entry.AllocateFunction != null) { - return _twain2Entry.AllocateFunction(size); + retVal = _twain2Entry.AllocateFunction(size); } else { // 0x0040 is GPTR - return WinGlobalAlloc(0x0040, new UIntPtr(size)); + retVal = NativeMethods.WinGlobalAlloc(0x0040, new UIntPtr(size)); } + + if (retVal == IntPtr.Zero) + { + throw new OutOfMemoryException("Failed to allocate requested memory."); + } + return retVal; } /// @@ -61,7 +69,7 @@ namespace NTwain } else { - WinGlobalFree(handle); + NativeMethods.WinGlobalFree(handle); } } @@ -78,7 +86,7 @@ namespace NTwain } else { - return WinGlobalLock(handle); + return NativeMethods.WinGlobalLock(handle); } } @@ -94,26 +102,8 @@ namespace NTwain } else { - WinGlobalUnlock(handle); + NativeMethods.WinGlobalUnlock(handle); } } - - #region old mem stuff for twain 1.x - - - [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalAlloc")] - static extern IntPtr WinGlobalAlloc(uint uFlags, UIntPtr dwBytes); - - [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalFree")] - static extern IntPtr WinGlobalFree(IntPtr hMem); - - [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalLock")] - static extern IntPtr WinGlobalLock(IntPtr handle); - - [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalUnlock")] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool WinGlobalUnlock(IntPtr handle); - - #endregion } } diff --git a/NTwain/NTwain.csproj b/NTwain/NTwain.csproj index 22bc771..fc7e51d 100644 --- a/NTwain/NTwain.csproj +++ b/NTwain/NTwain.csproj @@ -61,6 +61,7 @@ + True True diff --git a/NTwain/NativeMethods.cs b/NTwain/NativeMethods.cs new file mode 100644 index 0000000..0da84f4 --- /dev/null +++ b/NTwain/NativeMethods.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; + +namespace NTwain +{ + static class NativeMethods + { + // should be unsafe native methods? + + #region mem stuff for twain 1.x + + [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalAlloc")] + internal static extern IntPtr WinGlobalAlloc(uint uFlags, UIntPtr dwBytes); + + [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalFree")] + internal static extern IntPtr WinGlobalFree(IntPtr hMem); + + [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalLock")] + internal static extern IntPtr WinGlobalLock(IntPtr handle); + + [DllImport("kernel32", SetLastError = true, EntryPoint = "GlobalUnlock")] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool WinGlobalUnlock(IntPtr handle); + + #endregion + } +} diff --git a/NTwain/Triplets/PInvoke.Win32.cs b/NTwain/Triplets/PInvoke.Win32.cs index ca0d5b7..52f7b67 100644 --- a/NTwain/Triplets/PInvoke.Win32.cs +++ b/NTwain/Triplets/PInvoke.Win32.cs @@ -7,7 +7,7 @@ namespace NTwain.Triplets { static partial class PInvoke { - static partial class WinNativeMethods + static partial class NativeMethods { [DllImport("twain_32", EntryPoint = "#1")] public static extern ReturnCode DsmEntry32( diff --git a/NTwain/Triplets/PInvoke.Win64.cs b/NTwain/Triplets/PInvoke.Win64.cs index 597567d..0664a5f 100644 --- a/NTwain/Triplets/PInvoke.Win64.cs +++ b/NTwain/Triplets/PInvoke.Win64.cs @@ -7,7 +7,7 @@ namespace NTwain.Triplets { static partial class PInvoke { - static partial class WinNativeMethods + static partial class NativeMethods { [DllImport("twaindsm", EntryPoint = "#1")] public static extern ReturnCode DsmEntry64( diff --git a/NTwain/Triplets/PInvoke.cs b/NTwain/Triplets/PInvoke.cs index cfed7f1..4e44162 100644 --- a/NTwain/Triplets/PInvoke.cs +++ b/NTwain/Triplets/PInvoke.cs @@ -43,8 +43,8 @@ namespace NTwain.Triplets Message msg, ref IntPtr data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, dg, dat, msg, ref data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, dg, dat, msg, ref data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, dg, dat, msg, ref data); } + else { return NativeMethods.DsmEntry32(origin, destination, dg, dat, msg, ref data); } } public static ReturnCode DsmEntry( @@ -55,8 +55,8 @@ namespace NTwain.Triplets Message msg, ref uint data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, dg, dat, msg, ref data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, dg, dat, msg, ref data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, dg, dat, msg, ref data); } + else { return NativeMethods.DsmEntry32(origin, destination, dg, dat, msg, ref data); } } public static ReturnCode DsmEntry( @@ -65,8 +65,8 @@ namespace NTwain.Triplets Message msg, TWAudioInfo data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Audio, DataArgumentType.AudioInfo, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Audio, DataArgumentType.AudioInfo, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Audio, DataArgumentType.AudioInfo, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Audio, DataArgumentType.AudioInfo, msg, data); } } @@ -76,8 +76,8 @@ namespace NTwain.Triplets Message msg, TWCapability data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Capability, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Capability, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Capability, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Capability, msg, data); } } @@ -87,8 +87,8 @@ namespace NTwain.Triplets Message msg, TWCustomDSData data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.CustomDSData, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.CustomDSData, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.CustomDSData, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.CustomDSData, msg, data); } } @@ -98,8 +98,8 @@ namespace NTwain.Triplets Message msg, TWDeviceEvent data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.DeviceEvent, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.DeviceEvent, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.DeviceEvent, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.DeviceEvent, msg, data); } } @@ -109,8 +109,8 @@ namespace NTwain.Triplets Message msg, TWCallback data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } } @@ -120,8 +120,8 @@ namespace NTwain.Triplets Message msg, TWCallback2 data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Callback, msg, data); } } @@ -131,8 +131,8 @@ namespace NTwain.Triplets Message msg, TWEntryPoint data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.EntryPoint, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.EntryPoint, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.EntryPoint, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.EntryPoint, msg, data); } } @@ -142,8 +142,8 @@ namespace NTwain.Triplets Message msg, TWEvent data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Event, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Event, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Event, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Event, msg, data); } } @@ -153,8 +153,8 @@ namespace NTwain.Triplets Message msg, TWFileSystem data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.FileSystem, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.FileSystem, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.FileSystem, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.FileSystem, msg, data); } } public static ReturnCode DsmEntry( @@ -162,8 +162,8 @@ namespace NTwain.Triplets Message msg, TWIdentity data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, IntPtr.Zero, DataGroups.Control, DataArgumentType.Identity, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, IntPtr.Zero, DataGroups.Control, DataArgumentType.Identity, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, IntPtr.Zero, DataGroups.Control, DataArgumentType.Identity, msg, data); } + else { return NativeMethods.DsmEntry32(origin, IntPtr.Zero, DataGroups.Control, DataArgumentType.Identity, msg, data); } } @@ -173,8 +173,8 @@ namespace NTwain.Triplets Message msg, TWPassThru data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.PassThru, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.PassThru, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.PassThru, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.PassThru, msg, data); } } @@ -184,8 +184,8 @@ namespace NTwain.Triplets Message msg, TWPendingXfers data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.PendingXfers, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.PendingXfers, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.PendingXfers, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.PendingXfers, msg, data); } } @@ -195,8 +195,8 @@ namespace NTwain.Triplets Message msg, TWSetupFileXfer data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.SetupFileXfer, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.SetupFileXfer, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.SetupFileXfer, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.SetupFileXfer, msg, data); } } @@ -206,8 +206,8 @@ namespace NTwain.Triplets Message msg, TWSetupMemXfer data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.SetupMemXfer, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.SetupMemXfer, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.SetupMemXfer, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.SetupMemXfer, msg, data); } } @@ -217,8 +217,8 @@ namespace NTwain.Triplets Message msg, TWStatusUtf8 data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.StatusUtf8, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.StatusUtf8, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.StatusUtf8, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.StatusUtf8, msg, data); } } @@ -228,8 +228,8 @@ namespace NTwain.Triplets Message msg, TWUserInterface data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.UserInterface, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.UserInterface, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.UserInterface, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.UserInterface, msg, data); } } @@ -239,8 +239,8 @@ namespace NTwain.Triplets Message msg, TWCieColor data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.CieColor, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.CieColor, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.CieColor, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.CieColor, msg, data); } } @@ -250,8 +250,8 @@ namespace NTwain.Triplets Message msg, TWExtImageInfo data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ExtImageInfo, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ExtImageInfo, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ExtImageInfo, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ExtImageInfo, msg, data); } } public static ReturnCode DsmEntry( @@ -260,8 +260,8 @@ namespace NTwain.Triplets Message msg, TWFilter data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.Filter, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.Filter, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.Filter, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.Filter, msg, data); } } public static ReturnCode DsmEntry( @@ -270,8 +270,8 @@ namespace NTwain.Triplets Message msg, TWGrayResponse data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.GrayResponse, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.GrayResponse, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.GrayResponse, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.GrayResponse, msg, data); } } @@ -281,8 +281,8 @@ namespace NTwain.Triplets Message msg, TWImageInfo data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ImageInfo, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ImageInfo, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ImageInfo, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ImageInfo, msg, data); } } @@ -292,8 +292,8 @@ namespace NTwain.Triplets Message msg, TWImageLayout data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ImageLayout, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ImageLayout, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ImageLayout, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ImageLayout, msg, data); } } @@ -303,8 +303,8 @@ namespace NTwain.Triplets Message msg, TWImageMemXfer data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ImageMemXfer, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ImageMemXfer, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.ImageMemXfer, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.ImageMemXfer, msg, data); } } @@ -314,8 +314,8 @@ namespace NTwain.Triplets Message msg, TWJpegCompression data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.JpegCompression, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.JpegCompression, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.JpegCompression, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.JpegCompression, msg, data); } } @@ -325,8 +325,8 @@ namespace NTwain.Triplets Message msg, TWPalette8 data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.Palette8, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.Palette8, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.Palette8, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.Palette8, msg, data); } } @@ -336,8 +336,8 @@ namespace NTwain.Triplets Message msg, TWRgbResponse data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.RgbResponse, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.RgbResponse, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Image, DataArgumentType.RgbResponse, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Image, DataArgumentType.RgbResponse, msg, data); } } @@ -347,8 +347,8 @@ namespace NTwain.Triplets Message msg, TWStatus data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Status, msg, data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Status, msg, data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, DataArgumentType.Status, msg, data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, DataArgumentType.Status, msg, data); } } @@ -359,8 +359,8 @@ namespace NTwain.Triplets Message msg, ref TWMemory data) { - if (CanUseTwainDSM) { return WinNativeMethods.DsmEntry64(origin, destination, DataGroups.Control, dat, msg, ref data); } - else { return WinNativeMethods.DsmEntry32(origin, destination, DataGroups.Control, dat, msg, ref data); } + if (CanUseTwainDSM) { return NativeMethods.DsmEntry64(origin, destination, DataGroups.Control, dat, msg, ref data); } + else { return NativeMethods.DsmEntry32(origin, destination, DataGroups.Control, dat, msg, ref data); } } diff --git a/NTwain/TwainSession.cs b/NTwain/TwainSession.cs index ea513c4..3b71a3a 100644 --- a/NTwain/TwainSession.cs +++ b/NTwain/TwainSession.cs @@ -27,7 +27,7 @@ namespace NTwain { if (appId == null) { throw new ArgumentNullException("appId"); } _appId = appId; - State = 1; + ((ITwainStateInternal)this).ChangeState(1, false); EnforceState = true; } @@ -912,14 +912,49 @@ namespace NTwain } } - private void DoImageMemoryFileXfer() - { - throw new NotImplementedException(); - } - private void DoImageMemoryXfer() { throw new NotImplementedException(); + + TWSetupMemXfer memInfo; + if (DGControl.SetupMemXfer.Get(out memInfo) == ReturnCode.Success) + { + TWImageMemXfer xferInfo = new TWImageMemXfer(); + try + { + xferInfo.Memory = new TWMemory + { + Length = memInfo.Preferred, + TheMem = MemoryManager.Instance.Allocate(memInfo.Preferred) + }; + + + var xrc = ReturnCode.Success; + do + { + xrc = DGImage.ImageMemXfer.Get(xferInfo); + + if (xrc == ReturnCode.XferDone) + { + + } + } while (xrc == ReturnCode.Success); + + } + finally + { + if (xferInfo.Memory.TheMem != IntPtr.Zero) + { + MemoryManager.Instance.Free(xferInfo.Memory.TheMem); + } + } + } + } + + private void DoImageMemoryFileXfer() + { + // no way to test, not supported by sample source + throw new NotImplementedException(); } #endregion @@ -932,13 +967,31 @@ namespace NTwain [StructLayout(LayoutKind.Sequential)] protected struct MESSAGE { - public IntPtr hwnd; - public uint message; - public IntPtr wParam; - public IntPtr lParam; - public uint time; - public int x; - public int y; + /// + /// Initializes a new instance of the struct. + /// + /// The HWND. + /// The message. + /// The w parameter. + /// The l parameter. + public MESSAGE(IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam) + { + _hwnd = hwnd; + _message = (uint)message; + _wParam = wParam; + _lParam = lParam; + _time = 0; + _x = 0; + _y = 0; + } + + IntPtr _hwnd; + uint _message; + IntPtr _wParam; + IntPtr _lParam; + uint _time; + int _x; + int _y; } } } diff --git a/NTwain/TwainSessionOld.cs b/NTwain/TwainSessionOld.cs index 14d8352..82ebea7 100644 --- a/NTwain/TwainSessionOld.cs +++ b/NTwain/TwainSessionOld.cs @@ -209,11 +209,7 @@ namespace NTwain [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] bool IMessageFilter.PreFilterMessage(ref System.Windows.Forms.Message m) { - MESSAGE winmsg = default(MESSAGE); - winmsg.hwnd = m.HWnd; - winmsg.lParam = m.LParam; - winmsg.message = (uint)m.Msg; - winmsg.wParam = m.WParam; + var winmsg = new MESSAGE(m.HWnd, m.Msg, m.WParam, m.LParam); return HandleWndProcMessage(ref winmsg); } @@ -230,11 +226,7 @@ namespace NTwain [EnvironmentPermissionAttribute(SecurityAction.LinkDemand)] public IntPtr PreFilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { - MESSAGE winmsg = default(MESSAGE); - winmsg.hwnd = hwnd; - winmsg.lParam = lParam; - winmsg.message = (uint)msg; - winmsg.wParam = wParam; + var winmsg = new MESSAGE(hwnd, msg, wParam, lParam); handled = base.HandleWndProcMessage(ref winmsg); diff --git a/NTwain/TwainSessionWPF.cs b/NTwain/TwainSessionWPF.cs index 1deef70..4ecac94 100644 --- a/NTwain/TwainSessionWPF.cs +++ b/NTwain/TwainSessionWPF.cs @@ -33,11 +33,7 @@ namespace NTwain [EnvironmentPermissionAttribute(SecurityAction.LinkDemand)] public IntPtr PreFilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { - MESSAGE winmsg = default(MESSAGE); - winmsg.hwnd = hwnd; - winmsg.lParam = lParam; - winmsg.message = (uint)msg; - winmsg.wParam = wParam; + var winmsg = new MESSAGE(hwnd, msg, wParam, lParam); handled = base.HandleWndProcMessage(ref winmsg); diff --git a/NTwain/TwainSessionWinform.cs b/NTwain/TwainSessionWinform.cs index 415c555..2b889a7 100644 --- a/NTwain/TwainSessionWinform.cs +++ b/NTwain/TwainSessionWinform.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Security.Permissions; using System.Text; using System.Windows.Forms; @@ -22,13 +23,10 @@ namespace NTwain #region IMessageFilter Members - bool IMessageFilter.PreFilterMessage(ref Message m) + [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] + public bool PreFilterMessage(ref Message m) { - MESSAGE winmsg = default(MESSAGE); - winmsg.hwnd = m.HWnd; - winmsg.lParam = m.LParam; - winmsg.message = (uint)m.Msg; - winmsg.wParam = m.WParam; + var winmsg = new MESSAGE(m.HWnd, m.Msg, m.WParam, m.LParam); return HandleWndProcMessage(ref winmsg); }