Passing Rust String to C Function

Hello.
I have a problem with this function:

use libc::{c_char, c_uchar, c_void};
use std::ffi::{CStr};

// C Function
extern {
    fn cbGetObjectName(p_arg: *mut c_void, p_name: *mut c_char) -> c_uchar;
}

pub fn cb_get_object_name(cb_args: *mut c_void) -> Result<String, String>
{
	let mut char_value = [0 as c_char; 255];

	let name = unsafe { 
	    cbGetObjectName(cb_args, char_value.as_mut_ptr()); 
	    CStr::from_ptr(char_value.as_ptr())
	};

	return Ok(name.to_string_lossy().to_string());
}

This function is working correct on x86_64 but not working on ARM.
How to fix it?

In what way is it failing? Have you tried examining the contents of char_value after the FFI call returns?

Returned String is empty. Array char_value have 0-values.

I think the problem must lie outside of this Rust code. You'll need to figure out what's happening inside cbGetObjectName that's causing it to write nothing, or an empty name. Maybe also examine what's getting passed in cb_args.

C function cbGetObjectName - Shortcuts:

  std::string full_name = "TEST";
  LOG_info("cbGetObjectName full_name: %s\n", full_name.c_str());
  strcpy(p_name, full_name.c_str());

Function LOG_info prints full_name correct.
It's strange, because the code works on x86_64.

Yeah, I don't see any way that could fail. At this point I would start to suspect that I'm running the wrong code somehow, maybe a version that was compiled from an older or different source. Can you double-check that the source files for your ARM device exactly match the ones you are testing on x86_64? And maybe delete all compiled artifacts and rebuild both the C and Rust code from scratch?

How are you compiling and linking the C code, and how are you deploying things to your ARM machine?

I did it.

Maybe that's the problem: use libc::{c_char, c_uchar, c_void};

The only difference there is that c_char is signed on x86_64 and unsigned on ARM. But that shouldn't affect any of your code.

Can you step through your program under a debugger on ARM?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.