This project has moved and is read-only. For the latest updates, please go here.

InvalidCastException when retrieving SupportedCaps capability

Aug 11, 2016 at 12:01 PM
I'm using Saraff version, 1.0.20.517, with an HP Scanjet 8200 with official driver.

I am iterating on capabilities in order to retrieve the supported ones. In order to test if it is supporte I'm using:
twain32.IsCapSupported(TwCap.SupportedCaps)
I am getting:
InvalidCastException: Cannot cast from Saraff.Twain.__TwArray to Saraff.Twain.TwOneValue.
Is this expected?
Aug 11, 2016 at 5:56 PM
Edited Aug 11, 2016 at 7:23 PM
Hello, jruizaranguren.
According to the specification, the operation DG_CONTROL / DAT_CAPABILITY / MSG_QUERYSUPPORT must return the container of a TWON_ONEVALUE type.

DG_CONTROL / DAT_CAPABILITY / MSG_QUERYSUPPORT

Description

Returns the Source’s support status of this capability.

Application

Set the pCapability fields as follows:
pCapability->Cap = the CAP_xxxx or ACAP_xxxx or ICAP_xxxx identifier
pCapability->ConType = TWON_ONEVALUE
pCapability->hContainer = NULL
The Source will allocate the memory for the necessary container structure but the application must
free it when the operation is complete and the application no longer needs to maintain the
information.
Use MSG_QUERYSUPPORT:
  • To check the whether the Source supports a particular operation on the capability.
    This operation may fail for a low memory condition. Either recover from a TWCC_LOWMEMORY
    failure by freeing memory for the Source to use so it can continue, or terminating the acquisition
    and notifying the user of the low memory problem.

Source

Fill the fields in pCapability as follows:
pCapability->ConType = TWON_ONEVALUE
pCapability->hContainer = TW_HANDLE referencing a container of type TW_ONEVALUE.
Fill the fields in TW_ONEVALUE as follows:
  1. ItemType = TWTW_INT32;
  2. Item = Bit pattern representing the set of operation that are supported by the Data Source on
    this capability (TWQC_GET, TWQC_SET, TWQC_GETCURRENT, TWQC_GETDEFAULT,
    TWQC_RESET, TWQC_SETCONSTRAINT, TWQC_CONSTRAINABLE);
TWAIN 2.2 Specification 7-25


Moreover, in the CAP_SUPPORTEDCAPS description specified that the MSG_QUERYSUPPORT operation must return the TW_ONEVALUE.

CAP_SUPPORTEDCAPS

Description

Returns a list of all the capabilities for which the Source will answer inquiries. Does not indicate
which capabilities the Source will allow to be set by the application. Some capabilities can only be
set if certain setup work has been done so the Source cannot globally answer which capabilities
are “set-able.”

Containers

  • MSG_GET: TW_ARRAY
  • MSG_GETCURRENT: TW_ARRAY
  • MSG_GETDEFAULT: TW_ARRAY
  • MSG_SET: Not Allowed
  • MSG_SETCONSTRAINT: Not Allowed
  • MSG_RESET: Not Allowed
  • MSG_QUERYSUPPORT: TW_ONEVALUE
TWAIN 2.2 Specification 10-91


Thus, the problem in the HP Scanjet 8200 scanner driver.
Marked as answer by SARAFF on 8/12/2016 at 8:53 AM
Aug 12, 2016 at 7:23 AM
Thank you very much for the answer (and awesome library).
Aug 26, 2016 at 7:32 AM
I received the same error on a Kyocera TASKalfa 4551ci so I decided to review the issue. Both HP Scanjet 8200 and Kyocera TASKalfa 4551ci are not Twain 2 drivers, so the 2.2 spec does not apply here.

Previous versions of the specification (at least 1.9) do not mention the MSG_QUERYSUPPORT: TW_ONEVALUE.

SupportedCaps can be retrieved using the Twacker tool for both scanners without major problems, so I guess there is an alterante way to retrieve the capabilities that works:
ItemType=TWTY_UINT16
ItemList=CAP_XFERCOUNT
ItemList=CAP_SUPPORTEDCAPS
ItemList=CAP_UICONTROLLABLE
ItemList=ICAP_COMPRESSION
ItemList=ICAP_PLANARCHUNKY
ItemList=ICAP_PHYSICALHEIGHT
ItemList=ICAP_PHYSICALWIDTH
ItemList=ICAP_PIXELFLAVOR
ItemList=ICAP_BITDEPTH
ItemList=ICAP_BITORDER
ItemList=ICAP_PIXELTYPE
ItemList=ICAP_UNITS
ItemList=ICAP_XFERMECH
ItemList=ICAP_XRESOLUTION
ItemList=ICAP_YRESOLUTION
ItemList=ICAP_IMAGEFILEFORMAT
ItemList=CAP_INDICATORS
ItemList=CAP_ENABLEDSUIONLY
ItemList=CAP_DEVICEONLINE
ItemList=ICAP_XNATIVERESOLUTION
ItemList=ICAP_YNATIVERESOLUTION
ItemList=ICAP_BRIGHTNESS
ItemList=ICAP_CONTRAST
ItemList=ICAP_XSCALING
ItemList=ICAP_YSCALING
ItemList=ICAP_THRESHOLD
ItemList=CAP_FEEDERENABLED
ItemList=CAP_FEEDERLOADED
ItemList=CAP_AUTOFEED
I get the same structure for the Virtual Twain 2.2 scanner.
Aug 26, 2016 at 4:19 PM
Edited Aug 26, 2016 at 4:22 PM
Hello, jruizaranguren.
I think you mistaken, because a DG_CONTROL / DAT_CAPABILITY / MSG_QUERYSUPPORT operation must return a bit flags value of a TWQC_xxxx.
Bellow you can see result of DG_CONTROL / DAT_CAPABILITY / MSG_QUERYSUPPORT (for CAP_SUPPORTEDCAPS) operation for a "TWAIN FreeImage Software Scanner" (from a Twacker).
ItemType=TWTY_INT32
Item=TWQC_GET |TWQC_GETDEFAULT |TWQC_GETCURRENT 

DG_CONTROL / DAT_CAPABILITY / MSG_QUERYSUPPORT

Description

Returns the Source’s support status of this capability.

Application

Set the pCapability fields as follows:
pCapability->Cap = the CAP_xxxx or ACAP_xxxx or ICAP_xxxx identifier
pCapability->ConType = TWON_ONEVALUE
pCapability->hContainer = NULL
The Source will allocate the memory for the necessary container structure but the application
must free it when the operation is complete and the application no longer needs to maintain the
information.
Use MSG_QUERYSUPPORT:
  • To check the whether the Source supports a particular operation on the capability.
This operation may fail for a low memory condition. Either recover from a
TWCC_LOWMEMORY failure by freeing memory for the Source to use so it can continue, or
terminating the acquisition and notifying the user of the low memory problem.

Source

Fill the fields in pCapability as follows:
pCapability->ConType = TWON_ONEVALUE
pCapability->hContainer = TW_HANDLE referencing a container of type TW_ONEVALUE.
TWAIN 1.9 Specification 7-151