将ctypes args从C ++转换为python

问题描述

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

这是我要在python中使用的C#代码函数的args:

entry.image_set.all.0

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

[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);

解决方法

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