DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Calling c function from python (http://www.daniweb.com/forums/thread33520.html)

john_27 Oct 7th, 2005 10:19 am
Calling c function from python
 
Hi all,

I am a newbie to python and am not sure,what i am doing is correct or not ,i wanna call c function from within python.
i have my python program as:
import DLL

if __name__ == "__main__":
    for i in range(1,6):
        DLL.RECEIVE_FROM_IL_S(10,50)
here RECEIVE_FROM_IL_S() is c function which i wanna call.And my c
function would be,
#include <stdio.h>
#include <windows.h>
#include <stdio.h>
#ifdef _DEBUG
  #undef _DEBUG
  #include "python.h"
  #define _DEBUG
#else
  #include "python.h"
#endif

PyObject* _wrap_RECEIVE_FROM_IL_S(PyObject *self, PyObject *args)
{
    FILE* fp;
    PyObject* _resultobj;
    int i,j;

    if( !(PyArg_ParseTuple(args, "i",&i,&j)))
    {
      return NULL;
         
    }

/*Here if i want to call another c function say SEND_TO_IL(),what shud i be including*/

        fp= fopen('fileName.txt', "w");
        fprintf(fp, "i=%d  j=%d" , i,j);
            fclose(fp);

    _resultobj = Py_None;
    return _resultobj; 

}

static PyMethodDef MY_DLL_methods[] = {
      { "RECEIVE_FROM_IL_S", _wrap_RECEIVE_FROM_IL_S, METH_VARARGS },
      { "FUNC_CALL" , function,METH_VARARGS },
      { NULL , NULL}
      };

__declspec(dllexport) void __cdecl initDLL(void)
  {
    Py_InitModule("DLL",MY_DLL_methods);
  }

Also, if i don't want to pass any args to RECEIVE_FROM_IL_S from python what shud i do.

Regards,
john

Edit: Code tags added by vegaseat

vegaseat Oct 7th, 2005 4:53 pm
Re: Calling c function from python
 
Are you following an example from a book?

Normally I would use the ctypes module from: http://starship.python.net/crew/theller/ctypes/


All times are GMT -4. The time now is 3:46 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC