I have a Windows DLL, created in Delphi, which exposes a function with a contract
procedure analyys (p : pchar; len : word); far stdcall external 'ana.dll';
As this is a "procedure", result is written into the same area of memory as the first argument (it's a nul-terminated CP1257 string). And size of this area is provided in the second argument.
And it seems that Rust has so many possibilities here that I really struggle to choose: array, Vec, CStr/CString, &str/String, slices, Box (?)..
It seems like an idiotic question, but I really could not google my way out of it here. So basically what I have right now is a Vec, returned by "encode" crate method, and I have to allocate 4096 bytes of area,
write those Vec bytes into the beginning of the area, zero-fill the rest, call DLL function, then copy first X bytes till the first 0 to Vec, pass it to "decode" crate method, turn into String, and return.
Currently I do it like this:
fn analyze_encoded(encoded_word: Vec<u8>) -> Result<String, String> {
let mut text: [u8; 4096] = [0; 4096];
let mut i = 0;
for c in encoded_word {
text[i] = c;
i += 1;
}
const LEN: u16 = 4095;
if let Err(e) = analyze_dll(&text[0] as *const u8, LEN) {
return Err(e.description().to_string());
}
match WINDOWS_1257.decode(&text, DecoderTrap::Strict) {
Ok(s) => Ok(s),
Err(e) => Err(e.into_owned())
}
}
I cannot check if the code works because of some weird cargo issue (can not build Windows 32-bit executable with Cargo 1.32.0 on Windows 10 x64 · Issue #6754 · rust-lang/cargo · GitHub) but I still wonder, if this is actually correct?
I've seen copy_from_slice/clone_from_slice, but they seem to demand same size for both parties. So, how do I place content of a slice (Vec) on an array, and vice versa ?