public void Close() { if (SourceId.Id != 0) { UserInterface userInterface = new UserInterface(); TwainResult result = Twain32Native.DsUserInterface( _applicationId, SourceId, DataGroup.Control, DataArgumentType.UserInterface, Message.DisableDS, userInterface); if (result != TwainResult.Failure) { result = Twain32Native.DsmIdentity( _applicationId, IntPtr.Zero, DataGroup.Control, DataArgumentType.Identity, Message.CloseDS, SourceId); } } }
public void Close() { if (SourceId.Id != 0) { try { UserInterface userInterface = new UserInterface(); TwainResult result = Twain32Native.DsUserInterface( _applicationId, SourceId, DataGroup.Control, DataArgumentType.UserInterface, Message.DisableDS, userInterface); if (result != TwainResult.Failure) { result = Twain32Native.DsmIdentity( _applicationId, IntPtr.Zero, DataGroup.Control, DataArgumentType.Identity, Message.CloseDS, SourceId); } } catch { // ignore this is bypass an error that if trigerd needs the whole twain classto be reinitialised } } }
/// <summary> /// 打開DS,進入State 4以溝通Capabilities /// </summary> /// <exception cref="DeviceOpenExcetion">當DS被其他人占用的時候可能會擲回,或twain DLL沒有錯誤,但是回傳值失敗的話就會擲回</exception> public void OpenSource() { TwainResult result = TwainResult.NotDSEvent; try { result = Twain32Native.DsmIdentity( _applicationId, IntPtr.Zero, DataGroup.Control, DataArgumentType.Identity, Message.OpenDS, SourceId); } catch (Exception ex) { throw new DeviceOpenExcetion(ex.Message); } if (result != TwainResult.Success) { throw new DeviceOpenExcetion("Error opening data source", result); } Logger.WriteLog(LOG_LEVEL.LL_NORMAL_LOG, string.Format("DataSource \"{0}\" successfully opened.", this.SourceId.ProductName)); }
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); } }
public bool Disable() { if (SourceState != null && SourceState >= TwainState.SourceEnabled) { if (SourceId.Id != 0) { var userInterface = new UserInterface(); TwainResult result = Twain32Native.DsUserInterface( _applicationId, SourceId, DataGroup.Control, DataArgumentType.UserInterface, Message.DisableDS, userInterface); if (result != TwainResult.Failure) { _log.Debug(string.Format("DisableDS, result: {0}", result)); SourceState = TwainState.SourceOpen; return(true); } var condition = DataSourceManager.GetConditionCode(_applicationId, SourceId); _log.Debug(string.Format("DisableDS, result: {0}, conditionCode: {1}", result, condition)); return(false); } return(false); } return(false); }
protected void SetValue <T>(T value) { /*log.Debug(string.Format("Attempting to set capabilities:{0}, value:{1}, type:{1}", * _capability, value, _twainType));*/ Logger.WriteLog(LOG_LEVEL.LL_NORMAL_LOG, string.Format("Attempting to set capabilities:{0}, value:{1}, type:{2}", _capability, value, _twainType.ToString())); int rawValue = Convert.ToInt32(value); var oneValue = new CapabilityOneValue(_twainType, rawValue); var twainCapability = TwainCapability.From(_capability, oneValue); TwainResult result = Twain32Native.DsCapability( _applicationId, _sourceId, DataGroup.Control, DataArgumentType.Capability, Message.Set, twainCapability); if (result != TwainResult.Success) { Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, string.Format("Failed to set capabilities:{0}, value:{1}, type:{2}, result:{3}", _capability, value, _twainType, result)); if (result == TwainResult.Failure) { var conditionCode = GetStatus(); Logger.WriteLog(LOG_LEVEL.LL_SERIOUS_ERROR, string.Format("Failed to set capabilites:{0} reason: {1}", _capability, conditionCode)); throw new TwainException("Failed to set capability.", result, conditionCode); } else if (result == TwainResult.CheckStatus) { Logger.WriteLog(LOG_LEVEL.LL_SUB_FUNC, "Value changed but not to requested value"); } else { throw new TwainException("Failed to set capability.", result); } } else { Logger.WriteLog(LOG_LEVEL.LL_NORMAL_LOG, "Set capabilities:" + _capability + " successfully"); } }
protected void SetValue <T>(T value) { log.Debug(string.Format("Attempting to set capabilities:{0}, value:{1}, type:{1}", _capability, value, _twainType)); int rawValue = Convert.ToInt32(value); var oneValue = new CapabilityOneValue(_twainType, rawValue); var twainCapability = TwainCapability.From(_capability, oneValue); TwainResult result = Twain32Native.DsCapability( _applicationId, _sourceId, DataGroup.Control, DataArgumentType.Capability, Message.Set, twainCapability); if (result != TwainResult.Success) { log.Debug(string.Format("Failed to set capabilities:{0}, value:{1}, type:{1}, result:{2}", _capability, value, _twainType, result)); if (result == TwainResult.Failure) { var conditionCode = GetStatus(); log.Error(string.Format("Failed to set capabilites:{0} reason: {1}", _capability, conditionCode)); throw new TwainException("Failed to set capability.", result, conditionCode); } else if (result == TwainResult.CheckStatus) { log.Debug("Value changed but not to requested value"); } else { throw new TwainException("Failed to set capability.", result); } } else { log.Debug("Set capabilities successfully"); } }
public TwainException(string message, TwainResult returnCode, ConditionCode conditionCode) : this(message, null) { ReturnCode = returnCode; ConditionCode = conditionCode; }
public TwainException(string message, TwainResult returnCode, ConditionCode conditionCode) : this(message, null) { ReturnCode = returnCode; ConditionCode = conditionCode; }
protected IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (DataSource.SourceId.Id == 0) { handled = false; return(IntPtr.Zero); } int pos = User32Native.GetMessagePos(); WindowsMessage message = new WindowsMessage(); message.hwnd = hwnd; message.message = msg; message.wParam = wParam; message.lParam = lParam; message.time = User32Native.GetMessageTime(); message.x = (short)pos; message.y = (short)(pos >> 16); Marshal.StructureToPtr(message, _eventMessage.EventPtr, false); _eventMessage.Message = 0; TwainResult result = Twain32Native.DsEvent( ApplicationId, DataSource.SourceId, DataGroup.Control, DataArgumentType.Event, Message.ProcessEvent, ref _eventMessage); if (result == TwainResult.NotDSEvent) { handled = false; return(IntPtr.Zero); } switch (_eventMessage.Message) { case Message.XFerReady: Exception exception = null; try { TransferPictures(); } catch (Exception e) { exception = e; } CloseDsAndCompleteScanning(exception); break; case Message.CloseDS: case Message.CloseDSOK: case Message.CloseDSReq: CloseDsAndCompleteScanning(null); break; case Message.DeviceEvent: break; default: break; } handled = true; return(IntPtr.Zero); }
public DefaultDataSourceException(string message, TwainResult returnCode, ConditionCode conditionCode) : this(message, null) { ReturnCode = returnCode; ConditionCode = conditionCode; }
protected IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (DataSource.SourceId.Id == 0) { handled = false; return(IntPtr.Zero); } int pos = User32Native.GetMessagePos(); WindowsMessage message = new WindowsMessage(); message.hwnd = hwnd; message.message = msg; message.wParam = wParam; message.lParam = lParam; message.time = User32Native.GetMessageTime(); message.x = (short)pos; message.y = (short)(pos >> 16); Marshal.StructureToPtr(message, _eventMessage.EventPtr, false); _eventMessage.Message = 0; TwainResult result = Twain32Native.DsEvent( ApplicationId, DataSource.SourceId, DataGroup.Control, DataArgumentType.Event, Message.ProcessEvent, ref _eventMessage); if (result == TwainResult.NotDSEvent) { handled = false; return(IntPtr.Zero); } switch (_eventMessage.Message) { case Message.XFerReady: IList <IntPtr> imagePointers = TransferPictures(); foreach (IntPtr image in imagePointers) { using (var renderer = new BitmapRenderer(image)) { Images.Add(renderer.RenderToBitmap()); } } EndingScan(); DataSource.Close(); ScanningComplete(this, EventArgs.Empty); break; case Message.CloseDS: EndingScan(); DataSource.Close(); break; case Message.CloseDSOK: EndingScan(); DataSource.Close(); break; case Message.DeviceEvent: break; } handled = true; return(IntPtr.Zero); }