Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: handling return codes from CTYPES (http://www.velocityreviews.com/forums/t956760-re-handling-return-codes-from-ctypes.html)

Mike C. Fletcher 01-21-2013 03:14 PM

Re: handling return codes from CTYPES
 
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



All times are GMT. The time now is 11:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57