private unsafe bool SetCapOneValue(ushort capability, DataTypes type, uint value)
        {
            ResultCode rc = ResultCode.Failure;

            if (state >= TwainState.SourceOpen)
            {
                TwainStructs.Capability cap = new TwainStructs.Capability();
                cap.Cap     = capability;
                cap.ConType = ContainerType.OneValue;

                cap.hContainer = AllocateMemory(Marshal.SizeOf(typeof(TwainStructs.OneValue)));
                try
                {
                    if (cap.hContainer != IntPtr.Zero)
                    {
                        IntPtr ptr = LockMemory(cap.hContainer);

                        TwainStructs.OneValue *data = (TwainStructs.OneValue *)ptr.ToPointer();
                        data->ItemType = type;
                        data->Item     = value;

                        UnlockMemory(cap.hContainer);

                        rc = twainCapability(ref appId, ref deviceId, DataGroup.Control, DataArgumentType.Capability, TwainMessages.Set, ref cap);

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

                            System.Diagnostics.Debug.Assert(src == ResultCode.Success);
                        }
                    }
                }
                finally
                {
                    if (cap.hContainer != IntPtr.Zero)
                    {
                        FreeMemory(cap.hContainer);
                        cap.hContainer = IntPtr.Zero;
                    }
                }
            }

            return(rc == ResultCode.Success);
        }
        private unsafe bool GetCapOneValue(ushort capability, DataTypes type, out object value)
        {
            value = null;
            ResultCode rc = ResultCode.Failure;

            if (state >= TwainState.SourceOpen)
            {
                TwainStructs.Capability cap = new TwainStructs.Capability();
                cap.Cap        = capability;
                cap.ConType    = ContainerType.OneValue;
                cap.hContainer = IntPtr.Zero;

                rc = twainCapability(ref appId, ref deviceId, DataGroup.Control, DataArgumentType.Capability, TwainMessages.GetCurrent, ref cap);

                try
                {
                    if (cap.hContainer != IntPtr.Zero)
                    {
                        IntPtr ptr = LockMemory(cap.hContainer);

                        if (cap.ConType == ContainerType.OneValue)
                        {
                            TwainStructs.OneValue *pcon = (TwainStructs.OneValue *)ptr.ToPointer();

                            switch (pcon->ItemType)
                            {
                            case DataTypes.Bool:
                                value = pcon->Item != 0U;
                                break;

                            case DataTypes.Int16:
                                value = (short)pcon->Item;
                                break;

                            case DataTypes.UInt16:
                                value = (ushort)pcon->Item;
                                break;

                            case DataTypes.Int32:
                                value = (int)value;
                                break;

                            case DataTypes.UInt32:
                                value = pcon->Item;
                                break;
                            }
                        }

                        UnlockMemory(cap.hContainer);


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

                            System.Diagnostics.Debug.Assert(src == ResultCode.Success);
                        }
                    }
                }
                finally
                {
                    if (cap.hContainer != IntPtr.Zero)
                    {
                        FreeMemory(cap.hContainer);
                        cap.hContainer = IntPtr.Zero;
                    }
                }
            }

            return(rc == ResultCode.Success);
        }