Beispiel #1
0
        public DataSourceManager(Identity applicationId, IWindowsMessageHook messageHook)
        {
            // Make a copy of the identity in case it gets modified
            ApplicationId = applicationId.Clone();

            ScanningComplete += delegate { };
            TransferImage    += delegate { };

            _messageHook = messageHook;
            _messageHook.FilterMessageCallback = FilterMessage;
            IntPtr windowHandle = _messageHook.WindowHandle;

            _eventMessage.EventPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WindowsMessage)));

            // Initialise the data source manager
            TwainResult result = Twain32Native.DsmParent(
                ApplicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Parent,
                Message.OpenDSM,
                ref windowHandle);

            if (result == TwainResult.Success)
            {
                //according to the 2.0 spec (2-10) if (applicationId.SupportedGroups
                // | DataGroup.Dsm2) > 0 then we should call DM_Entry(id, 0, DG_Control, DAT_Entrypoint, MSG_Get, wh)
                //right here
                DataSource = DataSource.GetDefault(ApplicationId, _messageHook);
            }
            else
            {
                throw new TwainException("Error initialising DSM: " + result, result);
            }
        }
Beispiel #2
0
        public Scan(Program.Options options)
        {
            log.Debug("creating scan host");

            log.DebugFormat("create interprocess input pipe (handle={0})", options.pipeIn);
            pipeIn = new AnonymousPipeClientStream(PipeDirection.In, options.pipeIn);

            log.DebugFormat("create interprocess output pipe (handle={0})", options.pipeOut);
            pipeOut = new AnonymousPipeClientStream(PipeDirection.Out, options.pipeOut);

            log.Debug("create sync event");
            sync = new ManualResetEvent(false);

            log.Debug("create native messaging port");
            port = new Port(pipeIn, pipeOut);

            log.Debug("create stop event");
            stop = new ManualResetEvent(false);

            log.Debug("create form");
            form = new Form();
            form.TopMost = true;
            form.BringToFront();

            log.Debug("create hook");
            hook = new WinFormsWindowMessageHook(form);

            log.Debug("create image acquired and image transferred events");
            imageAcquired = new ManualResetEvent(false);
            imageTransferred = new ManualResetEvent(false);

            log.Debug("scan host created");
        }
Beispiel #3
0
        public static DataSource GetDefault(Identity applicationId, IWindowsMessageHook messageHook)
        {
            log.Debug("create new identity");
            var defaultSourceId = new Identity();

            // Attempt to get information about the system default source
            log.Debug("call DsmIdentity");
            var result = Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.GetDefault,
                defaultSourceId);

            if (result != TwainResult.Success)
            {
                log.Debug("result != success");
                var status = DataSourceManager.GetConditionCode(applicationId, null);
                throw new TwainException("Error getting information about the default source: " + result, result, status);
            }

            log.Debug("return new data source");
            return(new DataSource(applicationId, defaultSourceId, messageHook));
        }
        protected virtual void Dispose(bool disposing)
        {
            Marshal.FreeHGlobal(_eventMessage.EventPtr);

            if (disposing)
            {
                DataSource.Dispose();

                IntPtr windowHandle = _messageHook.WindowHandle;

                if (ApplicationId.Id != 0)
                {
                    // Close down the data source manager
                    Twain32Native.DsmParent(
                        ApplicationId,
                        IntPtr.Zero,
                        DataGroup.Control,
                        DataArgumentType.Parent,
                        Message.CloseDSM,
                        ref windowHandle);
                }

                ApplicationId.Id = 0;
                _messageHook     = null;
            }
        }
Beispiel #5
0
        public Scan(Program.Options options)
        {
            log.Debug("creating scan host");

            log.DebugFormat("create interprocess input pipe (handle={0})", options.pipeIn);
            pipeIn = new AnonymousPipeClientStream(PipeDirection.In, options.pipeIn);

            log.DebugFormat("create interprocess output pipe (handle={0})", options.pipeOut);
            pipeOut = new AnonymousPipeClientStream(PipeDirection.Out, options.pipeOut);

            log.Debug("create sync event");
            sync = new ManualResetEvent(false);

            log.Debug("create native messaging port");
            port = new Port(pipeIn, pipeOut);

            log.Debug("create stop event");
            stop = new ManualResetEvent(false);

            log.Debug("create form");
            form         = new Form();
            form.TopMost = true;
            form.BringToFront();

            log.Debug("create hook");
            hook = new WinFormsWindowMessageHook(form);

            log.Debug("create image acquired and image transferred events");
            imageAcquired    = new ManualResetEvent(false);
            imageTransferred = new ManualResetEvent(false);

            log.Debug("scan host created");
        }
        public DataSourceManager(Identity applicationId, IWindowsMessageHook messageHook)
        {
            // Make a copy of the identity in case it gets modified
            ApplicationId = applicationId.Clone();

            ScanningComplete += delegate { };
            TransferImage += delegate { };

            _messageHook = messageHook;
            _messageHook.FilterMessageCallback = FilterMessage;
            IntPtr windowHandle = _messageHook.WindowHandle;

            _eventMessage.EventPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WindowsMessage)));

            // Initialise the data source manager
            TwainResult result = Twain32Native.DsmParent(
                ApplicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Parent,
                Message.OpenDSM,
                ref windowHandle);

            if (result == TwainResult.Success)
            {
                //according to the 2.0 spec (2-10) if (applicationId.SupportedGroups
                // | DataGroup.Dsm2) > 0 then we should call DM_Entry(id, 0, DG_Control, DAT_Entrypoint, MSG_Get, wh)
                //right here
                DataSource = DataSource.GetDefault(ApplicationId, _messageHook);
            }
            else
            {
                throw new TwainException("Error initialising DSM: " + result, result);
            }
        }
