473,657 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

RE: swig or ctypes , under the gun and need help

Diez wrote...
I don't know swig, but if all you have is a real C-API, try &
use ctypes.
It's much easier to create bindings for, keeps you fully in
the warm and
cozy womb of python programming and doesn't need no
compilation to create
the actual binding.
You're right the ctypes does seem more pythonesque; however I'm still stuck trying return all these parameters
that the c api uses. my ctypes code is below. It just quits running when I try to print
one of the args I did a pass byref on, no error out, nothing. admittedly I'm a newbie to ctypes and not much of a c programmer
but I could sure use some help. my ctypes test code follows...

from ctypes import *

'''
create shared object file like so.
gcc -shared -o rug520.so rug520.c

the c api I want to call is like this.
int RugCalc( char * sMdsRecord,
char * sRehabType,
char * sModel,
int iQuarterlyFlag,
double nCmiArray[],
char * sRugHier,
char * sRugMax,
int * iRugHier,
int * iRugMax,
double * nCmiValueHier,
double * nCmiValueMax,
int * iAdlSum,
int * iCpsCode,
char * sRugsVersion,
char * sDllVersion,
int * iError );
'''
libc = CDLL("rug520.so ")

CmiArrayDef = c_double * 59

ZeroCmi = CmiArrayDef( ) #this is a table used internally, but 0.0 should work until I figure out the rest.

def getrug(mds):
#print mds
sMdsRecord = c_char_p()
sRehabType = c_char_p()
sModel = c_char_p()
iQuarterlyFlag = c_int()
sRugHier = c_char_p()
sRugMax = c_char_p()
iRugHier = c_int()
iRugMax = c_int()
nCmiValueHier = c_double()
nCmiValueMax = c_double()
iAdlSum = c_int()
iCpsCode = c_int()
sRugsVersion = c_char_p()
sDllVersion = c_char_p()
iError = c_int()
sMdsRecord.valu e = mds
sRehabType = 'mcare'
sModel = '34'

results = libc.RugCalc(sM dsRecord, sRehabType, sModel, iQuarterlyFlag,
ZeroCmi,
byref(sRugHier) ,
byref(sRugMax),
byref(iRugHier) ,
byref(iRugMax),
byref(nCmiValue Hier),
byref(nCmiValue Max),
byref(iAdlSum),
byref(iCpsCode) ,
byref(sRugsVers ion),
byref(sDllVersi on),
byref(iError ))
print 'results', results
print iQuarterlyFlag. value
print 'sRugMax', sRugMax #this print causes an exit, tried .value with same results
print 'return' #I never see this print.

datafile = open('mdsdata.t xt')
for d in datafile:
if d[0]=='B':
getrug(d)
break


Aug 25 '08 #1
2 1332
Sells, Fred schrieb:
Diez wrote...
>I don't know swig, but if all you have is a real C-API, try &
use ctypes.
It's much easier to create bindings for, keeps you fully in
the warm and
cozy womb of python programming and doesn't need no
compilation to create
the actual binding.
You're right the ctypes does seem more pythonesque; however I'm still stuck trying return all these parameters
that the c api uses. my ctypes code is below. It just quits running when I try to print
one of the args I did a pass byref on, no error out, nothing. admittedly I'm a newbie to ctypes and not much of a c programmer
but I could sure use some help. my ctypes test code follows...

from ctypes import *

'''
create shared object file like so.
gcc -shared -o rug520.so rug520.c
You need to create a proper library. The above doesn't do that - it
creates as simple object file.

I don't know how to really do that without using autoconf/automake.

Diez
Aug 25 '08 #2
Diez B. Roggisch wrote:
Sells, Fred schrieb:
>Diez wrote...
>>I don't know swig, but if all you have is a real C-API, try & use
ctypes.
It's much easier to create bindings for, keeps you fully in the warm and
cozy womb of python programming and doesn't need no compilation to
create
the actual binding.
You're right the ctypes does seem more pythonesque; however I'm still
stuck trying return all these parameters
that the c api uses. my ctypes code is below. It just quits running
when I try to print one of the args I did a pass byref on, no error
out, nothing. admittedly I'm a newbie to ctypes and not much of a c
programmer
but I could sure use some help. my ctypes test code follows...

from ctypes import *

'''
create shared object file like so.
gcc -shared -o rug520.so rug520.c

