Python CTYPES模块如何从C++ func返回python int,而不设置restype = c_long_long
我使用python ctypes模块从C ++函数中返回uint64_t类型。 在Python中,我没有设置Restype(C_LONG_LONG),我获得了一个python int值-870013293,但是设置Restype值为1470523793632320851。 您能告诉我有关int_val和long_val的关系吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ctypes
的默认返回类型是c_int
,它是一个32位签名的值。如果您不设置.restype = C_UINT64
为64位值,则返回值将从C不正确地转换为c转换为Python。如果您以十六进制显示,则可以看到该值将其截断为32位:请注意,64位值的最后32位与32位签名的值匹配。
The default return type for
ctypes
isc_int
which is a 32-bit signed value. If you don't set.restype = c_uint64
for a 64-bit value, the return value is converted incorrectly from C to Python. You can see that the value was truncated to 32 bits if you display it in hexadecimal:Note that the last 32 bits of the 64-bit value match the 32-bit signed value.
这是文档中写的内容: https> https://docs.python .org/3/library/ctypes.html#module-ctypes
因此,默认值将值视为C_INT,并且:
由于没有溢出并且数字签名,而不是大数字,因此可以输出任何数字,从-2,147,483,648到2,147,483,647
。 c_ulonglong),然后您将获得所需的价值。(64位)
Here is what is written in the documentation: https://docs.python.org/3/library/ctypes.html#module-ctypes
Therefore, default the value is treated as c_int, and:
Since there is no overflow and the number is signed, instead of a large number, it can output any number from -2,147,483,648 to 2,147,483,647.(32 bits)
If you know the return value will be unsigned and explicitly type c_uint64 ( usually an alias for c_ulonglong), then you'll get the value you want.(64 bits)