/*
         * public TwCapability(TwCap cap, short sval)
         * {
         *  Cap = (short)cap;
         *  ConType = (short)TwOn.One;
         *  Handle = Twain.GlobalAlloc(0x42, 6);
         *  IntPtr pv = Twain.GlobalLock(Handle);
         *  Marshal.WriteInt16(pv, 0, (short)TwType.Int16);
         *  Marshal.WriteInt32(pv, 2, (int)sval);
         *  Twain.GlobalUnlock(Handle);
         * }
         */
        public TwCapability(TwCap cap, TwFix32 val)
        {
            Cap     = (short)cap;
            ConType = (short)TwOn.One;
            Handle  = Twain.GlobalAlloc(0x42, 6);
            IntPtr pv = Twain.GlobalLock(Handle);

            Marshal.WriteInt16(pv, 0, (short)TwType.UInt16);
            Marshal.WriteInt32(pv, 2, (int)val.Whole);
            Marshal.WriteInt64(pv, 4, (long)val.Whole);
            Twain.GlobalUnlock(Handle);
        }
        public TwRC AcquireLexmark(short CountPags = 200, short ShowUI = 0, short ModalUI = 0, bool isDuplex = false)
        {
            TwRC rc;

            CloseSrc();
            if (appid.Id == IntPtr.Zero)
            {
                Init(hwnd);
                if (appid.Id == IntPtr.Zero)
                {
                    return(TwRC.Failure);
                }
            }
            rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, srcds);
            if (rc != TwRC.Success)
            {
                ACSLog.InsertLog(MessageBoxIcon.Information, "Sem comunicação com o Scanner: Primeira tentativa - " + rc.ToString());
                return(rc);
            }

            //Quantidade de Imagens a Capturar
            TwFix32 f32 = new TwFix32();

            f32.FromFloat(CountPags);
            TwCapability cap = new TwCapability(TwCap.XferCount, f32);

            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);

            if (rc != TwRC.Success)
            {
                ACSLog.InsertLog(MessageBoxIcon.Information, "Sem comunicação com o Scanner: Quantidade de imagens - " + rc.ToString());
                CloseSrc();
                return(rc);
            }

            //Scanner como...
            f32 = new TwFix32();
            f32.FromFloat(this.ScanAs);
            TwCapability capPixelType = new TwCapability(TwCap.IPixelType, f32);

            //rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Get, capPixelType);
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capPixelType);

            if (rc != TwRC.Success)
            {
                ACSLog.InsertLog(MessageBoxIcon.Information, "Sem comunicação com o Scanner: Cor da captura - " + rc.ToString());
                CloseSrc();
                return(rc);
            }

            //Resolução Imagem
            f32 = new TwFix32();
            f32.FromFloat(this.Resolution);//value of DPI
            var capResolution = new TwCapability(TwCap.IXResolution, f32);

            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.GetCurrent, capResolution);
            if (rc != TwRC.Success)
            {
                ACSLog.InsertLog(MessageBoxIcon.Information, "Sem comunicação com o Scanner: Resolução da Imagem - " + rc.ToString());
                CloseSrc();
                return(rc);
            }

            //Brightness
            short TWON_ONEVALUE = 5;

            f32 = new TwFix32();
            f32.FromFloat(this.Brightness * 10);
            var capBrightness = new TwCapability(TwCap.ICAP_BRIGHTNESS, f32);

            capBrightness.ConType = TWON_ONEVALUE;
            //capBrightness.Handle = hwnd;
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capBrightness);

            //Contrast
            f32 = new TwFix32();
            f32.FromFloat(this.Contrast * 10);
            var capContrast = new TwCapability(TwCap.ICAP_CONTRAST, f32);

            capContrast.ConType = TWON_ONEVALUE;
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capContrast);

            // duplex
            if (isDuplex)
            {
                f32 = new TwFix32();
                f32.FromFloat(-2);
                var XferCount = new TwCapability(TwCap.XferCount, f32);

                rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, XferCount);

                f32 = new TwFix32();
                f32.FromFloat(1);
                var capDuplexEnable = new TwCapability(TwCap.DuplexEnabled, f32);

                rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplexEnable);

                f32 = new TwFix32();
                f32.FromFloat(1);
                var capDuplex = new TwCapability(TwCap.Duplex, f32);
                capDuplex.ConType = 5;
                rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplex);
            }
            else
            {
                f32 = new TwFix32();
                f32.FromFloat(-2);
                var XferCount = new TwCapability(TwCap.XferCount, f32);

                rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, XferCount);

                f32 = new TwFix32();
                f32.FromFloat(0);
                var capDuplexEnable = new TwCapability(TwCap.DuplexEnabled, f32);

                rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplexEnable);

                f32 = new TwFix32();
                f32.FromFloat(0);
                var capDuplex = new TwCapability(TwCap.Duplex, f32);
                capDuplex.ConType = 5;
                rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplex);
            }


            TwUserInterface guif = new TwUserInterface();

            guif.ShowUI     = ShowUI;
            guif.ModalUI    = ModalUI;
            guif.ParentHand = hwnd;
            rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif);
            if (rc != TwRC.Success)
            {
                if (rc == TwRC.CheckStatus)
                {
                    // CloseSrc();
                    return(TwRC.Success);
                }
                ACSLog.InsertLog(MessageBoxIcon.Information, "Sem comunicação com o Scanner: Última verificação - " + rc.ToString());
                CloseSrc();
                return(rc);
            }

            return(rc);
        }