I've found A little C with your Rust - The Embedded Rust Book which teaches how to pass a C Struct to Rust. However, it uses the cty crate, which have to be generated by some kind of script.
I want to do things more easily as there is very few things I need to pass. Just some strings (char*
) and numbers.
I already sucessfully passed a single uint8_t
from C to Rust.
I'm now trying this on the Rust side:
#[repr(C)]
pub struct VPNParameters {
pub address: *mut c_char,
pub address_size: usize,
pub x: c_int,
}
#[no_mangle]
pub extern "C" fn passParameters(vpnParameters: *mut VPNParameters)
{
//error: says "vpnParameters" has no address field
println!("{}", vpnParameters.address);
}
and on C++:
struct VPNParameters {
char* address;
size_t address_size;
int x;
} VPNParameters;
extern "C" void passParameters(VPNParameters* vPNParameters);
I think it's something like that. But why I can't access the struct members on Rust? And possibly it won't work either, I may need to convert the char to a string.
I guess this would work:
println!("{}", unsafe { *vPNParameters.address(i as isize) });