I can call my test Rust program with integers as input and handle these fine, even without reference to ctypes. However I cannot seem to get a string without segfaulting in Rust.
Here is my test Rust code:
use std::env;
#[no_mangle]
pub extern fn helloworld(names: &str ) {
println!("{}", names);
println!("helloworld...");
}
#[no_mangle]
pub extern fn ihelloworld(names: i32 ) {
println!("{}", names);
println!("ihelloworld...");
}
ihelloworld works just fine. But I cannot find a way to get a string from python into Rust even if I use ctypes.
Here is the calling Python code:
import sys, ctypes, os
from ctypes import cdll
from ctypes import c_char_p
from ctypes import *
if __name__ == "__main__":
directory = os.path.dirname(os.path.abspath(__file__))
lib = cdll.LoadLibrary(os.path.join(directory, "target/release/libembeded.so"))
lib.ihelloworld(1)
lib.helloworld.argtypes = [c_char_p]
#lib.helloworld(str("test user"))
#lib.helloworld(u'test user')
lib.helloworld(c_char_p("test user"))
print("finished running!")
The output is:
1
ihelloworld...
Segmentation fault (core dumped)
The ihellowworld Rust function works just fine, but I cannot seem to get helloworld working.