[EuroPython] Please help in calling python fucntion from 'c'

M.-A. Lemburg mal@egenix.com
Wed, 04 Sep 2002 14:27:34 +0200


Please post this to comp.lang.python. This is not the right list to
discuss technical aspect around Python.

Thanks.

Praveen Patil wrote:
> 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.
> 
> 
> ------------------------------------------------------------------------
> 
> 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)
> 
> 
> ------------------------------------------------------------------------
> 
> [ 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.]

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/