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
Create PWSTR and PSTR from literals #581
Comments
Could something like this work? https://github.com/Lokathor/utf16_lit |
Yes, I also have my own crate that does this https://github.com/rylev/const-utf16 which introduced const compilation a bit before |
Do we need to use a macro? It would be nice if we could (also) write a const function to do this so you can write something like this: let s = PSTR::from("hello"); It would somehow have to distinguish between a string literal and a regular string reference. I'm not sure if that's possible. |
Any function that takes a Functions cannot distinguish literals in their input (although you can get close with For example, to convert a literal |
There is an issue with the fact that |
What's the current idiomatic way of creating a PWSTR from a string literal? I used this but it doesn't appear to work when I give it to a function that needs a pwstr for an input: let text = String::from("my pwstr literal").encode_utf16().collect::<Vec<u16>>();
let mypwstr = PWSTR(text.as_mut_ptr()); |
The API is likely assuming a null terminator. |
Yep, I'd recommend using a crate like |
The null terminator is no doubt part of it but actually it looks like the trait constraints of the way some functions are now defined don't allow for null pointers when the API expects them in some cases, which is where I was actually stuck (the compiler gave an ambiguous message that made me think I was using the wrong type at first.) Namely this: https://docs.microsoft.com/en-us/windows/win32/api/adshlp/nf-adshlp-adsopenobject Where in the example they pass a null pointer to lpzusername and lpzpassword. But in the windows-rs crate it MUST be PWSTR per the type constraints: So my previous way of simply passing a ptr::null_mut() doesn't work with the newest version of windows-rs when I want passive authentication. Is there another way of doing that? Sorry may be off topic, I can create a separate issue if required. |
Here's an example (the last parameter is effectively a null pointer value). windows-rs/examples/event/src/main.rs Line 10 in 87f3211
|
Ah I figured that was more analogous to an empty string than a null pointer, but at first glance it seems to work, thanks! |
Might be late to the party, but I got around it using my own impl: impl SystemServices::PWSTR {
fn from(text: &'static str) -> Self {
Self(text.encode_utf16().chain(::std::iter::once(0)).collect::<Vec<u16>>().as_mut_ptr())
}
} Then used it like so: let class_name = SystemServices::PWSTR::from("Hello world?"); |
That creates a |
Playing with an implementation of this here: https://github.com/microsoft/windows-rs/compare/utf16?expand=1 |
#1891 introduced string literal macros that address this issue. |
Right now literals need to be copied into new buffers every time one is passed into a function that expects a
PWSTR
. Instead we should provide some type of macro convenience for creatingPSTR
andPWSTR
from literals. These would encode the literals properly and add a trailing\0
at the end all at compile time. Bonus points if we can make macro that works for either kind of string pointer.The text was updated successfully, but these errors were encountered: