/// <summary> /// DG_CONTROL / DAT_SETUPFILEXFER / MSG_GET /// Returns information about the file into which the Source has or will put the acquired DG_IMAGE or DG_AUDIO data. /// /// DG_CONTROL / DAT_SETUPFILEXFER / MSG_GETDEFAULT /// Returns information for the default DG_IMAGE or DG_AUDIO file. /// /// DG_CONTROL / DAT_SETUPFILEXFER / MSG_RESET /// Resets the current file information to the DG_IMAGE or DG_AUDIO default file information and /// returns that default information. /// /// DG_CONTROL / DAT_SETUPFILEXFER / MSG_SET /// Sets the file transfer information for the next file transfer. The application is responsible for /// verifying that the specified file name is valid and that the file either does not currently exist(in /// which case, the Source is to create the file), or that the existing file is available for opening and /// read/write operations. The application should also assure that the file format it is requesting can /// be provided by the Source (otherwise,the Source will generate a TWRC_FAILURE / /// TWCC_BADVALUE error). /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _SetupFileXferProcessRequest(TwMSG msg, IntPtr data) { if (this.SupportedXferMechs.Contains(TwSX.File)) { switch (msg) { case TwMSG.Get: Marshal.StructureToPtr(new TwSetupFileXfer { FileName = this.XferEnvironment.FileXferName, Format = this.XferEnvironment.FileXferFormat }, data, true); return(TwRC.Success); case TwMSG.GetDefault: Marshal.StructureToPtr(new TwSetupFileXfer { FileName = this.XferEnvironment.DefaultFileXferName, Format = this.XferEnvironment.DefaultFileXferFormat }, data, true); return(TwRC.Success); case TwMSG.Set: var _setupFileXfer = Marshal.PtrToStructure(data, typeof(TwSetupFileXfer)) as TwSetupFileXfer; this.OnSetupFileXfer(_setupFileXfer.FileName, _setupFileXfer.Format); this.XferEnvironment.FileXferName = _setupFileXfer.FileName; this.XferEnvironment.FileXferFormat = _setupFileXfer.Format; return(TwRC.Success); case TwMSG.Reset: this.XferEnvironment._FileXferReset(); return(TwRC.Success); } } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// DG_CONTROL / DAT_USERINTERFACE / MSG_ENABLEDS /// This operation causes three responses in the Source: /// - Places the Source into a “ready to acquire” condition. If the application raises the Source’s user /// interface (see #2, below), the Source will wait to assert MSG_XFERREADY until the “GO” button /// in its user interface or on the device is clicked. If the application bypasses the Source’s user /// interface, this operation causes the Source to become immediately “armed”. That is, the Source /// should assert MSG_XFERREADY as soon as it has data to transfer. /// - The application can choose to raise the Source’s built-in user interface, or not, using this /// operation. The application signals the Source’s user interface should be displayed by setting /// pUserInterface->ShowUI to TRUE. If the application does not want the Source’s user interface /// to be displayed, or wants to replace the Source’s user interface with one of its own, it sets /// pUserInterface->ShowUI to FALSE. If activated, the Source’s user interface will remain /// displayed until it is closed by the user or explicitly disabled by the application(see Note). /// - Terminates Source’s acceptance of “set capability” requests from the application. Capabilities /// can only be negotiated in State 4 (unless special arrangements are made using the /// CAP_EXTENDEDCAPS capability). Values of capabilities can still be inquired in States 5 through 7. /// Note: Once the Source is enabled, the application must begin sending the Source every event /// that enters the application’s main event loop.The application must continue to send the /// Source events until it disables(MSG_DISABLEDS) the Source. This is true even if the /// application chooses not to use the Source’s built-in user interface. /// /// DG_CONTROL / DAT_USERINTERFACE / MSG_DISABLEDS /// This operation causes the Source’s user interface, if displayed during the /// DG_CONTROL / DAT_USERINTERFACE / MSG_ENABLEDS operation, to be lowered. The Source is returned to /// State 4,where capability negotiation can again occur. The application can invoke this operation /// either because it wants to shut down the current session,or in response to the Source “posting” a /// MSG_CLOSEDSREQ event to it. Rarely, the application may need to close the Source because an /// error condition was detected. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _UserInterfaceProcessRequest(TwMSG msg, IntPtr data) { var _ui = Marshal.PtrToStructure(data, typeof(TwUserInterface)) as TwUserInterface; switch (msg) { case TwMSG.EnableDS: if ((this.State & DataSourceState.Enabled) != 0) { throw new DataSourceException(TwRC.Failure, TwCC.SeqError); } this.State |= DataSourceState.Enabled; try { this.OnEnableDS(_ui.ShowUI, _ui.ModalUI, _ui.ParentHand); } catch (Exception) { this.State &= ~DataSourceState.Enabled; throw; } return(TwRC.Success); case TwMSG.DisableDS: if ((this.State & DataSourceState.Enabled) == 0) { throw new DataSourceException(TwRC.Failure, TwCC.SeqError); } this.OnDisableDS(_ui.ParentHand); this.State &= ~DataSourceState.Enabled; return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
internal static TwRC DsmInvoke(TwIdentity appId, TwMSG msg) { if (!DataSourceServices.DsmEntries.ContainsKey(appId.Id)) { DataSourceServices.DsmEntries.Add(appId.Id, _DsmEntry.Create()); } return(DataSourceServices.DsmEntries[appId.Id].DsmRaw(DataSourceServices.Handlers[appId.Id].DS.IndentityPointer, DataSourceServices.Handlers[appId.Id].Application.IndentityPointer, TwDG.Control, TwDAT.Null, msg, IntPtr.Zero)); }
/// <summary> /// DG_CONTROL / DAT_XFERGROUP / MSG_GET /// Returns the Data Group (the type of data) for the upcoming transfer. The Source is required to /// only supply one of the DGs specified in the SupportedGroups field of a AppIdentity. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _XferGroupProcessRequest(TwMSG msg, IntPtr data) { switch (msg) { case TwMSG.Get: Marshal.StructureToPtr((uint)this.OnGetXferGroup(), data, true); return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// DG_IMAGE / DAT_IMAGEFILEXFER / MSG_GET /// This operation is used to initiate the transfer of an image from the Source to the application via the /// disk-file transfer mechanism. It causes the transfer to begin. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <returns>TWAIN Return Codes.</returns> /// <exception cref="DataSourceException"></exception> private TwRC _ImageFileXferProcessRequest(TwMSG msg) { switch (msg) { case TwMSG.Get: this.OnImageFileXfer(); return(TwRC.XferDone); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// DG_IMAGE / DAT_IMAGEINFO / MSG_GET /// This operation provides the Application with specific image description /// information about the current image that has just been transferred. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> /// <exception cref="DataSourceException"></exception> private TwRC _ImageInfoProcessRequest(TwMSG msg, IntPtr data) { switch (msg) { case TwMSG.Get: Marshal.StructureToPtr(this.XferEnvironment.ImageInfo.ToTwImageInfo(), data, true); return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// DG_CONTROL / DAT_PENDINGXFERS / MSG_ENDXFER /// This triplet is used to cancel or terminate a transfer. Issued in state 6, this triplet cancels the next /// pending transfer, discards the transfer data, and decrements the pending transfers count. In state 7, /// this triplet terminates the current transfer. If any data has not been transferred (this is only /// possible during a memory transfer) that data is discarded. /// The application can use this operation to cancel the next pending transfer (Source writers take /// note of this). For example, after the application checks TW_IMAGEINFO(or TW_AUDIOINFO, if /// transferring audio snippets), it may decide to not transfer the next image. The operation must be /// sent prior to the beginning of the transfer, otherwise the Source will simply abort the current /// transfer. The Source decrements the number of pending transfers. /// /// DG_CONTROL / DAT_PENDINGXFERS / MSG_GET /// Returns the number of transfers the Source is ready to supply to the application, upon demand. If /// DAT_XFERGROUP is set to DG_IMAGE, this is the number of images.If DAT_XFERGROUP is set to /// DG_AUDIO, this is the number of audio snippets for the current image. If there is no current /// image, this call must return TWRC_FAILURE / TWCC_SEQERROR. /// /// DG_CONTROL / DAT_PENDINGXFERS / MSG_RESET /// Sets the number of pending transfers in the Source to zero. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _PendingXfersProcessRequest(TwMSG msg, IntPtr data) { try { switch (msg) { case TwMSG.EndXfer: if ((this.State & DataSourceState.Ready) == 0) { throw new DataSourceException(TwRC.Failure, TwCC.SeqError); } if (this.XferEnvironment.PendingXfers == 0) { throw new DataSourceException(TwRC.Failure, TwCC.OperationError); } this.XferEnvironment.PendingXfers--; this.OnEndXfer(); break; case TwMSG.Get: break; case TwMSG.Reset: if ((this.State & DataSourceState.Ready) == 0 || (this.State & DataSourceState.Transferring) != 0) { throw new DataSourceException(TwRC.Failure, TwCC.SeqError); } this.XferEnvironment.PendingXfers = 0; this.OnResetXfer(); break; default: throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); } var _pendingXfers = new TwPendingXfers { Count = this.XferEnvironment.PendingXfers }; for (var _cap = this[TwCap.JobControl]; _cap != null;) { _pendingXfers.EOJ = (uint)(TwJC)_cap.Value; break; } Marshal.StructureToPtr(_pendingXfers, data, true); return(TwRC.Success); } finally { if (this.XferEnvironment.PendingXfers >= 0) { this.State &= ~DataSourceState.Transferring; } if (this.XferEnvironment.PendingXfers == 0) { this.State &= ~DataSourceState.Ready; this.XferEnvironment.ImageInfo = null; } } }
private TwRC DSMident(TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, TwIdentity idds) { if (IntPtr.Size == 8) { return(Twain64.DSMident(origin, zeroptr, dg, dat, msg, idds)); } else { return(Twain32.DSMident(origin, zeroptr, dg, dat, msg, idds)); } }
private TwRC DSiinf(TwIdentity origin, TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwImageInfo imginf) { if (IntPtr.Size == 8) { return(Twain64.DSiinf(origin, dest, dg, dat, msg, imginf)); } else { return(Twain32.DSiinf(origin, dest, dg, dat, msg, imginf)); } }
private TwRC DSixfer(TwIdentity origin, TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap) { if (IntPtr.Size == 8) { return(Twain64.DSixfer(origin, dest, dg, dat, msg, ref hbitmap)); } else { return(Twain32.DSixfer(origin, dest, dg, dat, msg, ref hbitmap)); } }
private TwRC DSpxfer(TwIdentity origin, TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwPendingXfers pxfr) { if (IntPtr.Size == 8) { return(Twain64.DSpxfer(origin, dest, dg, dat, msg, pxfr)); } else { return(Twain32.DSpxfer(origin, dest, dg, dat, msg, pxfr)); } }
private TwRC DScap(TwIdentity origin, TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwCapability capa) { if (IntPtr.Size == 8) { return(Twain64.DScap(origin, dest, dg, dat, msg, capa)); } else { return(Twain32.DScap(origin, dest, dg, dat, msg, capa)); } }
/// <summary> /// DG_CONTROL / DAT_ENTRYPOINT / MSG_SET /// The TWAIN 2.0 Source Manager issues this command to Sources (that set DF_DS2) prior of any /// other command sent by the Application.In most cases it will immediately precede the call to /// DG_CONTROL / DAT_IDENTITY / MSG_OPEN. /// TWAIN 1.x Sources must continue to find and load the Source Manager DSM_Entry on /// their own. /// </summary> /// <param name="appId">This points to a TwIdentity structure.</param> /// <param name="msg">The Message of the operation triplet.</param> /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _EntryPointControlProcessRequest(TwIdentity appId, TwMSG msg, TwEntryPoint entry) { switch (msg) { case TwMSG.Set: DataSourceServices.Memory._SetEntryPoints(entry); this._dsmEntries.Add(appId.Id, _DsmEntry.Create(entry.DSM_Entry)); return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
private TwRC DSevent(TwIdentity origin, TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref TwEvent evt) { if (IntPtr.Size == 8) { return(Twain64.DSevent(origin, dest, dg, dat, msg, ref evt)); } else { return(Twain32.DSevent(origin, dest, dg, dat, msg, ref evt)); } }
/// <summary> /// DG_IMAGE / DAT_IMAGEMEMXFER / MSG_GET /// This operation is used to initiate the transfer of an image from the Source to the application via the /// Buffered Memory transfer mechanism. /// This operation supports the transfer of successive blocks of image data (in strips or, optionally, /// tiles) from the Source into one or more main memory transfer buffers. These buffers(for strips) /// are allocated and owned by the application. For tiled transfers, the source allocates the buffers. /// The application should repeatedly invoke this operation while TWRC_SUCCESS is returned by the Source. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <param name="isMemFile">If set to <c>true</c> that transfer a MemFile.</param> /// <returns>TWAIN Return Codes.</returns> /// <exception cref="DataSourceException"> /// </exception> private TwRC _ImageMemXferProcessRequest(TwMSG msg, IntPtr data, bool isMemFile) { switch (msg) { case TwMSG.Get: var _memXfer = Marshal.PtrToStructure(data, typeof(TwImageMemXfer)) as TwImageMemXfer; if (_memXfer.Memory.Length > this.XferEnvironment.MaxMemXferBufferSize || _memXfer.Memory.Length < this.XferEnvironment.MinMemXferBufferSize) { throw new DataSourceException(TwRC.Failure, TwCC.BadValue); } var _image = this.OnImageMemXfer(_memXfer.Memory.Length, isMemFile); if (_image == null) { return(TwRC.Cancel); } Marshal.StructureToPtr( new TwImageMemXfer { BytesPerRow = _image.BytesPerRow, BytesWritten = (uint)_image.ImageData.Length, Columns = _image.Columns, Compression = _image.Compression, Rows = _image.Rows, XOffset = _image.XOffset, YOffset = _image.YOffset, Memory = _memXfer.Memory }, data, true); var _pImageData = IntPtr.Zero; switch (_memXfer.Memory.Flags & (TwMF.Handle | TwMF.Pointer)) { case TwMF.Handle: _pImageData = DataSourceServices.Memory.Lock(_memXfer.Memory.TheMem); DataSourceServices.Memory.Unlock(_memXfer.Memory.TheMem); break; case TwMF.Pointer: _pImageData = _memXfer.Memory.TheMem; break; default: throw new DataSourceException(TwRC.Failure, TwCC.BadValue); } Marshal.Copy(_image.ImageData, 0, _pImageData, _image.ImageData.Length); return(_image.IsXferDone?TwRC.XferDone:TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// DG_CONTROL / DAT_STATUS / MSG_GET /// Returns the current Condition Code for the specified Source. /// </summary> /// <param name="appId">This points to a TwIdentity structure.</param> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The pData parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _StatusControlProcessRequest(TwIdentity appId, TwMSG msg, IntPtr data) { TwStatus _status = (TwStatus)Marshal.PtrToStructure(data, typeof(TwStatus)); switch (msg) { case TwMSG.Get: _status.ConditionCode = this._handlers.ContainsKey(appId.Id)?this._handlers[appId.Id].ConditionCode:this.ConditionCode; Marshal.StructureToPtr(_status, data, true); this._SetConditionCode(appId, TwCC.Success); return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// DG_CONTROL / DAT_SETUPMEMXFER / MSG_GET /// Returns the Source’s preferred, minimum, and maximum allocation sizes for transfer memory /// buffers.The application using buffered memory transfers must use a buffer size between /// MinBufSize and MaxBufSize in their TW_IMAGEMEMXFER.Memory.Length when using the /// DG_IMAGE / DAT_IMAGEMEMXFER / MSG_GET operation. Sources may return a more efficient /// preferred value in State 6 after the image size, etc. has been specified. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _SetupMemXferProcessRequest(TwMSG msg, IntPtr data) { switch (msg) { case TwMSG.Get: Marshal.StructureToPtr(new TwSetupMemXfer { MinBufSize = this.XferEnvironment.MinMemXferBufferSize, MaxBufSize = this.XferEnvironment.MaxMemXferBufferSize, Preferred = this.XferEnvironment.MemXferBufferSize }, data, true); return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// Invoked to processing a TWAIN operations (Triplets). /// </summary> /// <param name="dg">The Data Group of the operation triplet. Currently, only DG_CONTROL, DG_IMAGE, and DG_AUDIO are defined.</param> /// <param name="dat">The Data Argument Type of the operation triplet.</param> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The pData parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns> /// TWAIN Return Codes. /// </returns> /// <exception cref="DataSourceException"> /// </exception> protected override TwRC OnProcessRequest(TwDG dg, TwDAT dat, TwMSG msg, IntPtr data) { if (dg == TwDG.Image) { switch (dat) { case TwDAT.ImageLayout: return(this._ImageLayoutProcessRequest(msg, data)); case TwDAT.ImageInfo: if ((this.State & DataSourceState.Ready) != 0) { return(this._ImageInfoProcessRequest(msg, data)); } throw new DataSourceException(TwRC.Failure, TwCC.SeqError); case TwDAT.ImageNativeXfer: if ((this.State & DataSourceState.Ready) != 0) { return(this._ImageNativeXferProcessRequest(msg, data)); } throw new DataSourceException(TwRC.Failure, TwCC.SeqError); case TwDAT.ImageMemXfer: if ((this.State & DataSourceState.Ready) != 0) { return(this._ImageMemXferProcessRequest(msg, data, false)); } throw new DataSourceException(TwRC.Failure, TwCC.SeqError); case TwDAT.ImageMemFileXfer: if ((this.State & DataSourceState.Ready) != 0) { return(this._ImageMemXferProcessRequest(msg, data, true)); } throw new DataSourceException(TwRC.Failure, TwCC.SeqError); case TwDAT.ImageFileXfer: if ((this.State & DataSourceState.Ready) != 0) { return(this._ImageFileXferProcessRequest(msg)); } throw new DataSourceException(TwRC.Failure, TwCC.SeqError); } } return(base.OnProcessRequest(dg, dat, msg, data)); }
/// <summary> /// DG_CONTROL / DAT_IDENTITY / MSG_GET /// This operation triplet is generated only by the Source Manager and is sent to the Source. It returns /// the identity structure for the Source. /// /// DG_CONTROL / DAT_IDENTITY / MSG_OPENDS /// Opens the Source for operation. /// /// DG_CONTROL / DAT_IDENTITY / MSG_CLOSEDS /// Closes the Source so it can be unloaded from memory. The Source responds by doing its /// shutdown and clean-up activities needed to ensure the heap will be “clean” after the Source is /// unloaded.Under Windows, the Source will only be unloaded if the connection with the last /// application accessing it is about to be broken.The Source will know this by its internal “connect /// count” that should be maintained by any Source that supports multiple application connects. /// </summary> /// <param name="appId">This points to a TwIdentity structure.</param> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The pData parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _IdentityControlProcessRequest(TwIdentity appId, TwMSG msg, IntPtr data) { TwIdentity _identity = (TwIdentity)Marshal.PtrToStructure(data, typeof(TwIdentity)); switch (msg) { case TwMSG.Get: Marshal.StructureToPtr(this._GetDSIdentity(), data, true); return(TwRC.Success); case TwMSG.OpenDS: if (this._handlers.Count < this.MaxConnectionCount) { var _conteiner = new Saraff.IoC.ServiceContainer(); _conteiner.Bind <Saraff.IoC.IConfiguration, IoC._Configuration>(); _conteiner.Bind <IoC.IInstanceFactory>(_conteiner.CreateInstance <IoC._InstanceFactory>(i => i("container", _conteiner))); _conteiner.Bind <IoC.IBinder>(_conteiner.CreateInstance <IoC._Binder>(i => i("container", _conteiner))); _conteiner.Load(this.HandlerType.Assembly); this._conteiners.Add(appId.Id, _conteiner); var _ds = _conteiner.CreateInstance(this.HandlerType) as IDataSource; _conteiner.Bind <IDataSource>(_ds); this._handlers.Add(appId.Id, _conteiner.CreateInstance <HandlerIdentity>(i => i("appId", appId), i => i("identity", _identity))); return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.MaxConnections); case TwMSG.CloseDS: try { this._handlers[appId.Id].Dispose(); this._conteiners[appId.Id].Dispose(); } finally { this._handlers.Remove(appId.Id); this._conteiners.Remove(appId.Id); if (this._dsmEntries.ContainsKey(appId.Id)) { this._dsmEntries.Remove(appId.Id); } } return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// DG_CONTROL / DAT_EVENT / MSG_PROCESSEVENT /// This operation supports the distribution of events from the application to Sources so that the /// Source can maintain its user interface and return messages to the application.Once the /// application has enabled the Source, it must immediately begin sending to the Source all events /// that enter the application’s main event loop. This allows the Source to update its user interface in /// real-time and to return messages to the application which cause state transitions. Even if the /// application overrides the Source’s user interface, it must forward all events once the Source has /// been enabled. The Source will tell the application whether or not each event belongs to the Source. /// /// The Source should be structured such that identification of the event’s “owner” is handled before /// doing anything else. Further, the Source should return immediately if the Source isn’t the owner. /// This convention should minimize performance concerns for the application (remember, these /// events are only sent while a Source is enabled—that is, just before and just after the transfer is /// taking place). /// </summary> /// <remarks>Events only need to be forwarded to the Source while it is enabled.</remarks> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> private TwRC _EventProcessRequest(TwMSG msg, IntPtr data) { switch (msg) { case TwMSG.ProcessEvent: var _event = Marshal.PtrToStructure(data, typeof(TwEvent)) as TwEvent; var _msg = (WINMSG)Marshal.PtrToStructure(_event.EventPtr, typeof(WINMSG)); try { return(this.OnProcessEvent(Message.Create(_msg.hwnd, _msg.message, _msg.wParam, _msg.lParam))?TwRC.DSEvent:TwRC.NotDSEvent); } finally { if (_event.Message != TwMSG.Null) { _event.Message = TwMSG.Null; Marshal.StructureToPtr(_event, data, true); } } } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// Every Source is required to have a single entry point called DS_Entry. DS_Entry is only called by the Source Manager. /// </summary> /// <param name="appId"> /// This points to a TwIdentity structure, allocated by the application, that describes the /// application making the call. One of the fields in this structure, called Id, is an arbitrary and /// unique identifier assigned by the Source Manager to tag the application as a unique TWAIN /// entity. The Source Manager maintains a copy of the application’s identity structure, so the /// application must not modify that structure unless it first breaks its connection with the Source /// Manager,then reconnects to cause the Source Manager to store the new, modified identity. /// </param> /// <param name="dg">The Data Group of the operation triplet. Currently, only DG_CONTROL, DG_IMAGE, and DG_AUDIO are defined.</param> /// <param name="dat">The Data Argument Type of the operation triplet.</param> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data"> /// The pData parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet. /// </param> /// <returns>TWAIN Return Codes.</returns> public TwRC ProcessRequest(TwIdentity appId, TwDG dg, TwDAT dat, TwMSG msg, IntPtr data) { if (this.AppIdentity == null) { this.AppIdentity = appId; } if (this.AppIdentity.Id != appId.Id) { throw new DataSourceException(TwRC.Failure, TwCC.OperationError); } if (!this.SupportedDataCodes.Contains(dat)) { throw new DataSourceException(TwRC.Failure, TwCC.OperationError); } if (dg == TwDG.Control) { switch (dat) { case TwDAT.Event: return(this._EventProcessRequest(msg, data)); case TwDAT.UserInterface: return(this._UserInterfaceProcessRequest(msg, data)); case TwDAT.XferGroup: return(this._XferGroupProcessRequest(msg, data)); case TwDAT.Capability: return(this._CapabilityProcessRequest(msg, data)); case TwDAT.PendingXfers: return(this._PendingXfersProcessRequest(msg, data)); case TwDAT.SetupMemXfer: return(this._SetupMemXferProcessRequest(msg, data)); case TwDAT.SetupFileXfer: return(this._SetupFileXferProcessRequest(msg, data)); } } return(this.OnProcessRequest(dg, dat, msg, data)); }
/// <summary> /// DG_IMAGE / DAT_IMAGELAYOUT / MSG_GET /// The DAT_IMAGELAYOUT operations control information on the physical layout of the image on the /// acquisition platform of the Source(e.g.the glass of a flatbed scanner,the size of a photograph, /// etc.). /// The MSG_GET operation describes both the size and placement of the image on the scanner.The /// coordinates on the scanner and the extents of the image are expressed in the unit of measure /// currently negotiated for ICAP_UNITS(default is inches). /// /// DG_IMAGE / DAT_IMAGELAYOUT / MSG_GETDEFAULT /// This operation returns the default information on the layout of an image. This is the size and /// position of the image that will be acquired from the Source if the acquisition is started with the /// Source(and the device it is controlling) in its power-on state(for instance,most flatbed scanners /// will capture the entire bed). /// /// DG_IMAGE / DAT_IMAGELAYOUT / MSG_RESET /// This operation sets the image layout information for the next transfer to its default settings. /// /// DG_IMAGE / DAT_IMAGELAYOUT / MSG_SET /// This operation sets the layout for the next image transfer. This allows the application to specify /// the physical area to be acquired during the next image transfer(for instance,a frame-based /// application would pass to the Source the size of the frame the user selected within the /// application—the helpful Source would present a selection region already sized to match the /// layout frame size). /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> /// <exception cref="DataSourceException"> /// </exception> private TwRC _ImageLayoutProcessRequest(TwMSG msg, IntPtr data) { var _layout = Marshal.PtrToStructure(data, typeof(TwImageLayout)) as TwImageLayout; switch (msg) { case TwMSG.Get: Marshal.StructureToPtr(new TwImageLayout { Frame = this.CurrentImageLayout, DocumentNumber = 1, PageNumber = 1, FrameNumber = 1 }, data, true); return(TwRC.Success); case TwMSG.GetDefault: Marshal.StructureToPtr(new TwImageLayout { Frame = this.DefaultImageLayout, DocumentNumber = 1, PageNumber = 1, FrameNumber = 1 }, data, true); return(TwRC.Success); case TwMSG.Set: if ((this.State & DataSourceState.Enabled) != 0) { throw new DataSourceException(TwRC.Failure, TwCC.SeqError); } this.CurrentImageLayout = _layout.Frame; return(TwRC.Success); case TwMSG.Reset: if ((this.State & DataSourceState.Enabled) != 0) { throw new DataSourceException(TwRC.Failure, TwCC.SeqError); } this.CurrentImageLayout = this.DefaultImageLayout; return(TwRC.Success); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
/// <summary> /// Every Source is required to have a single entry point called DS_Entry. DS_Entry is only called by the Source Manager. /// </summary> /// <param name="appId">This points to a TwIdentity structure, allocated by the application, that describes the /// application making the call. One of the fields in this structure, called Id, is an arbitrary and /// unique identifier assigned by the Source Manager to tag the application as a unique TWAIN /// entity. The Source Manager maintains a copy of the application’s identity structure, so the /// application must not modify that structure unless it first breaks its connection with the Source /// Manager,then reconnects to cause the Source Manager to store the new, modified identity.</param> /// <param name="dg">The Data Group of the operation triplet. Currently, only DG_CONTROL, DG_IMAGE, and DG_AUDIO are defined.</param> /// <param name="dat">The Data Argument Type of the operation triplet.</param> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The pData parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> public TwRC ProcessRequest(TwIdentity appId, TwDG dg, TwDAT dat, TwMSG msg, IntPtr data) { try { if (dg == TwDG.Control) { switch (dat) { case TwDAT.Identity: return(this._IdentityControlProcessRequest(appId, msg, data)); case TwDAT.Status: return(this._StatusControlProcessRequest(appId, msg, data)); case TwDAT.EntryPoint: return(this._EntryPointControlProcessRequest(appId, msg, Marshal.PtrToStructure(data, typeof(TwEntryPoint)) as TwEntryPoint)); } } this._SetConditionCode(appId, TwCC.Success); return(this._handlers[appId.Id].ProcessRequest(dg, dat, msg, data)); } catch (DataSourceException ex) { this._SetConditionCode(appId, ex.ConditionCode); this._ToLog(ex); try { this._GetExtension <Extensions.ILog>(appId)?.Write(ex); } catch { } return(ex.ReturnCode); } catch (Exception ex) { this._ToLog(ex); try { this._GetExtension <Extensions.ILog>(appId)?.Write(ex); } catch { } } this._SetConditionCode(appId, TwCC.OperationError); return(TwRC.Failure); }
/// <summary> /// DG_IMAGE / DAT_IMAGENATIVEXFER / MSG_GET /// Causes the transfer of an image’s data from the Source to the application, via the Native transfer /// mechanism, to begin.The resulting data is stored in main memory in a single block.The data is /// stored in the Operating Systems native image format. The size of the image that can be transferred /// is limited to the size of the memory block that can be allocated by the Source.If the image is too /// large to fit,the Source may resize or crop the image. /// </summary> /// <param name="msg">The Message of the operation triplet.</param> /// <param name="data">The data parameter is a pointer to the data (a variable or, more /// typically, a structure) that will be used according to the action specified by the operation /// triplet.</param> /// <returns>TWAIN Return Codes.</returns> /// <exception cref="DataSourceException"></exception> private TwRC _ImageNativeXferProcessRequest(TwMSG msg, IntPtr data) { switch (msg) { case TwMSG.Get: using (var _stream = new MemoryStream()) { using (var _image = this.OnImageNativeXfer()) { _image.Save(_stream, ImageFormat.Bmp); } _stream.Seek(14, SeekOrigin.Begin); var _dib = new byte[_stream.Length - 14]; _stream.Read(_dib, 0, _dib.Length); var _hData = DataSourceServices.Memory.Alloc(_dib.Length); var _pData = DataSourceServices.Memory.Lock(_hData); DataSourceServices.Memory.Unlock(_hData); Marshal.Copy(_dib, 0, _pData, _dib.Length); Marshal.WriteIntPtr(data, _pData); } this.State |= DataSourceState.Transferring; return(TwRC.XferDone); } throw new DataSourceException(TwRC.Failure, TwCC.BadProtocol); }
private static extern TwRC DScap([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwSetupFileXFfer setupFileXFfer);
public static extern TwRC DSpxfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwPendingXfers pxfr);
public static extern TwRC DSilayout([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwImageLayout value);
public static extern TwRC DScap([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwCapability capa);
public static extern TwRC DSevent([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref TwEvent evt);
public static extern TwRC DSMstatus([In, Out] TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus dsmstat);
private TwRC DsImageXfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap) { using (new MessageBoxHook()) { return _DsImageXfer(origin, dest, dg, dat, msg, ref hbitmap); } }
private static extern TwRC DSevent([In, Out] TW_IDENTITY origin, [In, Out] TW_IDENTITY dest, TwDG dg, TwDAT dat, TwMSG msg, ref TW_EVENT evt);
private TwRC DsmIdent([In, Out] TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwIdentity idds) { using (new MessageBoxHook()) { return _DsmIdent(origin, zeroptr, dg, dat, msg, idds); } }
private TwRC DsStatus([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus dsmstat) { using (new MessageBoxHook()) { return _DsStatus(origin, dest, dg, dat, msg, dsmstat); } }
private TwRC DsImageLayuot([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwImageLayout imageLayuot) { using (new MessageBoxHook()) { return _DsImageLayuot(origin, dest, dg, dat, msg, imageLayuot); } }
private TwRC DsPendingXfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwPendingXfers pxfr) { using (new MessageBoxHook()) { return _DsPendingXfer(origin, dest, dg, dat, msg, pxfr); } }
private static extern TwRC DSixfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap);
private static extern TwRC DSpxfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwSetupFileXfers sfxfr);
private static extern TwRC DSixfer([In, Out] TW_IDENTITY origin, [In] TW_IDENTITY dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap);
public static extern TwRC DSMident([In, Out] TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwIdentity idds);
private static extern TwRC DSMident([In, Out] TW_IDENTITY origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TW_IDENTITY idds);
public static extern TwRC DSuserif([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwUserInterface guif);
private static extern TwRC DSMparent([In, Out] TW_IDENTITY origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr refptr);
public static extern TwRC DSstatus([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus dsmstat);
private static extern TwRC DSMstatus([In, Out] TW_IDENTITY origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TW_STATUS dsmstat);
public static extern TwRC DSiinf([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwImageInfo imginf);
private static extern TwRC DSpxfer([In, Out] TW_IDENTITY origin, [In] TW_IDENTITY dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TW_PENDINGXFERS pxfr);
public static extern TwRC DSixfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap);
private static extern TwRC DSstatus([In, Out] TW_IDENTITY origin, [In] TW_IDENTITY dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TW_STATUS dsmstat);
public static extern TwRC DScustomData([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwCustomDsData data);
private static extern TwRC DSuserif([In, Out] TW_IDENTITY origin, [In, Out] TW_IDENTITY dest, TwDG dg, TwDAT dat, TwMSG msg, TW_USERINTERFACE guif);
private static extern TwRC DSiinf([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwImageInfo imginf);
public bool ProcessEvent(TwIdentity dataSourceId, ref WINMSG message, ref TwMSG result) { TwEvent evtmsg; evtmsg.Message = 0; evtmsg.EventPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WINMSG))); Marshal.StructureToPtr(message, evtmsg.EventPtr, false); TwRC rc = LibTwain32.DSevent(fApplicationId, dataSourceId, TwDG.Control, TwDAT.Event, TwMSG.ProcessEvent, ref evtmsg); Marshal.FreeHGlobal(evtmsg.EventPtr); result = evtmsg.Message; return (bool)(rc == TwRC.DSEvent); }
private static extern TwRC DSpxfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwPendingXfers pxfr);
public static extern TwRC DSMparent([In, Out] TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr refptr);
private static extern TwRC DScap([In, Out] TW_IDENTITY origin, [In] TW_IDENTITY dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwCapability capa);
private TwRC DsCap([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwCapability capa) { using (new MessageBoxHook()) { return _DsCap(origin, dest, dg, dat, msg, capa); } }
public TwRC ProcessRequest(TwDG dg, TwDAT dat, TwMSG msg, IntPtr data) { return(this.Handler.ProcessRequest(this.Application.Identity, dg, dat, msg, data)); }
private static extern TwRC DSiinf([In, Out] TW_IDENTITY origin, [In] TW_IDENTITY dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TW_IMAGEINFO imginf);
private TwRC DsUI([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwUserInterface guif) { using (new MessageBoxHook()) { return _DsUI(origin, dest, dg, dat, msg, guif); } }