[Python-Dev] Please help in calling python fucntion from 'c'

Praveen Patil praveen.patil@silver-software.com
Wed, 4 Sep 2002 13:31:00 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0011_01C25417.4EC8F910
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi,

I have written 'C' dll(MY_DLL.DLL) . I am importing 'C' dll in python
file(example.py).
I want to call python function from 'c' function.
For your reference I have attached 'c' and python files to this mail.
In my pc:
python code is under the directory D:\test\example.py
dll is under the directory C:\Program Files\Python\DLLs\MY_DLL.pyd

Here are the steps I am following.

step(1): I am calling 'C' function(RECEIVE_FROM_IL_S) from python.
         This 'C' function is existing imported dll(MY_DLL).
step(2): I want to call python function(TestFunction) from 'C'
function(RECEIVE_FROM_IL_S).


Python code is(example.py)  :-
----------------------------
import MY_DLL

G_Logfile          = None

def TestFunction():
    G_Logfile = open('Pytestfile.txt', 'w')
    G_Logfile.write("%s \n"%'I am writing python created text file')
    G_Logfile.close
    G_Logfile = None
#end def TestFunction

if __name__ == "__main__":

   MY_DLL.RECEIVE_FROM_IL_S(10,50)


'C' code is (MY_DLL.c) :-
---------------------
#include <windows.h>
#include <stdio.h>
#include <Python.h>

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

    if( !(PyArg_ParseTuple(args, "ii",&i,&j)))
    {
       return NULL;
    }
    fp= fopen("RECEIVE_IL_S.txt", "w");
    fprintf(fp, "i=%d   j=%d" , i,j);
    fclose(fp);

    /* Here I want to call python function(TestFunction). Please suggest me
some solution*/

    _resultobj = Py_None;
    return _resultobj;
}


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

__declspec(dllexport) void __cdecl initMY_DLL(void)
  {
    Py_InitModule("MY_DLL",MY_DLL_methods);
  }


Please anybody help me solving the problem.


Cheers,

Praveen.

------=_NextPart_000_0011_01C25417.4EC8F910
Content-Type: text/plain;
	name="exampl.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="exampl.py"

import MY_DLL

G_Logfile          = None

def TestFunction():
    G_Logfile = open('Pytestfile.txt', 'w')
    G_Logfile.write("%s \n"%'I am writing python created text file')
    G_Logfile.close
    G_Logfile = None
#end def TestFunction  

if __name__ == "__main__":

   MY_DLL.RECEIVE_FROM_IL_S(10,50)

------=_NextPart_000_0011_01C25417.4EC8F910
Content-Type: application/octet-stream;
	name="MY_DLL.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="MY_DLL.c"

#include <windows.h>
#include <stdio.h>
#include <Python.h>

PyObject* _wrap_RECEIVE_FROM_IL_S(PyObject *self, PyObject *args)
{
    FILE* fp; =20
    PyObject* _resultobj;
    int i,j;
   =20
    if( !(PyArg_ParseTuple(args, "ii",&i,&j)))
    {
       return NULL;
    }
    fp=3D fopen("RECEIVE_IL_S.txt", "w");
    fprintf(fp, "i=3D%d   j=3D%d" , i,j);
    fclose(fp);

    /* Here I want to call python function(TestFunction). Please suggest =
me some solution*/

    _resultobj =3D Py_None;
    return _resultobj;
}


static PyMethodDef MY_DLL_methods[] =3D {
      { "RECEIVE_FROM_IL_S", _wrap_RECEIVE_FROM_IL_S, METH_VARARGS },
      { NULL , NULL}
      };

__declspec(dllexport) void __cdecl initMY_DLL(void)
  {
    Py_InitModule("MY_DLL",MY_DLL_methods);
  }

------=_NextPart_000_0011_01C25417.4EC8F910
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

[ The information contained in this e-mail is confidential and is intended for the named recipient only. If you are not the named recipient, please notify us by telephone on +44 (0)1249 442 430 immediately, destroy the message and delete it from your computer. Silver Software has taken every reasonable precaution to ensure that any attachment to this e-mail has been checked for viruses. However, we cannot accept liability for any damage sustained as a result of any such software viruses and advise you to carry out your own virus check before opening any attachment. Furthermore, we do not accept responsibility for any change made to this message after it was sent by the sender.]

------=_NextPart_000_0011_01C25417.4EC8F910--