Beispiel #7
0
 public DataSource(Identity applicationId, Identity sourceId, IWindowsMessageHook messageHook, ILog logger)
 {
     _applicationId = applicationId;
     SourceId       = sourceId.Clone();
     _messageHook   = messageHook;
     SourceState    = null;
     _log           = logger;
 }
Beispiel #8
0
        public DataSource(Identity applicationId, Identity sourceId, IWindowsMessageHook messageHook, ILog logger)
        {
            _applicationId = applicationId;
            SourceId = sourceId.Clone();
            _messageHook = messageHook;
			SourceState = null;
	        _log = logger;
        }
Beispiel #9
0
        public Twain(IWindowsMessageHook messageHook)
        {
            ScanningComplete += delegate {};

            _dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook);
            _dataSourceManager.ScanningComplete += delegate
            {
                ScanningComplete(this, EventArgs.Empty);
            };
        }
Beispiel #10
0
        public static List <DataSource> GetAllSources(Identity applicationId, IWindowsMessageHook messageHook)
        {
            var      sources = new List <DataSource>();
            Identity id      = new Identity();

            // Get the first source
            var result = Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.GetFirst,
                id);

            if (result == TwainResult.EndOfList)
            {
                return(sources);
            }
            else if (result != TwainResult.Success)
            {
                throw new TwainException("Error getting first source.", result);
            }
            else
            {
                sources.Add(new DataSource(applicationId, id, messageHook));
            }

            while (true)
            {
                // Get the next source
                result = Twain32Native.DsmIdentity(
                    applicationId,
                    IntPtr.Zero,
                    DataGroup.Control,
                    DataArgumentType.Identity,
                    Message.GetNext,
                    id);

                if (result == TwainResult.EndOfList)
                {
                    break;
                }
                else if (result != TwainResult.Success)
                {
                    throw new TwainException("Error enumerating sources.", result);
                }

                sources.Add(new DataSource(applicationId, id, messageHook));
            }

            return(sources);
        }
        public static List<DataSource> GetAllSources(Identity applicationId, IWindowsMessageHook messageHook)
        {
            var sources = new List<DataSource>();
            Identity id = new Identity();

            // Get the first source
            var result = Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.GetFirst,
                id);

            if (result == TwainResult.EndOfList)
            {
                return sources;
            }
            else if (result != TwainResult.Success)
            {
                throw new TwainException("Error getting first source.", result);
            }
            else
            {
                sources.Add(new DataSource(applicationId, id, messageHook));
            }

            while (true)
            {
                // Get the next source
                result = Twain32Native.DsmIdentity(
                    applicationId,
                    IntPtr.Zero,
                    DataGroup.Control,
                    DataArgumentType.Identity,
                    Message.GetNext,
                    id);

                if (result == TwainResult.EndOfList)
                {
                    break;
                }
                else if (result != TwainResult.Success)
                {
                    throw new TwainException("Error enumerating sources.", result);
                }

                sources.Add(new DataSource(applicationId, id, messageHook));
            }

            return sources;
        }
Beispiel #12
0
        public Twain(IWindowsMessageHook messageHook)
        {
            ScanningComplete += delegate { };

            _dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook);
            _dataSourceManager.ScanningComplete += delegate(object sender, ScanningCompleteEventArgs args)
            {
                ScanningComplete(this, args);
            };
            _dataSourceManager.TransferImages += delegate(object sender, TransferImagesEventArgs args)
            {
                TransferImages(this, args);
            };
        }
Beispiel #13
0
        public Twain(IWindowsMessageHook messageHook)
        {
            ScanningComplete += delegate { };
            TransferImage += delegate { };

            _dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook);
            _dataSourceManager.ScanningComplete += delegate(object sender, ScanningCompleteEventArgs args)
            {
                ScanningComplete(this, args);
            };
            _dataSourceManager.TransferImage += delegate(object sender, TransferImageEventArgs args)
            {
                TransferImage(this, args);
            };
        }
