Python 3.5,ctypes:TypeError:应使用字节或整数地址(而非str实例) [英] Python 3.5, ctypes: TypeError: bytes or integer address expected instead of str instance

查看:451
本文介绍了Python 3.5,ctypes:TypeError:应使用字节或整数地址(而非str实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ctypes时遇到问题。我认为我的类型转换是正确的,并且该错误对我来说没有意义。
线路上的错误 arg-ct.c_char_p(logfilepath)
TypeError:预期为字节或整数地址,而不是str实例

I'm having an issue with ctypes. I think my type conversion is correct and the error isn't making sense to me. Error on line " arg - ct.c_char_p(logfilepath) " TypeError: bytes or integer address expected instead of str instance

python 3.5和3.4。

I tried in both python 3.5 and 3.4.

我正在调用的函数:

stream_initialize('stream_log.txt')

Stream_initialize代码

Stream_initialize code"

def stream_initialize(logfilepath):
    f = shim.stream_initialize
    arg = ct.c_char_p(logfilepath)
    result = f(arg)

    if result:
        print(find_shim_error(result))


推荐答案

c_char_p 需要 bytes 对象,因此您必须转换 string bytes 首先:

c_char_p takes bytes object so you have to convert your string to bytes first:

ct.c_char_p(logfilepath.encode('utf-8'))

另一种解决方案是使用 c_wchar_p 类型,该类型采用字符串

Another solution is using the c_wchar_p type which takes a string.

这篇关于Python 3.5,ctypes:TypeError:应使用字节或整数地址(而非str实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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