944,132 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 11219
  • Python RSS
Oct 7th, 2005
0

Calling c function from python

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  1. import DLL
  2.  
  3. if __name__ == "__main__":
  4. for i in range(1,6):
  5. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
john_27 is offline Offline
1 posts
since Oct 2005
Oct 7th, 2005
0

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/
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python Graphics Help
Next Thread in Python Forum Timeline: Segmentation fault





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC