Call C functions from Python
Is there some other way, besides SWIG, which will allow me to call functions inside an Ansi C DLL? Example (C): defs.h --- typedef unsigned long MY_DIGIT; myapp.c - #include "defs.h" char *DoSomeStuff(char *input, MY_DIGIT *digits) { ... } ..thats an example of something I would like to call from Python. -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
On 2005-10-04, Java and Swing <[EMAIL PROTECTED]> wrote: > Is there some other way, besides SWIG, which will allow me to call > functions inside an Ansi C DLL? ctypes -- Grant Edwards grante Yow! Now KEN and BARBIE at are PERMANENTLY ADDICTED to visi.comMIND-ALTERING DRUGS... -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
ok i got ctypes...now i try >> from ctypes import * >> myApp = CDLL("C:\\myapp.dll") ..now how can I call functions on in myapp.dll? From the tutorial I am not sure..i try, dir(cdll.myApp) and dir(myApp)..but don't see my functions listed. thanks Grant Edwards wrote: > On 2005-10-04, Java and Swing <[EMAIL PROTECTED]> wrote: > > > Is there some other way, besides SWIG, which will allow me to call > > functions inside an Ansi C DLL? > > ctypes > > -- > Grant Edwards grante Yow! Now KEN and BARBIE > at are PERMANENTLY ADDICTED to >visi.comMIND-ALTERING DRUGS... -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
On 2005-10-04, Java and Swing <[EMAIL PROTECTED]> wrote: > ok i got ctypes...now i try > >>> from ctypes import * >>> myApp = CDLL("C:\\myapp.dll") I've never seen that sort of usage before. I don't know what CDLL does, and I can't find it in the docs anywhere. Based on my reading of the tutorial, I would have tried eitehr myApp = cdll.myapp or myApp = cdll.LoadLibrary("C:/myapp.dll") > ..now how can I call functions on in myapp.dll? From the > tutorial I am not sure.. Assuming CDLL did something equivalent to cdll.LoadLibrary(), I'd try: myApp.FuncName() I've always done it the way it's done in the tutorial: mylib = windll.Lib_Name mylib.myFuncName() > i try, dir(cdll.myApp) and dir(myApp)..but don't see my > functions listed. I don't think dir() works on dll's like that. I certainly don't see it mentioned in the tutorial. What happened when you tried calling the function the way the tutorial does? myapp = cdll.myapp myapp.MyFunc() -- Grant Edwards grante Yow! Yow! Is my fallout at shelter termite proof? visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
Java and Swing wrote: > Is there some other way, besides SWIG, which will allow me to call > functions inside an Ansi C DLL? You could write an extension module. See Modules/xxmodule.c in the Python source tree, as well as http://docs.python.org/ext/ext.html In the specific case, if it weren't for the digits argument, the wrapper function would read static PyObject* Py_DoSomeStuff(PyObject*unused, PyObject* args) { char *input; if (!PyArg_ParseTuple("s:DoSomeStuff", &input)) return NULL; return PyString_FromString(DoSomeStuff(input)); } I cannot extend this to digits, as I don't know how the digits are represented if there is more than one (i.e. how does DoSomeStuff know how many digits are being passed?) If DoSomeStuff returns NULL on errors, additional exception throwing is necessary. If DoSomeStuff returns memory that the caller needs to release, you need to do so before returning from Py_DoSomeStuff. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
I used, myApp = CDLL("C:...") ...as I saw it in one of the ctypes samples. Anyhow, I tried... myApp = cdll.LoadLibrary("C:\\myapp.dll") myApp.AddNumbers(1, 4) ..I get an error... AttributeError: function 'AddNumbers' not found ...myapp certainly has AddNumbers. Grant Edwards wrote: > On 2005-10-04, Java and Swing <[EMAIL PROTECTED]> wrote: > > ok i got ctypes...now i try > > > >>> from ctypes import * > >>> myApp = CDLL("C:\\myapp.dll") > > I've never seen that sort of usage before. I don't know what > CDLL does, and I can't find it in the docs anywhere. > > Based on my reading of the tutorial, I would have tried eitehr > > myApp = cdll.myapp > or > myApp = cdll.LoadLibrary("C:/myapp.dll") > > > ..now how can I call functions on in myapp.dll? From the > > tutorial I am not sure.. > > Assuming CDLL did something equivalent to cdll.LoadLibrary(), > I'd try: > >myApp.FuncName() > > I've always done it the way it's done in the tutorial: > > mylib = windll.Lib_Name > mylib.myFuncName() > > > i try, dir(cdll.myApp) and dir(myApp)..but don't see my > > functions listed. > > I don't think dir() works on dll's like that. I certainly > don't see it mentioned in the tutorial. What happened when you > tried calling the function the way the tutorial does? > > myapp = cdll.myapp > myapp.MyFunc() > > -- > Grant Edwards grante Yow! Yow! Is my fallout > at shelter termite proof? >visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
Java and Swing wrote: >I used, myApp = CDLL("C:...") ...as I saw it in one of the ctypes > samples. > > Anyhow, I tried... > > myApp = cdll.LoadLibrary("C:\\myapp.dll") > myApp.AddNumbers(1, 4) > > ..I get an error... > > AttributeError: function 'AddNumbers' not found > > ...myapp certainly has AddNumbers. properly exported? what does dumpbin /exports myapp.pyd say? -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
i tried... >> from ctypes import * >> myapp = cdll.LoadLibrary("c:\\myapp.dll") >> dumpbin /exports myapp.pyd i get, SyntaxError: invalid syntax with it pointing at the first "p" in myapp.pyd. -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
On 2005-10-05, Java and Swing <[EMAIL PROTECTED]> wrote: > i tried... > >>> from ctypes import * >>> myapp = cdll.LoadLibrary("c:\\myapp.dll") >>> dumpbin /exports myapp.pyd > > i get, SyntaxError: invalid syntax with it pointing at the first "p" in > myapp.pyd. Um, just a guess, but I don't think that was python code. Try it at a command prompt. -- Grant Edwards grante Yow! The entire CHINESE at WOMEN'S VOLLEYBALL TEAM all visi.comshare ONE personality -- and have since BIRTH!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
"Java and Swing" wrote: >>> from ctypes import * >>> myapp = cdll.LoadLibrary("c:\\myapp.dll") >>> dumpbin /exports myapp.pyd > > i get, SyntaxError: invalid syntax with it pointing at the first "p" in > myapp.pyd. dumpbin is a command-line utillity, usually included in the compiler toolsuite... -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
i dont have a myapp.pyd ...i have myapp.c, or are u suggesting I dump the dll? or the swig generated python file? the swig generated python file only has .py and .pyc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Call C functions from Python
"Java and Swing" wrote: >i dont have a myapp.pyd ...i have myapp.c, or are u suggesting I dump > the dll? if that's what you're trying to import, yes. > or the swig generated python file? > the swig generated python file only has .py and .pyc. huh? if you have a swig-generated python file, why are you using ctypes? -- http://mail.python.org/mailman/listinfo/python-list