Call C functions from Python

J

Java and Swing

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.
 
J

Java and Swing

ok i got ctypes...now i try

...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
 
G

Grant Edwards

ok i got ctypes...now i try

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()
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Java said:
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
 
J

Java and Swing

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.
 
F

Fredrik Lundh

Java said:
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>
 
J

Java and Swing

i tried...

i get, SyntaxError: invalid syntax with it pointing at the first "p" in
myapp.pyd.
 
G

Grant Edwards

i tried...


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.
 
F

Fredrik Lundh

Java and Swing said:
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>
 
J

Java and Swing

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.
 
F

Fredrik Lundh

Java and Swing said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top