TWAIN Working Group

Newsletter Signup
Donate
Help keep TWAIN free
  • About TWAIN
    • What’s New?
    • News
    • Events
    • Membership
    • Consider a Donation
    • Contact Us
  • Why TWAIN?
  • Developers
    • Driver Developer
    • Application Developer
    • TWAIN Features
    • Specification & Tools
    • Self Certification Process
  • Support Forums
  • Scanner End-User
  • Find Certified Drivers
    • Facebook
    • LinkedIn
    • Vimeo

Get Current, Default, Available Values

Forums › TWAIN Classic › Get Current, Default, Available Values

  • This topic has 4 replies, 2 voices, and was last updated 8 years, 5 months ago by si_ben.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • July 31, 2012 at 4:53 pm #22768 Reply
    si_ben
    Participant
    • Topics - 7
    • Replies - 22
    • Total Posts - 29

    Hellow,

    Question: I do a request on a Capability, how come the Length of my ItemList is always 1?

    Struct Def :

    _
    Friend Structure TwEnumeration
    Public ItemType As Short
    Public NumItems As Integer
    Public CurrentIndex As Integer
    Public DefaultIndex As Integer
    _
    Public ItemList() As Short
    End Structure
    _________________________________________________________________________
    Fill The Structure :

    Dim pvalEnum As New TwEnumeration
    Dim NumItems As Integer
    Dim CurrentIndex As Integer
    Dim DefaultIndex As Integer
    Dim valueBool As Boolean
    Dim value As Short
    Dim index As Short

    pvalEnum = Marshal.PtrToStructure(Twain.GlobalLock(thisCap.Handle),GetType(TwEnumeration))

    NumItems = pvalEnum.NumItems
    CurrentIndex = pvalEnum.CurrentIndex
    DefaultIndex = pvalEnum.DefaultIndex

    For index = 0 To pvalEnum.ItemList.Length – 1
    If (pvalEnum.ItemType = TwType.UInt16) Then
    value = CType(pvalEnum.ItemList(index), TwType)

    ElseIf (pvalEnum.ItemType = TwType.Bool) Then
    valueBool = CBool(pvalEnum.ItemList(index))
    End If
    Next
    _________________________________________________________________________

    The Problem Is — ANYTHING I request
    DScap(appid, appid, TwDG.Control, TwDAT.Capability, TwMSG.Get, cap)
    Always returns ItemList of 1

    Help?

    July 31, 2012 at 9:11 pm #25656 Reply
    spike
    Participant
    • Topics - 10
    • Replies - 139
    • Total Posts - 149

    Looks like the problem is marshalling the ItemList.
    You declared it:
    _
    Public ItemList() As Short

    (a) I’m guessing ItemList has a length of 1 because you didn’t specify the length so VB defaults the length to 1?
    (b) that isn’t at all what’s really there in the structure.

    I’ll tell you what’s really there, and maybe you can figure out how to marshall it in VB.NET 😉

    At the position of the ItemList field in the structure E, there are E.NumItems items, of the type implied by E.ItemType.
    (I think the alignment is Pack 2 also, but I’m not 100% sure about that.)
    So if E.NumItems = 5 and E.ItemType = TWTY_INT16, then E.ItemList is an (unmanaged) array of 5 16-bit signed integers.
    If E.ItemType = TWTY_FIX32, then E.ItemList is an array of TW_FIX32 structures.

    So both the length and element type of the ItemList array are dynamically determined by the NumItems and ItemType fields.
    Note that ItemList is not a handle or pointer! It is a variable-sized field at the end of the structure.
    This is an old, hackish C idiom, and is used in several places in the TWAIN API.

    I’m at a loss to recommend how to marshall that. You could declare several classes – TwInt16Enum, TwFix32Enum, etc. to get the ItemList typed correctly, but the length of the array? Maybe this SO thread helps:
    http://stackoverflow.com/questions/5902103/how-do-i-marshal-a-struct-that-contains-a-variable-sized-array-to-c

    July 31, 2012 at 9:29 pm #25657 Reply
    si_ben
    Participant
    • Topics - 7
    • Replies - 22
    • Total Posts - 29

    This is potentially really helpful thank you… Just reading it and the site maybe helpful. I think I get it yes your right the size is ALWAYS 1.

    When I delcare it though

    Public ItemList() As Short — I cannot Declare it as
    Public ItemList(1) As Short it doesn’t let me but — this might help. I will have
    many hours tomorrow to try so thanks again, very much!

    July 31, 2012 at 9:37 pm #25658 Reply
    si_ben
    Participant
    • Topics - 7
    • Replies - 22
    • Total Posts - 29

    For Testing purposes declared a SizeConst :=10

    There was more than one Value, thanks

    I will post my more permanent solution once i find one…

    August 1, 2012 at 4:42 pm #25659 Reply
    si_ben
    Participant
    • Topics - 7
    • Replies - 22
    • Total Posts - 29

    So far my solution to this problem is just to Declare the Structures with a Constant Field large enough to hold all the values and use the NumItem to iterate through each element that has been associated with a value :

    _
    Friend Structure TwArray
    Public ItemType As Short
    Public NumItems As Integer

    _
    Public ItemList() As Short
    End Structure
    ___________________________________________________________

    ‘/** TWAIN.H Declaration **’/
    ”/* Container for array of values */
    ”typedef struct {
    ” TW_UINT16 ItemType;
    ” TW_UINT32 NumItems;
    ” TW_UINT8 ItemList[1];
    ”} TW_ARRAY, FAR * pTW_ARRAY;
    ___________________________________________________________

    The only other way I could see approaching this problem was to Send the
    Operation Twice Example :

    CONTROL / CAPABILITY / GET / cap –> Retrieving the Value of NumItems

    Creating a Sub-Routine inside the Structure Something like :

    Public Sub SetItemList(ByVal _numItems As Integer)
    ReDim ItemList(_numItems)
    End Sub

    CONTROL / CAPABILITY / GET / cap –> Retrieving the Value of NumItems
    ______________________________________________________________

    Problem with this approach is that for some reason it does not accept this
    as a Constant size for the ItemList, therefore; When the query was made again
    the ItemList size was still 1. Like you mentioned something to do with Unmanaged types.
    I didn’t consider it a big deal, just set the SizeConst:=200 since from what I can tell
    there are only 165 Defined Capabilities in the TWAIN.H File.

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Reply #25659 in Get Current, Default, Available Values
Your information:




Quick Links

Service Providers
TWAIN Support Forums
Membership
Contact Us
Privacy Policy

Newsletter Signup

TWAIN Working Group Family

TWAIN Working Group
TWAIN Direct®
TWAIN Resources
TWAIN Certified Drivers
PDF/raster

  • Facebook
  • GitHub
  • LinkedIn
  • Vimeo

Recent Topics

  • New project started to be available today, check it out
  • EPSON V600 TWAIN and WIA on Windows 10
  • When and how to use WaitForEvents command ?
  • Problem enumerating list of installed scanners in windows server 2012
  • Failed to create TWAIN progress! Error code is 1260.
  • Quarterly Newsletter
  • TWAIN Working Group Membership
  • Logo Usage
  • TWAIN License
  • Contact Us
Privacy Policy • Privacy Tools • Copyright © 2021 TWAIN Working Group • by iHwy, LLC • Log in

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.