0

I' m trying to import a .so library into a python code to use c functions. I think using

from ctypes import *
import ctypes
lib = CDLL('./libcaenhvwrapper.so.5.56')

is working fine. I need to use some user defined types which are defined in a header file but I cannot access them.

thank you in advance

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
leonardo
  • 15
  • 2

2 Answers2

0

The types are not in the .so file that the ctypes module loads.

C types don't work like that, they're declared in the header and you must have the header to use a (C) library, even from C.

You're going to have to use the various ctypes APIs to re-create the types in Python. See this part of the tutorial, for instance, for how to work with struct and union types.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Now my code works with a normal c library, but I need to use a .so library from Caen and I get Segmentation fault. This is the code:

from ctypes import *
lib = CDLL('./libcaenhvwrapper.so.5.56')
lib.CAENHVInitSystem.restype = c_int
lib.CAENHVInitSystem.argtypes = [c_int, c_int, c_char_p, c_char_p, c_char_p]
lib.CAENHVGetError.restype = c_int    

CAENHV_SYSTEM_TYPE_t = c_int
sy1527 = CAENHV_SYSTEM_TYPE_t(0)
sy2527 = CAENHV_SYSTEM_TYPE_t(1)
sy4527 = CAENHV_SYSTEM_TYPE_t(2)
sy5527 = CAENHV_SYSTEM_TYPE_t(3)
n568 = CAENHV_SYSTEM_TYPE_t(4)
v65xx = CAENHV_SYSTEM_TYPE_t(5)
n1470 = CAENHV_SYSTEM_TYPE_t(6)
v8100 = CAENHV_SYSTEM_TYPE_t(7)

link = c_int
LINKTYPE_TCPIP = link(0)
LINKTYPE_RS232 = link(1)
LINKTYPE_CAENET = link(2)
LINKTYPE_USB = link(3)
LINKTYPE_OPTLINK = link(4)
LINKTYPE_USB_VCP = link(5)

string15=c_char*15
address=string15('1','3','7','.','1','3','8','.','1','3','.','2','0','3','\0')

userName = c_char_p('user')
passwd = c_char_p('user')
ret_init =  lib.CAENHVInitSystem(0, 0, address, userName, passwd)

when I try to call the function I get a segmentation fault. I think the types are correctly defined. Below you can see a piece of code which works ok.

from ctypes import *
lib2 = CDLL('/lib64/libc.so.6')
string15=c_char*15
address=string15('1','3','7','.','1','3','8','.','1','3','.','2','0','3','\0')
address1=create_string_buffer('137.138.13.203')
address2=c_char_p('137.138.13.200')

userName = c_char_p('user')
passwd = c_char_p('user')

a= lib2.strncmp(address, userName, c_int(4))    
a= lib2.strncmp(userName, address, 4)
a= lib2.strncmp(address2, address, 15)

lib2.printf('%d\n', ret_init)
lib2.printf('%s\n', address)
lib2.printf('%s\n', address1)
lib2.printf('%s\n', address2)
lib2.printf('%d\n', lib2.strlen(address))
lib2.printf('%d\n', lib2.strlen(address1))
lib2.printf('%d\n', lib2.strlen(address2))
leonardo
  • 15
  • 2