ctypes并通过引用传递给函数

use*_*346 7 python ctypes libpcap

我正在尝试使用ctypes在python3中使用libpcap.

在C中给出以下功能

pcap_lookupnet(dev, &net, &mask, errbuf)
Run Code Online (Sandbox Code Playgroud)

在python我有以下内容

pcap_lookupnet = pcap.pcap_lookupnet

mask = ctypes.c_uint32
net = ctypes.c_int32

if(pcap_lookupnet(dev,net,mask,errbuf) == -1):
print("Error could not get netmask for device {0}".format(errbuf))
sys.exit(0)
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

  File "./libpcap.py", line 63, in <module>
 if(pcap_lookupnet(dev,net,mask,errbuf) == -1):
ctypes.ArgumentError: argument 2: <class 'TypeError'>: Don't know how to convert parameter 2
Run Code Online (Sandbox Code Playgroud)

你如何应对&blah价值观?

Dav*_*nan 18

您需要为net和创建实例mask,并使用byref它们来传递它们.

mask = ctypes.c_uint32()
net = ctypes.c_int32()
pcap_lookupnet(dev, ctypes.byref(net), ctypes.byref(mask), errbuf)
Run Code Online (Sandbox Code Playgroud)

  • Stackoverflow 说要避免使用“谢谢”之类的评论,但我也花了很长时间试图找出解决此问题的方法。所以谢谢!:) (4认同)