Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37204 > unrolled thread
| Started by | "Mike C. Fletcher" <mcfletch@vrplumber.com> |
|---|---|
| First post | 2013-01-21 10:14 -0500 |
| Last post | 2013-01-21 10:14 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: handling return codes from CTYPES "Mike C. Fletcher" <mcfletch@vrplumber.com> - 2013-01-21 10:14 -0500
| From | "Mike C. Fletcher" <mcfletch@vrplumber.com> |
|---|---|
| Date | 2013-01-21 10:14 -0500 |
| Subject | Re: handling return codes from CTYPES |
| Message-ID | <mailman.749.1358781264.2939.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web