【发布时间】:2011-11-06 10:03:06
【问题描述】:
我正在尝试这个简单的 ctypes 示例并收到提到的错误
>>> from ctypes import create_string_buffer
>>> str = create_string_buffer("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python32\lib\ctypes\__init__.py", line 59, in create_string_buffer
buf.value = init
TypeError: str/bytes expected instead of str instance
有谁知道我做错了什么?
同样,我试图将指向字符串的指针从我的 python 代码传递给 C 函数,以便我可以在那里执行一些字符串操作并返回另一个字符串。有人可以给我一些示例代码吗?
extern "C" __declspec(dllexport) char * echo(char* c)
{
// do stuff
return c;
}
【问题讨论】:
-
请永远不要分配给
str(这不是这里的问题)。隐藏内置函数是邪恶的。
标签: python string pointers ctypes