Cant create window with the windows crate, error produced

use windows::core::{s, w, PCWSTR};
use windows::Win32::UI::WindowsAndMessaging::*;
use windows::Win32::UI::WindowsAndMessaging::WNDCLASSW;
use windows::Win32::Graphics::Gdi::HBRUSH;
use std::ffi::c_void;
use windows::Win32::UI::WindowsAndMessaging::RegisterClassW;
use windows::Win32::UI::WindowsAndMessaging::CreateWindowExW;
use windows::Win32::UI::WindowsAndMessaging::WINDOW_EX_STYLE;
use windows::Win32::UI::WindowsAndMessaging::WINDOW_STYLE;
use windows::Win32::UI::WindowsAndMessaging::ShowWindow;
use windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::WPARAM;
use windows::Win32::Foundation::LPARAM;
use windows::Win32::Foundation::LRESULT;
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::UI::WindowsAndMessaging::{WS_OVERLAPPEDWINDOW, WS_VISIBLE};

unsafe extern "system" fn proc(_: HWND, _: u32, _: WPARAM, _: LPARAM) -> LRESULT {
    println!("procedure was caled");
    LRESULT(0)
}

unsafe fn main_unsafe() {
    let instance = GetModuleHandleW(None).unwrap().into();
    let class_name = w!("WINDOW_CLASS_QUARTZ");
    let class = WNDCLASSW {
        style: Default::default(),
        lpfnWndProc: Some(proc),
        cbClsExtra: 0,
        cbWndExtra: 0,
        hInstance: instance,
        hIcon: Default::default(),
        hCursor: LoadCursorW(None, IDC_ARROW).unwrap(),
        hbrBackground: HBRUSH::default(),
        lpszMenuName: w!("hi"),
        lpszClassName: class_name,
    };

    RegisterClassW(&class);

    let hwnd = CreateWindowExW(
        WINDOW_EX_STYLE::default(),
        class_name,
        w!("hello world"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        100,
        100,
        500,
        300,
        None,
        None,
        instance,
        None
    ).unwrap();

    ShowWindow(hwnd, SHOW_WINDOW_CMD(5));
}

fn main() {
    unsafe { main_unsafe() };
}

I get

procedure was caled
procedure was caled
procedure was caled
thread 'main' panicked at jdrift-interface\examples\winbuffer.rs:54:7:
called `Result::unwrap()` on an `Err` value: Error { code: HRESULT(0x80070714), message: "The specified image file did not contain a resource section." }
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/06bb8364aaffefb0ce67e5f5445e66ec99c1f66e\library/std\src\panicking.rs:665
   1: core::panicking::panic_fmt
             at /rustc/06bb8364aaffefb0ce67e5f5445e66ec99c1f66e\library/core\src\panicking.rs:74
   2: core::result::unwrap_failed
             at /rustc/06bb8364aaffefb0ce67e5f5445e66ec99c1f66e\library/core\src\result.rs:1695
   3: enum2$<core::result::Result<windows::Win32::Foundation::HWND,windows_result::error::Error> >::unwrap
             at C:\Users\Rayyan K\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:1103
   4: winbuffer::main_unsafe
             at .\jdrift-interface\examples\winbuffer.rs:41
   5: winbuffer::main
             at .\jdrift-interface\examples\winbuffer.rs:60
   6: core::ops::function::FnOnce::call_once<void (*)(),tuple$<> >
             at C:\Users\Rayyan K\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:250
   7: core::hint::black_box
             at C:\Users\Rayyan K\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\hint.rs:389
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
error: process didn't exit successfully: `target\debug\examples\winbuffer.exe` (exit code: 101)

Process finished with exit code 101

It seems to be complaing about the icon in WNDCLASSW but Im not sure, especially since many others have just used default. I am also not sure how to make an icon here.

According to the relevant win32 docs you'd need RegisterClassExW if you want a small window icon, might try that? Or perhaps try changing CreateWindowExW to the non-Ex version, as that seems to be the only Ex call you have.

I personally had some issues with a few of the widestring APIs as well, especially for strings not known at compile time. You could try and see if the ANSI versions are easier to get to play nice

I tried to change everything to Ex callsand it compiled,but i still have the same issue

use windows::core::{s, w, PCWSTR};
use windows::Win32::UI::WindowsAndMessaging::*;
use windows::Win32::UI::WindowsAndMessaging::WNDCLASSEXW;
use windows::Win32::Graphics::Gdi::HBRUSH;
use std::ffi::c_void;
use windows::Win32::UI::WindowsAndMessaging::RegisterClassExW;
use windows::Win32::UI::WindowsAndMessaging::CreateWindowExW;
use windows::Win32::UI::WindowsAndMessaging::WINDOW_EX_STYLE;
use windows::Win32::UI::WindowsAndMessaging::WINDOW_STYLE;
use windows::Win32::UI::WindowsAndMessaging::ShowWindow;
use windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::WPARAM;
use windows::Win32::Foundation::LPARAM;
use windows::Win32::Foundation::LRESULT;
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::Foundation::GetLastError;
use windows::Win32::UI::WindowsAndMessaging::{WS_OVERLAPPEDWINDOW, WS_VISIBLE};

unsafe extern "system" fn proc(_: HWND, _: u32, _: WPARAM, _: LPARAM) -> LRESULT {
    println!("procedure was caled");
    LRESULT(0)
}

unsafe fn main_unsafe() {
    let instance = GetModuleHandleW(None).unwrap().into();
    let class_name = w!("WINDOW_CLASS_QUARTZ");
    let class = WNDCLASSEXW {
        cbSize: size_of::<WNDCLASSEXW>() as u32,
        style: Default::default(),
        lpfnWndProc: Some(proc),
        cbClsExtra: 0,
        cbWndExtra: 0,
        hInstance: instance,
        hIcon: Default::default(),
        hCursor: LoadCursorW(None, IDC_ARROW).unwrap(),
        hbrBackground: HBRUSH::default(),
        lpszMenuName: w!("hi"),
        lpszClassName: class_name,
        hIconSm: Default::default()
    };

    let class_result = RegisterClassExW(&class);
    if class_result == 0 {
        println!("failed to make class");
        dbg!(GetLastError());
        return;
    }

    let hwnd = CreateWindowExW(
        WINDOW_EX_STYLE::default(),
        class_name,
        w!("hello world"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        100,
        100,
        500,
        300,
        None,
        None,
        instance,
        None
    ).unwrap();

    ShowWindow(hwnd, SHOW_WINDOW_CMD(5));
}

fn main() {
    unsafe { main_unsafe() };
}

This error sounds like it's saying that your binary doesn't contain anything from the Resource Compiler, which is where several things like the application icon and menu layouts get stored.

I'm guessing it's complaining about the menu name being specified: that's the name of a menu resource.

I figured out that the docs say it wants the HICON fields to have null for default, Default::default isnt using null it seems.

Still not spawning but I got a new weird error

use windows::core::{s, w, PCWSTR};
use windows::Win32::UI::WindowsAndMessaging::*;
use windows::Win32::UI::WindowsAndMessaging::WNDCLASSEXW;
use windows::Win32::Graphics::Gdi::HBRUSH;
use std::ffi::c_void;
use windows::Win32::UI::WindowsAndMessaging::RegisterClassExW;
use windows::Win32::UI::WindowsAndMessaging::CreateWindowExW;
use windows::Win32::UI::WindowsAndMessaging::WINDOW_EX_STYLE;
use windows::Win32::UI::WindowsAndMessaging::WINDOW_STYLE;
use windows::Win32::UI::WindowsAndMessaging::ShowWindow;
use windows::Win32::Foundation::HWND;
use windows::Win32::Foundation::WPARAM;
use windows::Win32::Foundation::LPARAM;
use windows::Win32::Foundation::LRESULT;
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
use windows::Win32::Foundation::GetLastError;
use windows::Win32::UI::WindowsAndMessaging::{WS_OVERLAPPEDWINDOW, WS_VISIBLE};
use std::ptr::{null, null_mut};

unsafe extern "system" fn proc(_: HWND, _: u32, _: WPARAM, _: LPARAM) -> LRESULT {
    println!("procedure was caled");
    LRESULT(0)
}

unsafe fn main_unsafe() {
    let instance = GetModuleHandleW(None).unwrap().into();
    let class_name = w!("WINDOW_CLASS_QUARTZ");
    let class = WNDCLASSEXW {
        cbSize: size_of::<WNDCLASSEXW>() as u32,
        style: Default::default(),
        lpfnWndProc: Some(proc),
        cbClsExtra: 0,
        cbWndExtra: 0,
        hInstance: instance,
        hIcon: HICON(null_mut()),
        hCursor: LoadCursorW(None, IDC_ARROW).unwrap(),
        hbrBackground: HBRUSH::default(),
        lpszMenuName: PCWSTR(null()),
        lpszClassName: class_name,
        hIconSm: HICON(null_mut())
    };

    let class_result = RegisterClassExW(&class);
    if class_result == 0 {
        println!("failed to make class");
        dbg!(GetLastError());
        return;
    }

    let hwnd = CreateWindowExW(
        WINDOW_EX_STYLE::default(),
        class_name,
        w!("hello world"),
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        100,
        100,
        500,
        300,
        None,
        None,
        instance,
        None
    ).unwrap();

    ShowWindow(hwnd, SHOW_WINDOW_CMD(5));
}

fn main() {
    unsafe { main_unsafe() };
}
thread 'main' panicked at jdrift-interface\examples\winbuffer.rs:63:7:
called `Result::unwrap()` on an `Err` value: Error { code: HRESULT(0x00000000), message: "The operation completed successfully." }
stack backtrace:

what an error

The problem was quite dumb, never called GetMessageW, I forgot. And the icon was still an issue but using nullptrs fixed the proper default.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.