Пример #1
0
        /// <summary>
        /// Reads the value from a <see cref="TWCapability" /> that was returned
        /// from a TWAIN source.
        /// </summary>
        /// <param name="capability">The capability.</param>
        /// <param name="memoryManager">The memory manager.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// capability
        /// or
        /// memoryManager
        /// </exception>
        /// <exception cref="System.ArgumentException">capability</exception>
        public static CapabilityReader ReadValue(TWCapability capability, IMemoryManager memoryManager)
        {
            if (capability == null)
            {
                throw new ArgumentNullException("capability");
            }
            if (memoryManager == null)
            {
                throw new ArgumentNullException("memoryManager");
            }

            if (capability.Container != IntPtr.Zero)
            {
                IntPtr baseAddr = IntPtr.Zero;
                try
                {
                    baseAddr = memoryManager.Lock(capability.Container);
                    switch (capability.ContainerType)
                    {
                    case ContainerType.Array:
                        return(new CapabilityReader
                        {
                            ContainerType = capability.ContainerType,
                        }.ReadArrayValue(baseAddr));

                    case ContainerType.Enum:
                        return(new CapabilityReader
                        {
                            ContainerType = capability.ContainerType,
                        }.ReadEnumValue(baseAddr));

                    case ContainerType.OneValue:
                        return(new CapabilityReader
                        {
                            ContainerType = capability.ContainerType,
                        }.ReadOneValue(baseAddr));

                    case ContainerType.Range:
                        return(new CapabilityReader
                        {
                            ContainerType = capability.ContainerType,
                        }.ReadRangeValue(baseAddr));

                    default:
                        throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
                                                                  Resources.CapHasBadContainer, capability.Capability, capability.ContainerType), "capability");
                    }
                }
                finally
                {
                    if (baseAddr != IntPtr.Zero)
                    {
                        //memoryManager.Unlock(baseAddr);
                        memoryManager.Unlock(capability.Container);
                    }
                }
            }
            else
            {
                return(new CapabilityReader());
            }
        }