diff --git a/lib/libp4pillondebuglib.a b/lib/libp4pillondebuglib.a index f36c97a..e31fd85 100644 Binary files a/lib/libp4pillondebuglib.a and b/lib/libp4pillondebuglib.a differ diff --git a/lib/p4pillondebuglib.o b/lib/p4pillondebuglib.o index 4a1decf..3711dc7 100644 Binary files a/lib/p4pillondebuglib.o and b/lib/p4pillondebuglib.o differ diff --git a/src/main.rs b/src/main.rs index 6526ca4..69018ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,13 @@ extern crate libc; use libc::{ c_char, c_int, c_void, size_t }; -use std::ffi::CStr; +use std::ffi::{ CStr, CString }; use std::ptr; -extern { +#[link(name = "p4pillondebuglib")] +extern "C" { fn hello() -> *const c_char; + fn helloName(name: *const c_char) -> *const c_char; fn helloPtr(result: *mut c_char); fn helloPtrPtr(result: *mut *mut c_char, size: *mut size_t); fn helloVoidPtrPtr(result: *mut *mut c_void, size: *mut size_t); @@ -22,6 +24,15 @@ fn main() { println!("{} from C", r_str); } + let name = CString::new("John").expect("CString::new failed"); + unsafe { + let c_str = helloName(name.as_ptr()); + let r_str = CStr::from_ptr(c_str).to_str().expect("Conversion failed"); + println!("{} from C with name", r_str); + + libc::free(c_str as *mut c_void); + } + let mut buffer: [c_char; 50] = [0; 50]; unsafe { helloPtr(buffer.as_mut_ptr()); diff --git a/src/p4pillondebuglib.c b/src/p4pillondebuglib.c index 22348af..d9038c6 100644 --- a/src/p4pillondebuglib.c +++ b/src/p4pillondebuglib.c @@ -7,6 +7,14 @@ char* hello() { return "Hello, World!"; } +char* helloName(const char *name) { + char *result = (char *)malloc(strlen(name) + 9); + if (result) { + sprintf(result, "Hello, %s!", name); + } + return result; +} + void helloPtr(char *result) { sprintf(result, "Hello, World!"); } diff --git a/src/p4pillondebuglib.h b/src/p4pillondebuglib.h index 462098f..c839174 100644 --- a/src/p4pillondebuglib.h +++ b/src/p4pillondebuglib.h @@ -4,6 +4,7 @@ #include char* hello(); +char* helloName(const char *name); void helloPtr(char *result); void helloPtrPtr(char **result, size_t *size); void helloVoidPtrPtr(void **result, size_t *size);