在python中使用ctypes方法会产生意外错误

在python中使用ctypes方法会产生意外错误,python,ctypes,Python,Ctypes,我对python和ctypes非常陌生。我试图完成一项看似简单的任务,但却得到了意想不到的结果。我试图向c函数传递一个字符串,所以我使用c_char_p类型,但它给了我一条错误消息。简单地说,这就是正在发生的事情: >>>from ctypes import * >>>c_char_p("hello world") Traceback (most recent call last): File "<stdin>", line 1, in &

我对python和ctypes非常陌生。我试图完成一项看似简单的任务,但却得到了意想不到的结果。我试图向c函数传递一个字符串,所以我使用c_char_p类型,但它给了我一条错误消息。简单地说,这就是正在发生的事情:

>>>from ctypes import *
>>>c_char_p("hello world") 
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string or integer address expected instead of str instance
>>从ctypes导入*
>>>c_char_p(“你好,世界”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:需要字符串或整数地址,而不是str实例
这是怎么回事?

在Python3.x中,
的“文本文本”
实际上是一个unicode对象。您想使用字节字符串文字,如
b“字节字符串文字”

>>从ctypes导入*
>>>c_char_p(“你好,世界”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:需要字符串或整数地址,而不是str实例
>>>c_char_p(b'hello world')
c_char_p(b'hello world')
>>>

谢谢,这非常有帮助。原来我在看Python2.7文档,这就是为什么我如此困惑的原因。
>>> from ctypes import *
>>> c_char_p('hello world')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string or integer address expected instead of str instance
>>> c_char_p(b'hello world')
c_char_p(b'hello world')
>>>