accessing ctypes returned object's methods
Question: I need to wrap c++ dll to python. I'm using `ctypes` module for that.

c++ header is something like:

    
    
    class NativeObj
    {
        void func();
    }
    
    extern "C"
    {
        NativeObj* createNativeObj(); 
    
    }; //extern "C"
    

I want to create `NativeObj` in python code and then call its `func` method.

I wrote this code and get pointer to `NativeObj` but I didn't find how to
access `func`

    
    
    >>> import ctypes
    >>> d = ctypes.cdll.LoadLibrary('dll/path')
    >>> obj = d.createNativeObj()
    >>> obj
    36408838
    >>> type(obj)
    <type 'int'>
    

Thanks.


Answer: You cannot call C++ instance methods from ctypes. You will need to export a
non-member function that will call the method. It will look like this in C++:

    
    
    void callFunc(NativeObj* obj)
    {
        obj->func();
    }
    

You can then call it like this:

    
    
    import ctypes
    d = ctypes.cdll.LoadLibrary('dll/path')
    obj = d.createNativeObj()
    d.callFunc(obj)
    

It would also be useful to tell `ctypes` about the types involved.

    
    
    import ctypes
    d = ctypes.cdll.LoadLibrary('dll/path')
    
    createNativeObj = d.createNativeObj
    createNativeObj.restype = ctypes.c_void_p
    callFunc = d.callFunc
    callFunc.argtypes = [ctypes.c_void_p]
    
    obj = createNativeObj()
    callFunc(obj)
    

