Übergeben von Python-Objekten als Argumente an die C / C ++ - Funktion mithilfe von ctypes
Ich habe eine DLL mit einer Funktion, die PyObject als Argument so ähnlich nimmt
void MyFunction(PyObject* obj)
{
PyObject *func, *res, *test;
//function getAddress of python object
func = PyObject_GetAttrString(obj, "getAddress");
res = PyObject_CallFunction(func, NULL);
cout << "Address: " << PyString_AsString( PyObject_Str(res) ) << endl;
}
und ich möchte diese Funktion in der DLL von Python mit ctypes aufrufen
Mein Python-Code sieht aus wie
import ctypes as c
path = "h:\libTest"
libTest = c.cdll.LoadLibrary( path )
class MyClass:
@classmethod
def getAddress(cls):
return "Some Address"
prototype = c.CFUNCTYPE(
c.c_char_p,
c.py_object
)
func = prototype(('MyFunction', libTest))
pyobj = c.py_object(MyClass)
func( c.byref(pyobj) )
Es gibt ein Problem in meinem Python-Code, wenn ich diesen Code ausführe
WindowsError: Ausnahme: Zugriffsverletzung beim Lesen von 0x00000020
Jeder Vorschlag zur Verbesserung des Python-Codes wäre willkommen.