Python 3.5,ctypes:TypeError:期望的字节或整数地址而不是str实例

tos*_*bar 10 python ctypes

我遇到了ctypes的问题.我认为我的类型转换是正确的,错误对我没有意义."arg - ct.c_char_p(logfilepath)"行错误TypeError:预期的字节或整数地址而不是str实例

我试过python 3.5和3.4.

功能我打电话:

stream_initialize('stream_log.txt')
Run Code Online (Sandbox Code Playgroud)

Stream_initialize代码"

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

    if result:
        print(find_shim_error(result))
Run Code Online (Sandbox Code Playgroud)

use*_*367 19

c_char_p接受bytes对象,所以你必须转换stringbytes第一个:

ct.c_char_p(logfilepath.encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)

另一种解决方案是使用c_wchar_p带有a 的类型string.

  • 这个响应应该在关于ctypes的文档中.我花了很多时间试图解决这个问题. (3认同)
  • 那是因为您正在查看 python2 文档。将网址中的“2”替换为“3”,您实际上会发现 python3 文档声明它需要字节。 (2认同)