Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROCESSENTRY32 szExeFile usage #1845

Closed
scatrunner opened this issue Jun 23, 2022 · 3 comments
Closed

PROCESSENTRY32 szExeFile usage #1845

scatrunner opened this issue Jun 23, 2022 · 3 comments
Labels
question Further information is requested

Comments

@scatrunner
Copy link

I couldn't find an example on how to “correctly” convert Foundation::CHAR slices like szExeFile from ProcessEntry32 to a string.

My current solution looks like this

let mut buf = Vec::with_capacity(260);
for c in process_entry.szExeFile.iter() {
    if c.0 == 0 {
        break;
    }

    buf.push(c.0 as char);
}

let process_name: String = String::from_iter(buf);

Is there a better solution for this or another preferred/intended way on how we should handle these?

@tim-weis
Copy link
Contributor

Questions about the use of the windows and windows-sys crates are best delegated to Stack Overflow.

As for the specific question: You shouldn't be using Foundation::CHAR here at all. Process32First returns strings encoded as ANSI (CP_ACP by default). That can quickly turn into sequences of bytes that do not represent valid UTF-8, and String::from_iter will respond with a panic!.

Instead, call Process32FirstW, which will populate a PROCESSENTRY32W structure. You can now convert the &[u16] into an OsString, if you so choose, or keep a 'raw' Vec<u16> around.

Rust just doesn't offer a convenient way to represent strings as used by Windows (UTF-16, but not necessarily well-formed). String handling is always going to feel unnatural with Rust on Windows: You'll either have lots of OsStrings, lots of fallible operations, or lots of opportunities for panic!s.

@rylev
Copy link
Contributor

rylev commented Jun 24, 2022

Note: there are some suggestions for making this marginally better in #1848. Please give your feedback there as we try to make string handling a little less painful.

@kennykerr kennykerr added the question Further information is requested label Jun 24, 2022
@kennykerr
Copy link
Collaborator

Here's an example for a similar API: #1837 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants