我想将一个python的字符串列表传递给C函数,期望const char **。我看到了问题和解决方案here,但它似乎不适合我。下面的示例代码:Python的ctypes:传递给'const char **'参数的函数
argList = ['abc','def']
options = (ctypes.c_char_p * len(argList))()
options[:] = argList
提供了以下错误:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
TypeError: string or integer address expected instead of str instance
我在做什么错?
附录:
似乎有一个共识,那这个代码应工作。以下是如何重现该问题。
在我的Python命令行中输入的以下四行显示了我的问题。
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> argList = ['abc', 'def']
>>> options = (c_char_p * len(argList))()
>>> options[:] = argList
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string or integer address expected instead of str instance
>>>
哪里出错?该代码似乎工作。 – 2011-02-24 12:43:21
在我的机器上它没有。你在用什么系统?我在WinXP上使用Python 3.2。 - 这能改变吗? – ARF 2011-02-24 13:00:13
尝试'argList = [b'abc',b'def']'。 – 2011-02-26 04:17:34