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


Groups > comp.lang.python > #88991 > unrolled thread

ctypes: using .value or .value() doesn't work for c_long

Started byIronManMark20 <mr.smittye@gmail.com>
First post2015-04-15 12:48 -0700
Last post2015-04-15 15:18 -0700
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  ctypes: using .value or .value() doesn't work for c_long IronManMark20 <mr.smittye@gmail.com> - 2015-04-15 12:48 -0700
    Re: ctypes: using .value or .value() doesn't work for c_long Ian Kelly <ian.g.kelly@gmail.com> - 2015-04-15 14:32 -0600
    Re: ctypes: using .value or .value() doesn't work for c_long Dave Angel <davea@davea.name> - 2015-04-15 17:03 -0400
    Re: ctypes: using .value or .value() doesn't work for c_long ethansmithoakland@gmail.com - 2015-04-15 15:18 -0700

#88991 — ctypes: using .value or .value() doesn't work for c_long

FromIronManMark20 <mr.smittye@gmail.com>
Date2015-04-15 12:48 -0700
Subjectctypes: using .value or .value() doesn't work for c_long
Message-ID<8a777999-f941-4fcf-a924-7a8a804e158d@googlegroups.com>
I am using ctypes to call a few windll funcions. One of them returns a c_long object. I want to know what number the function returns. 

Problem is, when I try foo.value , it gives me this:

AttributeError: LP_c_long object has no attribute value.

Any idea of what could cause this?

[toc] | [next] | [standalone]


#88997

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-04-15 14:32 -0600
Message-ID<mailman.323.1429129996.12925.python-list@python.org>
In reply to#88991
On Wed, Apr 15, 2015 at 1:48 PM, IronManMark20 <mr.smittye@gmail.com> wrote:
> I am using ctypes to call a few windll funcions. One of them returns a c_long object. I want to know what number the function returns.
>
> Problem is, when I try foo.value , it gives me this:
>
> AttributeError: LP_c_long object has no attribute value.
>
> Any idea of what could cause this?

The error message indicates that this is not actually a c_long object,
but an LP_c_long object; i.e., a pointer to a c_long. You can
dereference the pointer using the contents attribute:

>>> x = ctypes.c_long(42)
>>> px = ctypes.pointer(x)
>>> px
<__main__.LP_c_long object at 0x7fd0812d7950>
>>> px.contents
c_long(42)
>>> px.contents.value
42

See https://docs.python.org/3.4/library/ctypes.html#pointers for more
details on working with pointers in ctypes.

[toc] | [prev] | [next] | [standalone]


#88998

FromDave Angel <davea@davea.name>
Date2015-04-15 17:03 -0400
Message-ID<mailman.324.1429131838.12925.python-list@python.org>
In reply to#88991
On 04/15/2015 03:48 PM, IronManMark20 wrote:
> I am using ctypes to call a few windll funcions. One of them returns a c_long object. I want to know what number the function returns.
>
> Problem is, when I try foo.value , it gives me this:
>
> AttributeError: LP_c_long object has no attribute value.
>
> Any idea of what could cause this?
>

I don't use Windows, so I can't try it, especially since you didn't 
include any code <hint>.  And apparently whatever function you're using 
returns a pointer to a c_long, not a c_long.

I see Ian has given you an answer.  But let me show you how you might 
have come up with it on your own, for next time.

The fragment of the error you include says that the instance of the 
class LP_c_long doesn't have an attribute called "value".  But it 
undoubtedly has other attributes, so you could look with dir

     print(dir(foo))

Presumably from there you'll see that you need the 'contents' attribute. 
  Then you print that out, and/or dir a dir() on it, and you see that 
you now have a value attribute.


https://docs.python.org/3/library/ctypes.html

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#89000

Fromethansmithoakland@gmail.com
Date2015-04-15 15:18 -0700
Message-ID<893ff631-cc6e-46ee-bb38-d23632ae84cb@googlegroups.com>
In reply to#88991
Thanks so much! Btw (face-palm) Dave, I know dir(), I probably should have used it here, I just read that the return type was a c_long, and I didn't know that LP_c_long was a pointer. Thanks to you both!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web