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

TranslateMessage falsely gives error #3145

Closed
JunkuiZhang opened this issue Jul 4, 2024 · 6 comments
Closed

TranslateMessage falsely gives error #3145

JunkuiZhang opened this issue Jul 4, 2024 · 6 comments
Labels
question Further information is requested

Comments

@JunkuiZhang
Copy link

Summary

The code TranslateMessage(&msg).ok() will give Err(), which is wrong.

Crate manifest

No response

Crate code

No response

@JunkuiZhang JunkuiZhang added the bug Something isn't working label Jul 4, 2024
@JunkuiZhang
Copy link
Author

Screenshot 2024-07-04 160412

The error message is "Operation successful";

@JunkuiZhang
Copy link
Author

By the way, I noticed some chages from isize to *mut cvoid, for example, HWND, HANDLE, HMONOTOR, HMODULE. And this change makes them thread unsafe, I have to manually unsafe impl Sync Send ... for them. Is this intended?

@tim-weis
Copy link
Contributor

tim-weis commented Jul 4, 2024

The code TranslateMessage(&msg).ok() will give Err(), which is wrong.

No, that is correct. The return value of TranslateMessage indicates whether the API processed the message. It is not a success/error discriminator. The issue is converting the BOOL to a Result<T> via ok().

The conventional use pattern for this API is to check whether the message was processed and pass it on to DispatchMessageW() if not, something like this:

if !TranslateMessage(&msg).as_bool() {
    DispatchMessageW(&msg);
}

@riverar riverar added question Further information is requested and removed bug Something isn't working labels Jul 4, 2024
@riverar
Copy link
Collaborator

riverar commented Jul 4, 2024

Thanks Tim, beat me by a few seconds!

By the way, I noticed some chages from isize to *mut cvoid, for example, HWND, HANDLE, HMONOTOR, HMODULE. And this change makes them thread unsafe, I have to manually unsafe impl Sync Send ... for them. Is this intended?

This was a recent change (#3111). This now matches the Rust standard handle type std::os::windows::raw::HANDLE.

@riverar riverar closed this as completed Jul 4, 2024
@riverar
Copy link
Collaborator

riverar commented Jul 4, 2024

I do wonder if there's any value in having an .ok() impl for BOOL, given it unexpectedly calls GetLastError which could contain anything and confuse folks further.

@JunkuiZhang
Copy link
Author

JunkuiZhang commented Jul 4, 2024

@tim-weis Thank you for your reply. The .ok() really confuses here, I thought it checks BOOL and reports Err if return false for me.

This was a recent change (#3111). This now matches the Rust standard handle type std::os::windows::raw::HANDLE.

Thanks for that information. And, my idea is that the HWND, HANDLE and all these should be thread safe, right? So, can windows-rs unsafe impl Send + Sync for them, so I dont have to impl myself?

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

3 participants