在python中使用ctypes方法会产生意外错误 [英] using ctypes methods in python gives unexpected error

查看:81
本文介绍了在python中使用ctypes方法会产生意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python和ctypes很陌生。我正在尝试完成看似简单的任务,但是却获得了意想不到的结果。我正在尝试将字符串传递给c函数,因此我使用的是c_char_p类型,但这给了我一条错误消息。简而言之,这就是发生的事情:

I'm pretty new to python and ctypes. I'm trying to accomplish a seemingly easy task but am getting unexpected results. I'm trying to pass a string to a c function so I'm using the c_char_p type but it's giving me an error message. To simply it, this is whats happening:

>>>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

这是怎么回事? / p>

What's going on here?

推荐答案

在Python 3.x中,文本文字 确实是unicode对象。您想使用 b字节字符串文字

In Python 3.x, the "text literal" is really a unicode object. You want to use the byte-string literal like b"byte-string literal"

>>> 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')
>>>

这篇关于在python中使用ctypes方法会产生意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