【问题标题】:Translate ctypes args from c++ to python将 ctypes args 从 c++ 翻译成 python
【发布时间】:2020-10-19 09:01:17
【问题描述】:

我尝试使用 ctypes 在 python 程序上使用 DLL。

这是我想在 python 中使用的 C# 代码和函数的参数:

[DllImport("test.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern int function(
  byte[] filename,
  byte[] name,
  IntPtr result,
  out IntPtr UnmanagedStringArray,
  out int iStringsCountReceiver);

下面的代码是我的python代码。我搜索了几个站点,但找不到有关 argtypes 的提示。 请帮我在python中使用DLL。

mydll = c.cdll.LoadLibrary("test.dll")
func = mydll['function']
func.argtypes=( ....)

【问题讨论】:

标签: python dll ctypes args


【解决方案1】:

byte[] 等价于 C 语言中的 char*,所以 c_char_p 可以工作。 C# 中的IntPtr 是一个指针大小的整数。 c_void_p 应该可以工作。 out 参数需要指针。

你可以试试:

import ctypes as c
mydll = c.CDLL('./test')
func = mydll.function
func.argtypes = c.c_char_p,c_c_char_p,c.c_void_p,c.POINTER(c_void_p),c.POINTER(c.c_int)
func.restype = c.c_int

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 2017-10-12
    • 1970-01-01
    相关资源
    最近更新 更多