ctypes and passing a by reference to a function

寵の児 提交于 2019-11-29 04:29:23

You need to create instances for net and mask, and use byref to pass them.

mask = ctypes.c_uint32()
net = ctypes.c_int32()
pcap_lookupnet(dev, ctypes.byref(net), ctypes.byref(mask), errbuf)

You probably need to use ctypes.pointer, like this:

pcap_lookupnet(dev, ctypes.pointer(net), ctypes.pointer(mask), errbuf)

See the ctypes tutorial section on pointers for more information.

I'm assuming you've created ctypes proxies for the other arguments as well. If dev requires a string, for example, you can't simply pass in a Python string; you need to create a ctypes_wchar_p or something along those lines.

ctypes.c_uint32 is a type. You need an instance:

mask = ctypes.c_uint32()
net = ctypes.c_int32()

Then pass using ctypes.byref:

pcap_lookupnet(dev,ctypes.byref(mask),ctypes.byref(net),errbuf)

You can retrieve the value using mask.value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!