I am trying to build a simple Python wrapper for a library we are using. But the weird thing is this extension doesn't work with the official Python build, only with my own Python build. Even though they were based on the same version of source code. I am also a C/C++ rookie so I am not sure if my program is correct or not.
So one of the method I am trying to wrap is like this (copied from their SDK document):
Syntax
void IoWrite(IOHANDLE hIo, void * sourceBuffer, long size);
Parameters
hIo The handle to the IO signal, i.e. the return value from the IoConnect operation.
sourceBuffer The parameter containing the value that the IO signal should have.
size The size of the signal to write.
Return value
No return value used.
Code Example
IOHANDLE hIoSig;
long ioVariable = 10;
hIoSig = IoConnect("IoSigName", sizeof(ioVariable));
IoWrite(hIoSig, &ioVariable, sizeof(ioVariable));
Here is the Python extension:
#include "Python.h"
#include <conio.h>
#include "SimIoPort.h"
static PyObject *
ex_IoWrite(PyObject *self, PyObject *args)
{
char *IoPortName;
long PortValue;
IOHANDLE hIOsignal;
if (!PyArg_ParseTuple(args, "si", &IoPortName, &PortValue))
{
return NULL;
}
hIOsignal = IoConnect(IoPortName, sizeof(PortValue));
IoWrite(hIOsignal, &PortValue, sizeof(PortValue));
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef SimIo_methods[] = {
{"io_write", ex_IoWrite, METH_VARARGS,
"IoWrite method."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initSimIo(void)
{
Py_InitModule("SimIo", SimIo_methods);
}
Here is my setup.py:
from distutils.core import setup, Extension
simio_module = Extension('SimIo',
include_dirs = ['Headers', 'Libs'],
libraries = ['user32', ],
sources = ['SimIoModule.c', 'Libs\SimIoPort.c'],
)
setup(name = "SimIo",
version = "0.1",
description = "SimIo",
ext_modules = [simio_module],
)
If I use the official Python build 2.7.5, it will crash some other applications that use this SDK, but I don't have problem if I use it with my own Python 2.7.5 built with VS 2008 Express.
Thanks a lot!
Additional information regarding to the compiler: I was using MS Visual C++ 2008 Express Edition with SP1, nothing else.
Compiled using "C:\Python27\python.exe setup.py build"