Search code examples
pythondllctypesargs

Translate ctypes args from c++ to python


I try to use ctypes for using DLL on python program.

This is C# code and function's args that I want to use in python:

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

The code below is my python code. I searched several sites but I could not find hint about argtypes. Please help me use DLL in python.

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

Solution

  • byte[] is equivalent to char* in C, so c_char_p will work. IntPtr in C# is an integer the size of a pointer. c_void_p should work. out parameters require pointers.

    You can try:

    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