【发布时间】:2017-01-24 20:57:19
【问题描述】:
我有一个通过 C FFI(通过 winapi-rs)利用 Windows API 的程序。其中一个函数需要一个指向字符串的指针作为输出参数。该函数会将其结果存储到此字符串中。我为此字符串使用WideCString 类型的变量。
我可以“只是”将一个可变引用传递给一个字符串引用到这个函数(在一个不安全的块内)还是应该使用像.into_raw()和.from_raw()这样的功能,它也会移动C函数的变量?
两个版本都可以编译和工作,但我想知道直接方式是否有任何缺点。
以下是我的代码中使用 .into_raw 和 .from_raw 的相关行。
let mut widestr: WideCString = WideCString::from_str("test").unwrap(); //this is the string where the result should be stored
let mut security_descriptor_ptr: winnt::LPWSTR = widestr.into_raw();
let rtrn3 = unsafe {
advapi32::ConvertSecurityDescriptorToStringSecurityDescriptorW(sd_buffer.as_mut_ptr() as *mut std::os::raw::c_void,
1,
winnt::DACL_SECURITY_INFORMATION,
&mut security_descriptor_ptr,
ptr::null_mut())
};
if rtrn3 == 0 {
match IOError::last_os_error().raw_os_error() {
Some(1008) => println!("Need to fix this errror in get_acl_of_file."), // Do nothing. No idea, why this error occurs
Some(e) => panic!("Unknown OS error in get_acl_of_file {}", e),
None => panic!("That should not happen in get_acl_of_file!"),
}
}
let mut rtr: WideCString = unsafe{WideCString::from_raw(security_descriptor_ptr)};
description of this parameter in MSDN 说:
指向变量的指针,该变量接收指向以 null 结尾的安全描述符字符串的指针。有关字符串格式的说明,请参阅Security Descriptor String Format。要释放返回的缓冲区,请调用 LocalFree 函数。
我期待函数改变变量的值。这不——根据定义——意味着我正在转移所有权吗?
【问题讨论】:
-
在这种情况下,指向 C 中字符串指针的指针是什么?
WCHAR**?该函数说它将对此做什么?也许您可以告诉我们您要调用哪个函数,以便我们在 MSDN 中查找。 -
@Shepmaster 不是真的,因为我仍然不知道 C 端函数 想要什么。
-
短版,显示一些代码。
-
我刚刚从我的代码中添加了一个 sn-p(一个版本)。 MSDN 中对这个参数的描述说:一个指向变量的指针,它接收指向一个以空值结尾的安全描述符字符串的指针。有关字符串格式的说明,请参阅安全描述符字符串格式。要释放返回的缓冲区,请调用 LocalFree 函数。
-
@Shepmaster 我希望该函数能够更改变量的值。这不是 - 根据定义 - 意味着我正在转移所有权吗?
标签: rust ffi c-strings widestring