public DataSource(TwainDevice twain, TwIdentity identity)
 {
   fTwain = twain;
   fIdent = identity;
   fState = StateType.Closed;
   fSettings = null;
   fCapabilities = null;
 }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Twain32"/> class.
        /// </summary>
        public Twain32() {
			
            var _window=new Form();
            this._hwnd=_window.Handle;
	       
            Assembly _asm=Assembly.GetExecutingAssembly();
            AssemblyName _asm_name=new AssemblyName(_asm.FullName);
            Version _version=new Version(((AssemblyFileVersionAttribute)_asm.GetCustomAttributes(typeof(AssemblyFileVersionAttribute),false)[0]).Version);

            this._appid=new TwIdentity() {
                Id = IntPtr.Zero,
                Version = new TwVersion()
                {
                    MajorNum = (short)_version.Major,
                    MinorNum = (short)_version.Minor,
                    Language = TwLanguage.USA,
                    Country = TwCountry.USA,
                    Info = _asm_name.Version.ToString()
                },
                ProtocolMajor = TwProtocol.Major,
                ProtocolMinor = TwProtocol.Minor,
                SupportedGroups = (int)(TwDG.Image | TwDG.Control),
                Manufacturer = ((AssemblyCompanyAttribute)_asm.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false)[0]).Company,
                ProductFamily = "TWAIN Class Library",
                ProductName = ((AssemblyProductAttribute)_asm.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0]).Product
            };

            this._srcds=new TwIdentity();
            this._srcds.Id=IntPtr.Zero;
            this._evtmsg.EventPtr=Marshal.AllocHGlobal(Marshal.SizeOf(this._winmsg));

            #region Загружаем twain_32.dll, получаем адрес точки входа DSM_Entry и приводим ее к соответствующим делегатам

            this._hTwainDll=LoadLibrary(string.Format("{0}\\..\\twain_32.dll",Environment.SystemDirectory));
            if(this._hTwainDll!=IntPtr.Zero) {
                IntPtr _pDsmEntry=GetProcAddress(this._hTwainDll,1);
                if(_pDsmEntry!=IntPtr.Zero) {
                    this._DsmParent=(_DSMparent)Marshal.GetDelegateForFunctionPointer(_pDsmEntry,typeof(_DSMparent));
                    this._DsmIdent=(_DSMident)Marshal.GetDelegateForFunctionPointer(_pDsmEntry,typeof(_DSMident));
                    this._DsUI=(_DSuserif)Marshal.GetDelegateForFunctionPointer(_pDsmEntry,typeof(_DSuserif));
                    this._DsStatus=(_DSstatus)Marshal.GetDelegateForFunctionPointer(_pDsmEntry,typeof(_DSstatus));
                    this._DsCap=(_DScap)Marshal.GetDelegateForFunctionPointer(_pDsmEntry,typeof(_DScap));
                    this._DsImageXfer=(_DSixfer)Marshal.GetDelegateForFunctionPointer(_pDsmEntry,typeof(_DSixfer));
                    this._DsPendingXfer=(_DSpxfer)Marshal.GetDelegateForFunctionPointer(_pDsmEntry,typeof(_DSpxfer));
                    this._DsImageLayuot = (_DSimagelayuot)Marshal.GetDelegateForFunctionPointer(_pDsmEntry, typeof(_DSimagelayuot));
                } else {
                    throw new TwainException("Cann't find DSM_Entry entry point.");
                }
            } else {
                throw new TwainException("Cann't load library twain_32.dll");
            }

            #endregion
        }
      public TwainDevice(IntPtr windowHandle)
      {
        fWindowHandle = windowHandle;

        fApplicationId = new TwIdentity();
        fApplicationId.Id = IntPtr.Zero;
        fApplicationId.Version.MajorNum = 1;
        fApplicationId.Version.MinorNum = 1;
        fApplicationId.Version.Language = Language.USA;
        fApplicationId.Version.Country = Country.USA;
        fApplicationId.Version.Info = "TwainLibrary";
        fApplicationId.ProtocolMajor = TwProtocol.Major;
        fApplicationId.ProtocolMinor = TwProtocol.Minor;
        fApplicationId.SupportedGroups = (int)(TwDG.Image | TwDG.Control);
        fApplicationId.Manufacturer = "TwainLibrary";
        fApplicationId.ProductFamily = "TwainLibrary";
        fApplicationId.ProductName = "TwainLibrary";
      }
        /// <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);
        }
 public bool StartDataSession(TwIdentity dataSourceId, bool showDialog, bool showTransfer)
 {
   TwUserInterface guif = new TwUserInterface();
   guif.ShowUI = (short)(showDialog ? 1 : 0);
   guif.ModalUI = 1;
   guif.ParentHand = showTransfer ? fWindowHandle : IntPtr.Zero;
   TwRC rc = LibTwain32.DSuserif(fApplicationId, dataSourceId, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif);
   return (bool)(rc == TwRC.Success);
 }
 public TwStatus GetDataSourceStatus(TwIdentity dataSourceId)
 {
   TwStatus result = new TwStatus();
   TwRC rc = LibTwain32.DSstatus(fApplicationId, dataSourceId, TwDG.Control, TwDAT.Status, TwMSG.Get, result);
   return (rc == TwRC.Success) ? result : null;
 }
