简体   繁体   中英

How to pass char* to Python

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.

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM