Re: handling return codes from CTYPES

Discussion in 'Python' started by Mike C. Fletcher, Jan 21, 2013.

  1. On 13-01-21 05:46 AM, Steve Simmons wrote:

    ....
    > >>> from ctypes import *
    > >>> sLib = cdll.slib
    > >>> lic_key = c_char_p("asdfghjkl".encode(encoding='utf_8',

    > errors='strict'))
    > >>> initResult = sLib.InitScanLib(lic_key.value)
    > >>> print("InitScanLib Result: ", initResult)

    > InitScanLib Result: 65535
    > >>>

    >
    > I've tried declaring initResult as c_short by: inserting...
    >
    > >>> initResult = c_short(0)

    >
    > ... before the call to sLib.InitScanLib but I still get the same
    > response (65535).

    That's because you've just discarded the object you created.

    What you wanted was, I believe:

    initScanLib = sLib.InitScanLib
    initScanLib.restype = c_short

    initResult = initScanLib( ... )

    i.e. you tell the initScanLib function how to coerce its result-type.
    *Some* C functions take a pointer to a data-value to fill in their data,
    but not *your* function. That pattern looks like:

    result = c_short(0)
    my_ctypes_function( ..., byref(result) )
    print result.value

    i.e. you have to pass the variable into the function (as a
    reference/pointer).

    HTH,
    Mike

    --
    ________________________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://www.vrplumber.com
    http://blog.vrplumber.com
     
    Mike C. Fletcher, Jan 21, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    2,939
    Malcolm
    Aug 20, 2005
  2. Steve Simmons

    handling return codes from CTYPES

    Steve Simmons, Jan 21, 2013, in forum: Python
    Replies:
    2
    Views:
    120
    Chris Angelico
    Jan 22, 2013
  3. Steve Simmons

    Re: handling return codes from CTYPES

    Steve Simmons, Jan 21, 2013, in forum: Python
    Replies:
    0
    Views:
    125
    Steve Simmons
    Jan 21, 2013
  4. MRAB
    Replies:
    0
    Views:
    127
  5. Mike C. Fletcher

    Re: handling return codes from CTYPES

    Mike C. Fletcher, Jan 21, 2013, in forum: Python
    Replies:
    0
    Views:
    144
    Mike C. Fletcher
    Jan 21, 2013
Loading...

Share This Page