Beispiel #14
0
        public static DataSource UserSelected(Identity applicationId, IWindowsMessageHook messageHook)
        {
            var defaultSourceId = new Identity();

            // Show the TWAIN interface to allow the user to select a source
            Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.UserSelect,
                defaultSourceId);

            return(new DataSource(applicationId, defaultSourceId, messageHook));
        }
Beispiel #15
0
        /// <summary>
        /// Twain dll的最外層。當建構子完成,代表進入twain state 3,接下來就可以去open data souse了。
        /// </summary>
        /// <param name="messageHook"></param>
        /// <param name="useIncremental">使用一次性傳輸或是buffered memory mode傳輸</param>
        public Twain(IWindowsMessageHook messageHook, bool useIncremental = true)
        {
            ScanningComplete += delegate { };
            TransferImage    += delegate { };

            _dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook);
            IsIncremental      = useIncremental;
            _dataSourceManager.UseIncrementalMemoryXfer = useIncremental;
            _dataSourceManager.ScanningComplete        += delegate(object sender, ScanningCompleteEventArgs args)
            {
                ScanningComplete(this, args);
            };
            _dataSourceManager.TransferImage += delegate(object sender, TransferImageEventArgs args)
            {
                TransferImage(this, args);
            };
        }
Beispiel #16
0
        public Twain(IWindowsMessageHook messageHook)
        {
            log.Debug("add scanning complete delegate");
            ScanningComplete += delegate { };
            log.Debug("add transfer image delegate");
            TransferImage += delegate { };

            log.Debug("create data source manager");
            _dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook);

            log.Debug("add scanning complete delegate to dsm");
            _dataSourceManager.ScanningComplete += delegate(object sender, ScanningCompleteEventArgs args)
            {
                ScanningComplete(this, args);
            };
            log.Debug("add transfer image delegate to dsm");
            _dataSourceManager.TransferImage += delegate(object sender, TransferImageEventArgs args)
            {
                TransferImage(this, args);
            };
            log.Debug("finished");
        }
Beispiel #17
0
        public Twain(IWindowsMessageHook messageHook)
        {
            log.Debug("add scanning complete delegate");
            ScanningComplete += delegate { };
            log.Debug("add transfer image delegate");
            TransferImage += delegate { };

            log.Debug("create data source manager");
            _dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook);

            log.Debug("add scanning complete delegate to dsm");
            _dataSourceManager.ScanningComplete += delegate(object sender, ScanningCompleteEventArgs args)
            {
                ScanningComplete(this, args);
            };
            log.Debug("add transfer image delegate to dsm");
            _dataSourceManager.TransferImage += delegate(object sender, TransferImageEventArgs args)
            {
                TransferImage(this, args);
            };
            log.Debug("finished");
        }
Beispiel #18
0
        /// <summary>
        /// Get default data source.
        /// </summary>
        /// <param name="applicationId"></param>
        /// <param name="messageHook"></param>
        /// <returns></returns>
        public static DataSource GetDefault(Identity applicationId, IWindowsMessageHook messageHook)
        {
            var defaultSourceId = new Identity();

            // Attempt to get information about the system default source
            var result = Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.GetDefault,
                defaultSourceId);

            if (result != TwainResult.Success)
            {
                ConditionCode status = DataSourceManager.GetConditionCode(applicationId, null);
                Logger.WriteLog(LOG_LEVEL.LL_SERIOUS_ERROR, "Error getting information about the default source: " + result);
                throw new TwainException("Error getting information about the default source: " + result, result, status);
            }

            return(new DataSource(applicationId, defaultSourceId, messageHook));
        }
Beispiel #19
0
        public Diagnostics(IWindowsMessageHook messageHook)
        {
            using (var dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook))
            {
                dataSourceManager.SelectSource();

                var dataSource = dataSourceManager.DataSource;
                dataSource.OpenSource();

                foreach (Capabilities capability in Enum.GetValues(typeof(Capabilities)))
                {
                    try
                    {
                        var result = Capability.GetBoolCapability(capability, dataSourceManager.ApplicationId, dataSource.SourceId);

                        Console.WriteLine("{0}: {1}", capability, result);
                    }
                    catch (TwainException e)
                    {
                        Console.WriteLine("{0}: {1} {2} {3}", capability, e.Message, e.ReturnCode, e.ConditionCode);
                    }
                }
            }
        }
