Python:转换与ctypes.c_void_p()一起使用的字符串 [英] Python: converting strings for use with ctypes.c_void_p()

查看:3145
本文介绍了Python:转换与ctypes.c_void_p()一起使用的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个字符串:

  msg =hello world
pre>

如何将它定义为 ctypes.c_void_p()数据类型?



以下代码产生无法转换为指针异常:

  data = ctypes.c_void_p(msg)

code>必须是C中的一个 void * 类型,因为它被传递给一个DLL。



我假设有一种使用 struct 包打包/解压缩字符串的方法,但不幸的是我非常不熟悉这个过程。

解决方案

这样的东西?使用 ctypes.cast

 >>> import ctypes 
>>> p1 = ctypes.c_char_p(hi mom)
>>> ctypes.cast(p1,ctypes.c_void_p)
c_void_p(11133300)


given a string:

msg="hello world"

How can I define this as a ctypes.c_void_p() data type?

the following code yields a "cannot be converted to pointer" exception:

data=ctypes.c_void_p(msg)

data is required to be a void* type in C, because it is being passed to a DLL.

I'm assuming there is a way to pack/unpack the string using the struct package, but unfortunately I am very unfamiliar with this process.

解决方案

Something like this? Using ctypes.cast?

>>> import ctypes
>>> p1= ctypes.c_char_p("hi mom")
>>> ctypes.cast( p1, ctypes.c_void_p )
c_void_p(11133300)

这篇关于Python:转换与ctypes.c_void_p()一起使用的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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