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


Groups > comp.lang.python > #37204

Re: handling return codes from CTYPES

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!nuzba.szn.dk!pnx.dk!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <mcfletch@vrplumber.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.030
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'url:blog': 0.09; 'steve': 0.13; '...,': 0.16; 'created.': 0.16; 'discarded': 0.16; 'result:': 0.16; 'subject:handling': 0.16; 'wrote:': 0.17; 'pointer': 0.17; '>>>': 0.18; 'variable': 0.20; 'import': 0.21; 'ctypes': 0.22; "i've": 0.23; 'pass': 0.25; 'tried': 0.25; 'header :In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'am,': 0.27; 'mike': 0.27; '(as': 0.27; 'i.e.': 0.27; 'function': 0.30; 'print': 0.32; 'skip:s 30': 0.33; 'function.': 0.33; 'like:': 0.33; 'to:addr:python-list': 0.33; 'data,': 0.35; 'skip:_ 40': 0.35; 'but': 0.36; 'wanted': 0.36; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; "you've": 0.61; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'received:192.168.15': 0.84; 'received:98.158': 0.84; 'simmons': 0.84
Date Mon, 21 Jan 2013 10:14:15 -0500
From "Mike C. Fletcher" <mcfletch@vrplumber.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: handling return codes from CTYPES
References <50FD1C6F.6030801@gmail.com>
In-Reply-To <50FD1C6F.6030801@gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To mcfletch@vrplumber.com
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.749.1358781264.2939.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358781264 news.xs4all.nl 6869 [2001:888:2000:d::a6]:46079
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37204

Show key headers only | 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