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 DataSource(Identity applicationId, Identity sourceId, IWindowsMessageHook messageHook, ILog logger)
        {
            _applicationId = applicationId;
            SourceId = sourceId.Clone();
            _messageHook = messageHook;
			SourceState = null;
	        _log = logger;
        }
        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 DataSource GetDefault(Identity applicationId)
		{
			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 = GetConditionCode(applicationId, null);

				_log.Debug(string.Format("GetDefault, result: {0}, conditionCode: {1}", result, status));
				throw new TwainException("Error getting information about the default source: " + result, result, status);
			}
			_log.Debug(string.Format("GetDefault, result: {0}", result));
			return new DataSource(applicationId, defaultSourceId,_messageHook, _log);
		}
 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;
        }
        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 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 ConditionCode GetConditionCode(Identity applicationId, Identity sourceId)
        {
            Status status = new Status();

            Twain32Native.DsmStatus(
                applicationId,
                sourceId,
                DataGroup.Control,
                DataArgumentType.Status,
                Message.Get,
                status);

            return status.ConditionCode;
        }
		public List<DataSource> GetAllSources()
		{
			var sources = new List<DataSource>();
			var id = new Identity();

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

			_log.Debug(string.Format("GetFirst (GetAllSources), result: {0}", result));
			if (result == TwainResult.EndOfList)
			{
				return sources;
			}
			if (result != TwainResult.Success)
			{
				throw new TwainException("Error getting first source.", result);
			}
			
			sources.Add(new DataSource(ApplicationId, id, _messageHook, _log));
			

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

				_log.Debug(string.Format("GetNext (GetAllSources), result: {0}", result));
				if (result == TwainResult.EndOfList)
				{
					break;
				}
				if (result != TwainResult.Success)
				{
					throw new TwainException("Error enumerating sources.", result);
				}

				sources.Add(new DataSource(ApplicationId, id, _messageHook, _log));
			}

			var sb = new StringBuilder("GetAllSources result: ");
			foreach (var dataSource in sources)
			{
				sb.Append(string.Format("sourceId: {0}; ", dataSource.SourceId.ProductName));
			}
			_log.Debug(sb);
			return sources;
		}