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

Request for implementations of From/Into to convert between PWSTR/PCWSTR and OsString/&OsStr #710

Closed
enebo opened this issue Apr 17, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@enebo
Copy link

enebo commented Apr 17, 2021

Similar to #645. I am working with raw command-line info as OsStr/OsString and I want to pass that to CreateProcessW. Here is mostly the gist of how I am dealing with PWSTR now:

    let command_line = quote_vec(command_line); // OsString
    let mut command_line_wide: Vec<u16> = OsStr::new(&command_line).encode_wide().chain(once(0)).collect();
    let mut c = PWSTR::default();
    c.0 = command_line_wide.as_mut_ptr();

c is passed as second param to CreateProcessW but it would be nice to just to pass command_line itself.

@rylev
Copy link
Contributor

rylev commented Apr 20, 2021

This should be safe to do so I think we should it. We do allow compilation on Linux, so we should be careful to document things properly as if we ever allow for the use of this crate on all platforms, this will not work on Linux.

@enebo
Copy link
Author

enebo commented Apr 20, 2021

@rylev Does windows-rs work on non-windows platforms at all? I can see you may allow cross-compilation so you can build the crate on linux but I assumed it would still have OsStr as u16 for the sake of that compilation.

@kennykerr kennykerr added the enhancement New feature or request label Apr 20, 2021
@rylev
Copy link
Contributor

rylev commented Apr 21, 2021

@enebo currently the crate is only useable on Windows (though it builds on Linux). This might be the case forever, but it might not be. I personally don't want to get in a situation where we have very subtle reliance on platform specific behavior where a comment could easily make that apparent.

@ibigbug
Copy link

ibigbug commented May 2, 2021

I had the same question today and found this example https://github.com/microsoft/windows-rs/blob/master/examples/clock/src/main.rs#L414

So you probably don't need to do the conversion and can pass in the &str directly

@MarijnS95
Copy link
Contributor

currently the crate is only useable on Windows (though it builds on Linux)

Following https://github.com/microsoft/windows-rs/discussions/566 this crate is perfectly usable on Linux and allows to retrieve an interface from DXC without hassle. I hope to share a working example in the coming days :)

Wide strings are tricky because they are 32-bit on Linux platforms, and even DXC treats them like that. The widestring crate could be used to abstract this away, and otherwise windows-rs implementations should be careful to add #[cfg] so that it won't compile on Linux instead of silently doing the wrong thing.
Regardless, for DXC support to be complete this crate will need string helpers implemented natively in Rust when Windows libraries are not available, like we do currently in https://github.com/Traverse-Research/hassle-rs/blob/dxc-prefix-sized-BSTR/src/os.rs. Downside is, these implementations depend on how DXC shims them in the first place, see also microsoft/DirectXShaderCompiler#3265. But that's all for a different discussion; don't want to derail this thread too much, just making you aware.

@kennykerr
Copy link
Collaborator

The windows crate now provides conversions for OsString/OsStr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants