How to pass char* to Python

Viewed 127

I need to pass two Python strings, representing an input file and an output file, to a C function named decrypt_file.

If I hardcode the string (for example, 'Test.OUT'), then it works. I don't know how to use a variable string. The C function returns a string with wrong characters.

int decrypt_file(char *inputfile, char *outputfile);
try:
    file_name = bytes("example.txt", encoding='utf8')

    p_file_name = ctypes.create_string_buffer(file_name, len(file_name))
    so = "/home/hello/lib.so"
    sodium = ctypes.CDLL(so)
    sodium.strfry(p_file_name)
    sodium.decrypt_file.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
    sodium.decrypt_file(p_file_name, ctypes.c_char_p(b"Test.OUT"))
except Exception as e:
    print(e)
return ->  input file: mlxta.txeep

I use Linux and Pycharm terminal. Thanks for any tips.

1 Answers

ctypes was wrong. I don't know if this is the right way but it works.

filename = b"helloworld.txt"
so = "/home/lib.so"
sodium = ctypes.CDLL(so)
sodium.decrypt_file.argtypes = [ctypes.POINTER(ctypes.c_char), ctypes.c_char_p]
sodium.decrypt_file(filename, ctypes.c_char_p(b"Test.OUT"))
Related