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

Bug: Pointer types differ in mutability #1490

Closed
balatamoghna opened this issue Feb 2, 2022 · 4 comments
Closed

Bug: Pointer types differ in mutability #1490

balatamoghna opened this issue Feb 2, 2022 · 4 comments
Labels
bug Something isn't working

Comments

@balatamoghna
Copy link

Which crate is this about?

windows

Crate version

0.30.0

Summary

use windows::{core::*, Win32::Foundation::*, Win32::Graphics::Gdi::ValidateRect, Win32::System::LibraryLoader::GetModuleHandleA, Win32::UI::WindowsAndMessaging::*};

fn main() -> Result<()> {
    unsafe {
        let instance = GetModuleHandleA(None);
        debug_assert!(instance.0 != 0);

        let window_class = "window";

        let wc = WNDCLASSA {
            hCursor: LoadCursorW(None, IDC_ARROW),
            hInstance: instance,
            lpszClassName: PSTR(b"window\0".as_ptr()),

            style: CS_HREDRAW | CS_VREDRAW,
            lpfnWndProc: Some(wndproc),
            ..Default::default()
        };

        let atom = RegisterClassA(&wc);
        debug_assert!(atom != 0);

        CreateWindowExA(Default::default(), window_class, "This is a sample window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, None, None, instance, std::ptr::null_mut());

        let mut message = MSG::default();

        while GetMessageA(&mut message, HWND(0), 0, 0).into() {
            DispatchMessageA(&message);
        }

        Ok(())
    }
}

extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
    unsafe {
        match message as u32 {
            WM_PAINT => {
                println!("WM_PAINT");
                ValidateRect(window, std::ptr::null());
                LRESULT(0)
            }
            WM_DESTROY => {
                println!("WM_DESTROY");
                PostQuitMessage(0);
                LRESULT(0)
            }
            _ => DefWindowProcA(window, message, wparam, lparam),
        }
    }
}

Running the basic sample for create-window gives the error:
"expected raw pointer *mut u8
found raw pointer *const u8"

This seems to be the offending line: lpszClassName: PSTR(b"window\0".as_ptr()),
The function .as_ptr() gives a *const u8 but the expected type for lpszClassName is *mut u8.

My Cargo.toml file has:
[dependencies.windows]
features = [
"alloc",
"Win32_Foundation",
"Win32_Graphics_Gdi",
"Win32_System_LibraryLoader",
"Win32_UI_WindowsAndMessaging",
]

Expected behavior

I expected the sample create-windows to work

Actual behavior

I got a compiler error: error[E0308]: mismatched types

Additional comments

No response

@balatamoghna balatamoghna added the bug Something isn't working label Feb 2, 2022
@balatamoghna
Copy link
Author

Update: You can still cast the *const u8 pointer to *mut u8 ptr and it'll work.

Is this the solution for the bug? Is there any unintended behavior expected if we do this?

@tim-weis
Copy link
Contributor

tim-weis commented Feb 2, 2022

This seems to be the same issue as #777. In that case the upcoming release of 0.31.0 will address this. The recently imported metadata version 17.0.9 provides the appropriate [Const] annotations for the respective fields in the WNDCLASSA structure.

@balatamoghna
Copy link
Author

Thank you!

@kennykerr
Copy link
Collaborator

Yes, this was fixed with #1470.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants