Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将char*作为参数传递给函数 我使用Cython插件从C++静态库创建了Python模块。C++库提供了一个列出文件夹内容的函数。它提供要显示其内容的目录的名称,以及存储目录内容的输出缓冲区,当然还有输出缓冲区的长度。 函数的C++定义如下: int ListItems(char *dir, char *buf, int buflen)_Python_C++_String_Cython - Fatal编程技术网

Python将char*作为参数传递给函数 我使用Cython插件从C++静态库创建了Python模块。C++库提供了一个列出文件夹内容的函数。它提供要显示其内容的目录的名称,以及存储目录内容的输出缓冲区,当然还有输出缓冲区的长度。 函数的C++定义如下: int ListItems(char *dir, char *buf, int buflen)

Python将char*作为参数传递给函数 我使用Cython插件从C++静态库创建了Python模块。C++库提供了一个列出文件夹内容的函数。它提供要显示其内容的目录的名称,以及存储目录内容的输出缓冲区,当然还有输出缓冲区的长度。 函数的C++定义如下: int ListItems(char *dir, char *buf, int buflen),python,c++,string,cython,Python,C++,String,Cython,python模块又有一个函数PyListItems,它只调用此函数: def PyListItems(char *dir, char *buf, int buflen): return ListItems(dir,buf,buflen)` 在使用此函数的脚本中,我定义了一个字符串缓冲区和一个类型为c\u char\u p的指向它的指针,并将其传递给函数以代替 buf参数: buf = c_types.create_string_buffer(100) pBuf = c_char_p(a

python模块又有一个函数
PyListItems
,它只调用此函数:

def PyListItems(char *dir, char *buf, int buflen):
    return ListItems(dir,buf,buflen)`
在使用此函数的脚本中,我定义了一个字符串缓冲区和一个类型为
c\u char\u p
的指向它的指针,并将其传递给函数以代替
buf
参数:

buf = c_types.create_string_buffer(100)
pBuf = c_char_p(addressof(buf))
PyListItems('someFolder',pBuf,100)
但我得到了以下错误:

TypeError:应为字符串或Unicode对象,找到c\u char\p

任何帮助都将不胜感激

pBuf = c_char_p(addressof(buf))
你不需要这个<代码>创建字符串缓冲区已返回字符数组。只要做:

buf = c_types.create_string_buffer(100)
PyListItems('someFolder',buf,100)

如果您使用的是Python 3,那么Python 3中的字符串(str类型)是Unicode对象。create_string_buffer创建C字符数组,因此需要传递一个bytes对象。如果您有一个str对象,请使用适当的编码进行编码,以创建一个bytes对象。注意,有一个create_unicode_缓冲区,它也可以创建C wchar数组并接受Python 3 str对象。事实上,您应该对此进行标记或以其他方式指定C/Python的版本。我正在使用Python 2.6,但仍然遇到一个错误:>TypeError:预期的字符串或unicode对象,找到了C_char_数组