我需要传递一个Rust字符串的向量到一个C可变函数。但是我不知道期望的(CString
,[u8]
..)格式是什么。
引用:
- API引用:isc_event_block
- 我的外部C声明:ibase
- 如何调用:que_events C示例:api16
- API实现:isc_event_block
我的api16
示例代码版本:
strcpy (ids[0], "evento");
length = (short) isc_event_block((char **) &event_buffer, (char **) &result_buffer, 1, ids[0], 0);
printf("event_buffer: '%s' %dn", event_buffer, sizeof(event_buffer));
printf("result_buffer: '%s' %dn", result_buffer, sizeof(result_buffer));
api16
版本结果:
event_buffer: 'evento' 8
result_buffer: '' 8
我的Rust代码:
let mut event_buffer: Vec<u8> = Vec::with_capacity(256);
let mut result_buffer: Vec<u8> = Vec::with_capacity(256);
let mut len = 0;
let names = "evento".to_string();
unsafe {
len = self.ibase.isc_event_block()(
event_buffer.as_mut_ptr() as *mut _,
result_buffer.as_mut_ptr() as *mut _,
names.len() as u16,
names.as_ptr()
);
event_buffer.set_len(len as usize);
result_buffer.set_len(len as usize);
}
println!("{:?} {:?}", len, names);
println!("{:x?} {:x?}", event_buffer, result_buffer);
println!("{:?} {:?}", String::from_utf8_lossy(&event_buffer.clone()), String::from_utf8_lossy(&result_buffer.clone()));
我的Rust代码结果:
12 ["evento"]
[e0, 4f, 51, 28, a8, 7f, 0, 0, 0, 0, 0, 0] [0, 50, 51, 28, a8, 7f, 0, 0, 0, 0, 0, 0]
"�OQ(�u{7f} " "