Beispiel #20
0
        public Diagnostics(IWindowsMessageHook messageHook)
        {
            using (var dataSourceManager = new DataSourceManager(DataSourceManager.DefaultApplicationId, messageHook))
            {
                dataSourceManager.SelectSource();

                var dataSource = dataSourceManager.DataSource;
                dataSource.OpenSource();

                foreach (Capabilities capability in Enum.GetValues(typeof(Capabilities)))
                {
                    try
                    {
                        var result = Capability.GetBoolCapability(capability, dataSourceManager.ApplicationId, dataSource.SourceId);

                        Console.WriteLine("{0}: {1}", capability, result);
                    }
                    catch (TwainException e)
                    {
                        Console.WriteLine("{0}: {1} {2} {3}", capability, e.Message, e.ReturnCode, e.ConditionCode);
                    }
                }
            }
        }
Beispiel #21
0
        public static DataSource GetSource(string sourceProductName, Identity applicationId, IWindowsMessageHook messageHook)
        {
            // A little slower than it could be, if enumerating unnecessary sources is slow. But less code duplication.
            foreach (var source in GetAllSources(applicationId, messageHook))
            {
                if (sourceProductName.Equals(source.SourceId.ProductName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(source);
                }
            }

            return(null);
        }
Beispiel #22
0
        public static DataSource GetSource(string sourceProductName, Identity applicationId, IWindowsMessageHook messageHook)
        {
            Identity id = new Identity();

            // Get the first source
            var result = Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.GetFirst,
                id);

            if (result == TwainResult.EndOfList)
            {
                return(null);
            }
            else if (result != TwainResult.Success)
            {
                throw new TwainException("Error getting first source.", result);
            }
            else if (id.ProductName == sourceProductName)
            {
                return(new DataSource(applicationId, id, messageHook));
            }

            while (true)
            {
                id = new Identity();
                // Get the next source
                result = Twain32Native.DsmIdentity(
                    applicationId,
                    IntPtr.Zero,
                    DataGroup.Control,
                    DataArgumentType.Identity,
                    Message.GetNext,
                    id);

                if (result == TwainResult.EndOfList)
                {
                    break;
                }
                else if (result != TwainResult.Success)
                {
                    throw new TwainException("Error enumerating sources.", result);
                }

                else if (id.ProductName == sourceProductName)
                {
                    return(new DataSource(applicationId, id, messageHook));
                }
            }

            return(null);
        }
Beispiel #23
0
 public ImageScan(IWindowsMessageHook messageHook)
 {
     Twain = new Twain(messageHook);
 }
Beispiel #24
0
 public DataSource(Identity applicationId, Identity sourceId, IWindowsMessageHook messageHook)
 {
     _applicationId = applicationId;
     SourceId       = sourceId.Clone();
     _messageHook   = messageHook;
 }
		public static DataSource UserSelected(Identity applicationId, IWindowsMessageHook messageHook)
		{
			var defaultSourceId = new Identity();

			// Show the TWAIN interface to allow the user to select a source
			var result = Twain32Native.DsmIdentity(
				applicationId,
				IntPtr.Zero,
				DataGroup.Control,
				DataArgumentType.Identity,
				Message.UserSelect,
				defaultSourceId);

			_log.Debug(string.Format("UserSelect, result: {0}", result));
			return new DataSource(applicationId, defaultSourceId, messageHook, _log);
		}
        public static DataSource GetDefault(Identity applicationId, IWindowsMessageHook messageHook)
        {
            var defaultSourceId = new Identity();

            // Attempt to get information about the system default source
            var result = Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.GetDefault,
                defaultSourceId);

            if (result != TwainResult.Success)
            {
                var status = DataSourceManager.GetConditionCode(applicationId, null);
                throw new TwainException("Error getting information about the default source: " + result, result, status);
            }

            return new DataSource(applicationId, defaultSourceId, messageHook);
        }
 public DataSource(Identity applicationId, Identity sourceId, IWindowsMessageHook messageHook)
 {
     _applicationId = applicationId;
     SourceId = sourceId.Clone();
     _messageHook = messageHook;
 }
        public static DataSource UserSelected(Identity applicationId, IWindowsMessageHook messageHook)
        {
            var defaultSourceId = new Identity();

            // Show the TWAIN interface to allow the user to select a source
            Twain32Native.DsmIdentity(
                applicationId,
                IntPtr.Zero,
                DataGroup.Control,
                DataArgumentType.Identity,
                Message.UserSelect,
                defaultSourceId);

            return new DataSource(applicationId, defaultSourceId, messageHook);
        }
        public static DataSource GetSource(string sourceProductName, Identity applicationId, IWindowsMessageHook messageHook)
        {
            // A little slower than it could be, if enumerating unnecessary sources is slow. But less code duplication.
            foreach (var source in GetAllSources(applicationId, messageHook))
            {
                if (sourceProductName.Equals(source.SourceId.ProductName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return source;
                }
            }

            return null;
        }