View Single Post
  #1  
Old 01-12-2006, 09:41 PM
Vygreif Vygreif is offline
Member
 
Join Date: Sep 2005
Posts: 21
Extending Python With C++

Hi,
I'm trying to make a new dll for Python using C++ and am having a lot of trouble with it.
Firstly, I've tried looking at Guido's guide to extending dll s, but I can't find the file example_nt he speaks of in 4.1 (Guido's guide is the one used in your help menu).

Here is the actual code:


// include guards?
#include <Python.h>
#include "stdafx.h"
#include "ImmDevice3.h"
#include "impulse.h"

using namespace std;

// device is set up at inithdjoy()
static ImmDevice3* device;

BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}

static PyObject* py_getPosition(PyObject *self)
{
ImmCoord3 pos;
device->GetPosition(&pos);

return Py_BuildValue("[dd]", pos.x, pos.y);
}

//Set the force on the Impulse Engine
PyObject* py_setForce(PyObject *self, PyObject *args)
{
ImmCoord3 force;

//we don't have any force in the z direction
force.z = 0;
//Get two double arguments
if( !PyArg_ParseTuple(args, "dd", &force.x, &force.y) )
return NULL;
// What previous person did
//device->SetForce(&data.force);
//What I think should be done
device->SetForce(&force);
device->Update();

Py_INCREF(Py_None);
return Py_None;
}

//This function will take an integer and return a list of numbers starting from 0 to that number
PyObject* py_calibrate(PyObject *self)
{
device->Calibrate();

Py_INCREF(Py_None);
return Py_None;
}

//This will declare the functions that are available within python
//The first value is the name of the function within python.
//The second value is the name of the function within this file.
static PyMethodDef HDJoyMethods[] = {
{"getPosition", py_getPosition, METH_NOARGS},
{"setForce", py_setForce, METH_VARARGS},
{"calibrate", py_calibrate, METH_NOARGS},
{NULL, NULL}
};

//Initialize the hdjoy module
extern "C" __declspec(dllexport) void inithdjoy()
{
device = ImmDevice3Factory::AcquireImpulse( kImm_IE_DEVICE_N, DEVICE_IE2000 );
Py_InitModule("hdjoy", HDJoyMethods);
}

It gets the following errors before the compiler gives up:
std: a namespace with this name does not exist.

d:\Projects - current\hdjoy\hdjoy\hdjoy.cpp(20): error C2065: 'self' : undeclared identifier
d:\Projects - current\hdjoy\hdjoy\hdjoy.cpp(20): error C2143: syntax error : missing ';' before '*'
d:\Projects - current\hdjoy\hdjoy\hdjoy.cpp(20): error C2501: 'py_getPosition' : missing storage-class or type specifiers



If I move the #include <Python.h>
I get this error instead, as well as the std error
d:\Projects - current\hdjoy\hdjoy\hdjoy.cpp(63): error C2440: 'initializing' : cannot convert from 'PyObject *(__cdecl *)(PyObject *)' to 'PyCFunction'
None of the functions with this name in scope match the target type

Sorry that I can't be more specific about my problem. It might very well be that I don't know how to properly include the libraries. In the header files directory I put in ImmDevice3.h, impulse.h, and stdafx.h. in the Header Files folder.

Thanks,
YG
Reply With Quote