python and ctypes cdll, not getting expected return from function
Question: I'm in the process of working on interfacing a haptic robot and eye tracker.
So, both come with their own programming requirements, namely that the eye
tracker software is based in python and is the main language in which I'm
programming. Our haptic robot has an API in C, so I've had to write a wrapper
in C, compile it as a DLL, and use ctypes in python to load the functions.

I've tested my DLL with MATLAB, and everything works just fine. However,
something about my implementation of ctypes in my python class is not giving
me the expected return value when i query the robot's positional coordinates.

I'll post the code here, and a clearer explanation of the problem at the
bottom.

C source code for DLL wrapper:

    
    
    #include <QHHeadersGLUT.h>
    using namespace std;
    class myHapClass{
    public:
    void InitOmni()
    {
        DeviceSpace* Omni = new DeviceSpace; //Find a the default phantom
    }
    double GetCoord(int index)
    {
        HDdouble hapPos[3];
        hdGetDoublev(HD_CURRENT_POSITION,hapPos);
        return hapPos[index];
    }
    };
    
    extern "C" {
    int index = 1;
    __declspec(dllexport) myHapClass* myHap_new(){ return new myHapClass();}
    __declspec(dllexport) void myHapInit(myHapClass* myHapObj){ myHapObj->InitOmni();}
    __declspec(dllexport) double myHapCoord(myHapClass* myHapObj){ double nowCoord = myHapObj->GetCoord(index); return nowCoord;}
    }
    

The theory for this code is simply to have 3 available C (not C++) calls that
will be compatible with python/ctypes:

  1. myHap_new() returns a pointer to an instance of the class
  2. myHapInit initializes the haptics device
  3. myHapCoord returns a double for the current position, of the axis referenced by int Index.

The python class follows here:

    
    
    import sreb
    from ctypes import cdll
    lib = cdll.LoadLibrary('C:\Documents and Settings\EyeLink\My Documents\Visual Studio 2010\Projects\myHapDLLSolution\Debug\myHapDLL.dll')
    
    class CustomClassTemplate(sreb.EBObject):
    def __init__(self):
        sreb.EBObject.__init__(self)
        self.pyMyHapObj = pyMyHap()
        self.coordval=2.0
    
    def setCoordval(self,c):
        self.coordval = c
        pass
    
    def getCoordval(self):
        return self.coordval
    
    def initOmni(self):
        self.pyMyHapObj.pyHapInit()
        pass
    
    def getCoord(self):
        self.coordval = self.pyMyHapObj.pyHapCoord()
        return self.coordval
    
    class pyMyHap(object):
    def __init__(self):
        self.obj = lib.myHap_new()
        self.coord = 1.0
    
    def pyHapInit(self):
            lib.myHapInit(self.obj)
    
        def pyHapCoord(self):
        self.coord = lib.myHapCoord(self.obj)
        return self.coord
    

The theory of the python custom class is to instantiate an object
(self.pyMyHapObj = pyMyHap()) of the loaded DLL class. Making a call to the
function 'initOmni' successfully initializes the robot, however a call to
'getCoord' does not return the expected value. In fact, the result I get from
'getCoord' is 1 (and it is listed as 1, not 1.0, so I think it is returning an
integer, not a double as it should).

In MATLAB, I have use the DLL library, and both the myHapInit and myHapCoord
functions work, and I can initialize the robot and query the position
coordinates successfully.

So what is it about my python class that is causing ctypes to not have the
proper value returned from myHapCoord from my DLL?

Any help would be appreciated. Thanks

edit: Python version 2.3, if it matters... I'm stuck to that version.


Answer: Return values default to `int`. Use something like:

    
    
    lib.myHapCoord.restype = ctypes.c_double
    

before calling the function to interpret the return value properly.

