简体   繁体   中英

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=( ....)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM