1

The Windows API has functions like GetWindowTextW that read a string into a buffer you provide. How do I provide such a buffer and then read a string from it in Rust?

P.S. In contrast to this question Calling the GetUserName WinAPI function with a mutable string doesn't populate the string this question focuses on string reading on Windows in general, and not a particular problem. Also, this question will hopefully be easily googled by keywords, answering what was asked for.

VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62
  • @IInspectable I don't get it really. How can this question be considered the same as "Calling the GetUserName WinAPI function with a mutable string doesn't populate the string"? The other title has nothing to do with what asked here. Only half of the answer code is similar. I raised both the question and made an answer explicitly to make it easy to find and easy to use. – VasiliNovikov Oct 08 '18 at 07:31
  • There is no substantial difference between this question and the duplicate. Both try to access Windows APIs with equivalent interfaces, comprised of a pointer to buffer/size of buffer pair. If you wanted to provide a canonical Q&A, you should have asked the generic question: How do you call a C API that returns a character string into a caller-provided buffer? If you insist, that this is not a duplicate, please explain the substantial difference. – IInspectable Oct 08 '18 at 08:02
  • 1
    also note that duplicate mean there is answer is in the other question that answer this question, duplicate doesn't mean your question is the exact same than the other. "This question already has an answer here" not "This question has already been asked here" – Stargateur Oct 08 '18 at 08:42
  • Well OK, as you say. But I had serious trouble googling out this question, spent more than 2 hours opening multiple tabs and SO questions, experimenting with code, traversing stdlib code etc. The "calling GetUserName WinAPI .... doesn't populate a string" just doesn't speak, to my tastes. Maybe it's a problem of my understanding. – VasiliNovikov Oct 08 '18 at 12:29
  • 4
    @VasyaNovikov that's why SO has duplicates at all. This question will now show up in searches for the terms that you used, and it will redirect people to the existing answer. This avoids fragmentation of answers while still increasing searchability. – Shepmaster Oct 08 '18 at 12:49
  • @VasyaNovikov: It's also why, even though the question is closed, you can still receive upvotes for it. Such a redirection sign is welcome and helpful, thank you for your efforts in setting up. – Matthieu M. Oct 08 '18 at 13:00
  • I guess from this point of view, it could make sense. IDK whether the restriction of "no new answers" could have any downsides -- but maybe no. Anyway, at least one possible answer should surely be googleable now, so main point achieved anyway.) – VasiliNovikov Oct 08 '18 at 16:13

1 Answers1

0

Example:

#[cfg(target_os = "windows")]
fn get_window_name(max_size: i32) -> String {
    let mut vec = Vec::with_capacity(max_size as usize);
    unsafe {
        let hwnd = user32::GetForegroundWindow();
        let err_code = user32::GetWindowTextW(hwnd, vec.as_mut_ptr(), max_size);
        assert!(err_code != 0);
        assert!(vec.capacity() >= max_size as usize);
        vec.set_len(max_size as usize);
    };
    String::from_utf16(&vec).unwrap()
}

Solution taken from Calling the GetUserName WinAPI function with a mutable string doesn't populate the string

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62