示例#7
0
 internal static extern TwRC DsEntryCapability([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwCapability twcapability);
 public bool SetDataSourceCapability(TwIdentity dataSourceId, TwCapability cap)
 {
   TwRC rc = LibTwain32.DScap(fApplicationId, dataSourceId, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
   return (bool)(rc == TwRC.Success);
 }
 public TwIdentity GetFirstDataSource()
 {
   TwIdentity result = new TwIdentity();
   TwRC rc = LibTwain32.DSMident(fApplicationId, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.GetFirst, result);
   return (rc == TwRC.Success) ? result : null;
 }
示例#10
0
 public TwRC DsmStatus([In, Out] TwIdentity origin, IntPtr zeropt, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus dsmstat)
 {
     return(DsmEntryStatus(origin, zeropt, dg, dat, msg, dsmstat));
 }
 public bool DiscardPendingTransfers(TwIdentity dataSourceId, TwPendingXfers result)
 {
   TwRC rc = LibTwain32.DSpxfer(fApplicationId, dataSourceId, TwDG.Control, TwDAT.PendingXfers, TwMSG.Reset, result);
   return (bool)(rc == TwRC.Success);
 }
示例#12
0
 public TwRC DsmParent([In, Out] TwIdentity origin, IntPtr zeropt, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hwnd)
 {
     return(DsmEntryParent(origin, zeropt, dg, dat, msg, ref hwnd));
 }
示例#13
0
 public TwRC DsmIdentity([In, Out] TwIdentity origin, IntPtr zeropt, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwIdentity twidentity)
 {
     return(DsmEntryIdentity(origin, zeropt, dg, dat, msg, twidentity));
 }
示例#14
0
 internal static extern TwRC DsEntryPendingxfers([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwPendingXfers pendingxfer);
示例#15
0
 internal static extern TwRC DsEntryImagexfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap);
示例#16
0
 internal static extern TwRC DsEntryImageInfo([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwImageInfo imgeinfo);
 public TwImageInfo GetImageInfo(TwIdentity dataSourceId)
 {
   TwImageInfo result = new TwImageInfo();
   TwRC rc = LibTwain32.DSiinf(fApplicationId, dataSourceId, TwDG.Image, TwDAT.ImageInfo, TwMSG.Get, result);
   return (rc == TwRC.Success) ? result : null;
 }
示例#18
0
 public TwRC DsUserinterface([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwUserInterface twuserinterface)
 {
     return(DsEntryUserinterface(origin, dest, dg, dat, msg, twuserinterface));
 }
 public TwImageLayout GetImageLayout(TwIdentity dataSourceId)
 {
   TwImageLayout result = new TwImageLayout();
   TwRC rc = LibTwain32.DSilayout(fApplicationId, dataSourceId, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, result);
   return (rc == TwRC.Success) ? result : null;
 }
示例#20
0
 public TwRC DsEvent([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref TwEvent twevent)
 {
     return(DsEntryEvent(origin, dest, dg, dat, msg, ref twevent));
 }
 public bool ProcessEvent(TwIdentity dataSourceId, ref TwEvent result)
 {
   TwRC rc = LibTwain32.DSevent(fApplicationId, dataSourceId, TwDG.Control, TwDAT.Event, TwMSG.ProcessEvent, ref result);
   return (bool)(rc != TwRC.NotDSEvent);
 }
示例#22
0
 public TwRC DsStatus([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus twstatus)
 {
     return(DsEntryStatus(origin, dest, dg, dat, msg, twstatus));
 }
示例#23
0
 internal static extern TwRC DsEntryEvent([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref TwEvent twevent);
 public bool SetDataSourceCapability(TwIdentity dataSourceId, TwCap capType, TwType valueType, object value)
 {
   TwCapability cap = new TwCapability(capType, valueType, value);
   return SetDataSourceCapability(dataSourceId, cap);
 }
 public bool CloseDataSource(TwIdentity dataSourceId)
 {
   TwRC rc = LibTwain32.DSMident(fApplicationId, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.CloseDS, dataSourceId);
   return (bool)(rc == TwRC.Success);
 }
 public bool GetDataSourceAvailableCapabilityValues(TwIdentity dataSourceId, TwCapability result)
 {
   TwRC rc = LibTwain32.DScap(fApplicationId, dataSourceId, TwDG.Control, TwDAT.Capability, TwMSG.Get, result);
   return (bool)(rc == TwRC.Success);
 }
示例#27
0
 public TwRC DsCapability([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwCapability twcapability)
 {
     return(DsEntryCapability(origin, dest, dg, dat, msg, twcapability));
 }
示例#28
0
 internal static extern TwRC DsEntryStatus([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus twstatus);
      public object GetDataSourceCapability(TwIdentity dataSourceId, TwCap capType)
      {
        object result = null;
        TwCapability cap = new TwCapability(capType);

        if(GetDataSourceCapability(dataSourceId, cap))
        {
          result = cap.GetCurrentValue();
        }
        return result;
      }
示例#30
0
 public TwRC DsImageInfo(TwIdentity origin, TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwImageInfo imageinfo)
 {
     return(DsEntryImageInfo(origin, dest, dg, dat, msg, imageinfo));
 }
 public bool GetDataSourceStatus(TwIdentity dataSourceId, TwStatus result)
 {
   TwRC rc = LibTwain32.DSstatus(fApplicationId, dataSourceId, TwDG.Control, TwDAT.Status, TwMSG.Get, result);
   return (bool)(rc == TwRC.Success);
 }
示例#32
0
 public TwRC DsImagexfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap)
 {
     return(DsEntryImagexfer(origin, dest, dg, dat, msg, ref hbitmap));
 }
 public TwCustomDsData GetDataSourceCustomData(TwIdentity dataSourceId)
 {
   TwCustomDsData result = new TwCustomDsData();
   TwRC rc = LibTwain32.DScustomData(fApplicationId, dataSourceId, TwDG.Control, TwDAT.Status, TwMSG.Get, result);
   return (rc == TwRC.Success) ? result : null;
 }
示例#34
0
 public TwRC DsPendingxfers([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwPendingXfers pendingxfer)
 {
     return(DsEntryPendingxfers(origin, dest, dg, dat, msg, pendingxfer));
 }
 public bool FinishDataSession(TwIdentity dataSourceId)
 {
   TwUserInterface guif = new TwUserInterface(); // Contents are not used during DisableDS
   TwRC rc = LibTwain32.DSuserif(fApplicationId, dataSourceId, TwDG.Control, TwDAT.UserInterface, TwMSG.DisableDS, guif);
   return (bool)(rc == TwRC.Success);
 }
示例#36
0
 internal static extern TwRC DsmEntryParent([In, Out] TwIdentity origin, IntPtr zeropt, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hwnd);
 public bool GetImageLayout(TwIdentity dataSourceId, TwImageLayout result)
 {
   TwRC rc = LibTwain32.DSilayout(fApplicationId, dataSourceId, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, result);
   return (bool)(rc == TwRC.Success);
 }
示例#38
0
 internal static extern TwRC DsmEntryIdentity([In, Out] TwIdentity origin, IntPtr zeropt, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwIdentity twidentity);
 public bool TransferImage(TwIdentity dataSourceId, ref IntPtr hresultBitmap)
 {
   TwRC rc = LibTwain32.DSixfer(fApplicationId, dataSourceId, TwDG.Image, TwDAT.ImageNativeXfer, TwMSG.Get, ref hresultBitmap);
   return (bool)(rc == TwRC.XferDone);
 }
示例#40
0
 internal static extern TwRC DsmEntryStatus([In, Out] TwIdentity origin, IntPtr zeropt, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus dsmstat);
 public TwPendingXfers DiscardPendingTransfers(TwIdentity dataSourceId)
 {
   TwPendingXfers result = new TwPendingXfers();
   TwRC rc = LibTwain32.DSpxfer(fApplicationId, dataSourceId, TwDG.Control, TwDAT.PendingXfers, TwMSG.Reset, result);
   return (rc == TwRC.Success) ? result : null;
 }
示例#42
0
 internal static extern TwRC DsEntryUserinterface([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwUserInterface twuserinterface);
      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);
      }
示例#44
0
 /// <summary>
 /// Получает описание всех доступных источников данных.
 /// </summary>
 private void _GetAllSorces() {
     List<TwIdentity> _src=new List<TwIdentity>();
     TwIdentity _item=new TwIdentity();
     TwRC _rc=this.DsmIdent(this._appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.GetFirst, _item);
     if(_rc==TwRC.Success) {
         _src.Add(_item);
         do {
             _item=new TwIdentity();
             _rc=this.DsmIdent(this._appid,IntPtr.Zero,TwDG.Control,TwDAT.Identity,TwMSG.GetNext,_item);
             if(_rc==TwRC.Success) {
                 _src.Add(_item);
             }
         } while(_rc!=TwRC.EndOfList);
         _rc=this.DsmIdent(this._appid,IntPtr.Zero,TwDG.Control,TwDAT.Identity,TwMSG.GetDefault,this._srcds);
     } else {
         TwCC _state=this._GetStatus();
     }
     this._sorces=_src.ToArray();
 }
 public bool GetNextDataSource(TwIdentity result)
 {
   TwRC rc = LibTwain32.DSMident(fApplicationId, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.GetNext, result);
   return (bool)(rc == TwRC.Success);
 }
      public TwainCapability(TwainDevice twain, TwIdentity dataSourceId, TwCap capType)
      {
        fTwain = twain;
        fDataSourceId = dataSourceId;
        fCapType = capType;
        fValueType = TwType.DontCare16;

        TwCapability cap = new TwCapability(capType);

        if(fTwain.GetDataSourceAvailableCapabilityValues(dataSourceId, cap))
        {
          fValueType = cap.GetValueType();

          Items = new List<object>();

          int numItems = cap.GetNumItems();

          for(int i = 0; i < numItems; i++)
          {
            Items.Add(cap.GetItem(i));
          }

          MinValue = cap.GetMinValue();
          MaxValue = cap.GetMaxValue();
          StepSize = cap.GetStepSize();
          fCurrentValue = cap.GetCurrentValue();
        }
      }