Hi,
Below is the code I am compiling to a .dll & .lib but when I try to import the "Rand" module I get ImportError: No module named Rand, I renamed the .dll to .pyd then this error is removed but when I call the sub module Rand.myRand(10,30) it crashes with a message pythonw.exe has stopped working
I am using Python2.6 on windows 7 machine.

#include <Python.h>
#define RANDMAX 32767

#ifdef __cplusplus
	extern "C" PyMODINIT_FUNC initRand(void);
#endif 

time_t Tval = 0;

int myRand(int lower,int upper)
{
	int temp = 0;
	int b = 0 ;
	if (upper == lower)
		return  lower;
	temp = rand();
	b = lower + (temp / RANDMAX) * (upper - lower);
	return b;
}
static PyObject* py_myRand(PyObject* self, PyObject* args)
{
	int lower = 0;
	int upper = 0;
	int value = 0;
	if (!PyArg_ParseTuple(args, "ii", lower,upper))
        return NULL;
	value = myRand(lower,upper);
	return Py_BuildValue("i", value);
}
static PyMethodDef RandMethods[] = {
    {"myRand",  py_myRand, METH_VARARGS,"Generate Random Number"},
    {NULL, NULL, 0, NULL} 
};

PyMODINIT_FUNC initRand(void)
{
    (void) Py_InitModule("Rand", RandMethods);
}

int main(int argc, char *argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    initRand();
    return 0;
}

Thanks in advance.....

Recommended Answers

All 2 Replies

The worst looking line is line 25: PyArg_ParseTuple takes addresses. It should be

if (!PyArg_ParseTuple(args, "ii", &lower, &upper))

See if it runs this way.

The worst looking line is line 25: PyArg_ParseTuple takes addresses. It should be

if (!PyArg_ParseTuple(args, "ii", &lower, &upper))

See if it runs this way.

GREAT ! thanks a ton.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.