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

Ian Kelly ian.g.kelly at gmail.com
Wed Apr 15 22:32:28 CEST 2015

On Wed, Apr 15, 2015 at 1:48 PM, IronManMark20 <mr.smittye at 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
>>> px.contents.value

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

More information about the Python-list mailing list