Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37204

Re: handling return codes from CTYPES

Date 2013-01-21 10:14 -0500
From "Mike C. Fletcher" <mcfletch@vrplumber.com>
Subject Re: handling return codes from CTYPES
References <50FD1C6F.6030801@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.749.1358781264.2939.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: handling return codes from CTYPES "Mike C. Fletcher" <mcfletch@vrplumber.com> - 2013-01-21 10:14 -0500

csiph-web