Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass char pointer as argument in ctypes python

Please help me in converting below line of c++ code into ctypes python:

Ret = openFcn(&Handle, "C:\\Config.xml");

below are the declarations of each:

typedef uint16_t (* OpenDLLFcnP)(void **, const char *);
OpenDLLFcnP openFcn = NULL;
openFcn = (OpenDLLFcnP) myLibrary.resolve("Open");
void *Handle = NULL;
like image 488
Naveen kumar Katta rathanaiah Avatar asked Sep 23 '13 12:09

Naveen kumar Katta rathanaiah


People also ask

What is Ctypes pointer?

ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.

What is CDLL in Python?

It is a big C++ codebase with a (very) thin python wrapper which uses CDLL to load the C++ and call some C functions that are available to allow primitive python scripting of the code.

What is Restype?

Specifies the type of AFP print resources ACIF retrieves from the resource directories or libraries for inclusion in the resource file (specified with the RESOBJDD parameter).


1 Answers

myLibrary.resolve is undefined, but the general code you need (untested) is:

import ctypes
dll = ctypes.CDLL('your.dll')
Open = dll.Open
Open.argtypes = [ctypes.POINTER(ctypes.c_void_p),ctypes.c_char_p]
Open.restype = ctypes.c_uint16
Handle = ctypes.c_void_p()
result = Open(ctypes.byref(Handle),'c:\\Config.xml')

This assumes you have a DLL named your.dll with a function Open you want to call.

like image 87
Mark Tolonen Avatar answered Sep 24 '22 06:09

Mark Tolonen