0

I'm trying to call a main() from open-plc-utils/slac/evse.c via ctypes. Therefor I turned the c file into a shared object (.so) and called it from Python.

from ctypes import *

so_file = "/home/evse/open-plc-utils/slac/evse.so"

evse = CDLL(so_file)

evse.main.restype = c_int
evse.main.argtypes = c_int,POINTER(c_char_p)
args = (c_char_p * 1)(b'd')
evse.main(len(args),args)

The d should return debug output but the output in the stdout is the same no matter which letter I pass to main(). Do you know what I did wrong here? And how can I pass something like

evse -i eth1 -p evse.ini -c -d 

in one comand via ctypes?

c0nr3f
  • 87
  • 7
  • I suggest to modify the `main` function to print all arguments to see what you actually passed to it. Example: `for(int i = 0; i < argc; i++) { printf("argv[%d] = %s\n", i, argv[i]); }` The `main` function expects the program name as `argv[0]` and the first command line argument as `argv[1]` etc. – Bodo Oct 11 '22 at 14:20

1 Answers1

0

Try the following. Make sure the first argument is the program name.

from ctypes import *

so_file = "/home/main/open-plc-utils/slac/main.so"

evse = CDLL(so_file)

evse.main.restype = c_int
evse.main.argtypes = c_int,POINTER(c_char_p)

def make_args(cmd):
    args = cmd.encode().split()
    return (c_char_p * len(args))(*args)

args = make_args('main -i eth1 -p main.ini -c -d')
evse.main(len(args), args)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251