Python Language ctypes Basic usage


Let's say we want to use libc's ntohl function.

First, we must load

>>> from ctypes import *
>>> libc = cdll.LoadLibrary('')
>>> libc
<CDLL '', handle baadf00d at 0xdeadbeef>

Then, we get the function object:

>>> ntohl = libc.ntohl
>>> ntohl
<_FuncPtr object at 0xbaadf00d>

And now, we can simply invoke the function:

>>> ntohl(0x6C)
>>> hex(_)

Which does exactly what we expect it to do.