468,139 Members | 1,419 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,139 developers. It's quick & easy.

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.

Oct 4 '05 #1
11 2529
On 2005-10-04, Java and Swing <co*******@gmail.com> 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.com MIND-ALTERING DRUGS...
Oct 4 '05 #2
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 <co*******@gmail.com> 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.com MIND-ALTERING DRUGS...


Oct 4 '05 #3
On 2005-10-04, Java and Swing <co*******@gmail.com> 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
Oct 4 '05 #4
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
Oct 4 '05 #5
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 <co*******@gmail.com> 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


Oct 5 '05 #6
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?

</F>

Oct 5 '05 #7
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.

Oct 5 '05 #8
On 2005-10-05, Java and Swing <co*******@gmail.com> 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.com share ONE personality --
and have since BIRTH!!
Oct 5 '05 #9
"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...

</F>

Oct 5 '05 #10
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.

Oct 5 '05 #11
"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?

</F>

Oct 5 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dave Kuhlman | last post: by
reply views Thread by sdhyok | last post: by
19 posts views Thread by Riko Wichmann | last post: by
6 posts views Thread by enjoying the view | last post: by
6 posts views Thread by mh | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.