public TwainProxy(ITwainApplication app)
        {
            this.disposed = false;
            this.state    = TwainState.PreSession;

            this.availableSources   = new List <TwainStructs.Identity>();
            this.defaultSourceIndex = -1;
            this.latestDIB          = IntPtr.Zero;

            this.appId             = new TwainStructs.Identity();
            appId.Id               = 0U;
            appId.Version.MajorNum = 1;
            appId.Version.MinorNum = 0;
            appId.Version.Country  = TwainConstants.TWCY_USA;
            appId.Version.Language = TwainConstants.TWLG_ENGLISH_USA;
            appId.Version.Info     = string.Format("TwainProxy v{0}", typeof(TwainProxy).Assembly.GetName().Version.ToString(4));
            appId.Manufacturer     = "null54";
            appId.ProductFamily    = "TwainablePlus";
            appId.ProductName      = "TwainProxy";
            appId.ProtocolMajor    = TwainConstants.ProtocolMajor;
            appId.ProtocolMinor    = TwainConstants.ProtocolMinor;
            appId.SupportedGroups  = (uint)(DataGroup.Control | DataGroup.Image);

            this.deviceId           = new TwainStructs.Identity();
            this.parentWindowHandle = app.WindowHandle;
            this.hostApp            = app;
        }
        private void EnumAvailableSources()
        {
            if (OpenSourceManager())
            {
                TwainStructs.Identity defaultSource = new TwainStructs.Identity();

                ResultCode rc = DSMIdentity(DataGroup.Control, TwainMessages.GetDefault, ref defaultSource);

                if (rc == ResultCode.Success)
                {
                    TwainStructs.Identity source = new TwainStructs.Identity();

                    rc = DSMIdentity(DataGroup.Control, TwainMessages.GetFirst, ref source);

                    if (rc == ResultCode.Success)
                    {
                        do
                        {
                            this.availableSources.Add(source);
                            rc = DSMIdentity(DataGroup.Control, TwainMessages.GetNext, ref source);
                        } while (rc == ResultCode.Success);

                        this.availableSources.Sort(new TwainIdentityComparer());
                        this.defaultSourceIndex = this.availableSources.FindIndex(i => i.ProductName == defaultSource.ProductName);
                    }
                }
            }
        }
        public void SetSelectedSource(int index)
        {
            if (state == TwainState.SourceManagerOpen)
            {
                this.deviceId = this.availableSources[index];

                if (index != defaultSourceIndex)
                {
                    ResultCode rc = DSMIdentity(DataGroup.Control, TwainMessages.Set, ref deviceId);

#if DEBUG
                    if (rc == ResultCode.Failure)
                    {
                        TwainStructs.Status status = new TwainStructs.Status();
                        DSMStatus(ref status);

                        System.Diagnostics.Debug.WriteLine(status.ConditionCode);
                    }
#endif
                }
            }
        }
        private ResultCode DSMIdentity(DataGroup group, ushort msg, ref TwainStructs.Identity ident)
        {
            ResultCode rc = twainIdentity(ref appId, IntPtr.Zero, group, DataArgumentType.Identity, msg, ref ident);

            return(rc);
        }