473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2716
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
22321
by: Dave Kuhlman | last post by:
Is JPE (the Python Java Extension) being used widely/actively? I tried to build it (with Python 2.3.4, j2se 1.4 on Debian GNU/Linux) and had quite a bit of trouble. And, then, the samples did...
0
1166
by: sdhyok | last post by:
PyMat enables to call matlab functions from python. But, what I want is to call python functions from matlab. Is there any library for it? Thanks. Daehyok Shin
2
2986
by: FAN | last post by:
I want to define some function in python script dynamicly and call them later, but I get some problem. I have tried the following: ################################## # code...
19
17485
by: Riko Wichmann | last post by:
hi everyone, I'm googeling since some time, but can't find an answer - maybe because the answer is 'No!'. Can I call a function in python inline, so that the python byte compiler does...
6
1461
by: enjoying the view | last post by:
I am working on a school project, trying to build a simple RPC stub generator. The idea is that the generator takes a normal python file with functions defined in it and produces a client and...
7
8503
by: beginner | last post by:
Hi Everyone, I have encountered a small problems. How to call module functions inside class instance functions? For example, calling func1 in func2 resulted in a compiling error. "my module...
2
2306
by: bappai | last post by:
Hello, I am trying to actually call a GUI from my C++ code which would have buttons and therefore can call functions from C++ again, ie extend the C++ code also. I am faced with a peculiar...
6
2535
by: mh | last post by:
I'm cleaning up some old code, and want to see what orphan functions might be sitting around. Is there a static call tree analyzer for python? Many TIA! Mark -- Mark Harrison
275
12035
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
3566
by: hofsoc20 | last post by:
Hi, I am trying to call a python script (Python 2.5) and implement one of its functions. I have successfully called the script and executed it with jep.runscript("sample.py"). Does anyone...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.