You need to create a proper library. The above doesn't do that - it
creates as simple object file.

I don't know how to really do that without using autoconf/automake.

Diez
--
http://mail.python.org/mailman/listinfo/python-list
==
see info gcc
search shared (3.13 Options for Linking)

mkso.scr:
#!/bin/bash
# create a lib.so from param 1 filename
#
#vi $1
f=`basename $1 .c`
gcc -c -fPIC $f
ld -shared -fPIC $f.o -o $f.so
# end of file
I suppose $1 could be `cat *.c`
Haven't tried that, but.... Who knows?
(I write my libs to be libs.)

Steve
no******@hughes .net
Aug 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3615
by: Harish | last post by:
I am using Python2.3 version with SWIG-1.3.19 to access C++ interface provided by a DLL. Some of its function returns MFC class objects like CString , CMap. I tried to generate wrapper for these classes using SWIG by including respective header files in .i file. But it doesn't work. Swig reports hundreds parsing errors in MFC's file. Could you give me any directions.. Thank you in anticipation
2
3266
by: zapazap | last post by:
Dear Snake Charming Gurus, (Was: http://mail.python.org/pipermail/python-list/2004-January/204454.html) First, a thank you to Tim Golden, Thomas Heller, and Mark Hammond for your earlier help with this problem. I am uncertain about what etiquette calls for, but more on that later. My Objective: I am trying to control the _VMWare Desktop_ application
19
2487
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call functions exposed from dlls/shared libraries and has extensive facilities to create, access and manipulate
1
2173
by: Frank | last post by:
Hi, I have the following problem: I want to parse an array M1 from python to a cpp function fct which returns an array M2. How can I do this best? Is SWIG appropriate or is there something else?
9
4601
by: Daniel Watrous | last post by:
Hello, I am interested in using python to script access to some hardware for which there are existing drivers in the form of DLLs. The DLLs each have four exported functions and a host of COM Properties and COM Methods. The four exported functions are as follows: DllCanUnloadNow DllGetClassObject DllRegisterServer DllUnregisterServer
0
1134
by: gianluca | last post by:
I've a problem with dll function colled with python/ctypes. My functions (C) requred a typedef int "value_type" in tree different way: same as value_type; - mydll.foo1(value_type) same as *value_type; - mydll.foo2(*value_type) same as **value_type; - mydll.foo3(**value_type) How can pass it in python. If i do that: rules=POINTER(value_type) opr=rsl.StrengthOfRules(rules,10)
2
16504
by: Jean-Paul Calderone | last post by:
On Mon, 30 Jun 2008 09:13:42 -0700 (PDT), gianluca <geonomica@gmail.comwrote: POINTER takes a class and returns a new class which represents a pointer to input class. pointer takes an instance and returns a new object which represents a pointer to that instance. Jean-Paul
3
1100
by: Anish Chapagain | last post by:
Hi.. I'm new to SWIG and need to create Wrapper for C code, so, I have installed the SWIG already but doesnot know how to run it for generating Interface file... My C code is in message.c so what do i need to do the first step..uisng SWIG..i read the documentation but cannot grasp creating interface file. thank's anish
1
1659
by: Sells, Fred | last post by:
I'm using python 2.4 under linux (centos 5.1). I need to pass an array of doubles to a c function but am getting an error, shown near the bottom of this post. ---------------------------------------------------- my swig interface file looks like this * File: rug520.i */ %module rug520 %include "typemaps.i"
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8610
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6174
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4168
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.