#How Can I Create a Window with the WinAPI?

1 messages · Page 1 of 1 (latest)

inner fog
#

I am trying to make a Window using the WinAPI by following this tutorial. I wrote the following code and used zig build -Dtarget=x86_64-windows-msvc to build the file and linked all the relevant libraries n build.zig.

const std = @import("std");
const c = @cImport({
    @cDefine("UNICODE", "");
    @cInclude("Windows.h");
});

fn windowProcedure(window: c.HWND, message: c.UINT, wparam: c.WPARAM, lparam: c.LPARAM) callconv(.C) c.LRESULT {
    ...
}

pub export fn wWinMain(
    instance: c.HINSTANCE,
    previous_instance: c.HINSTANCE,
    command_line: c.LPSTR,
    command_show: c.INT,
) callconv(.Win64) c.INT {
    ...
x}

However, I got these errors.

C:\Program Files\zig\lib\std\start.zig:610:43: error: cast increases pointer alignment
    const hInstance = @as(MAIN_HINSTANCE, @ptrCast(peb.ImageBaseAddress));
                                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\zig\lib\std\start.zig:610:55: note: '*os.windows.HMODULE__opaque_55476' has alignment '1'
    const hInstance = @as(MAIN_HINSTANCE, @ptrCast(peb.ImageBaseAddress));
                                                   ~~~^~~~~~~~~~~~~~~~~
C:\Program Files\zig\lib\std\start.zig:610:43: note: '[*c]cimport.struct_HINSTANCE__' has alignment '4'
C:\Program Files\zig\lib\std\start.zig:610:43: note: use @alignCast to assert pointer alignment
C:\Program Files\zig\lib\std\start.zig:548:12: note: called from here
    return @call(.always_inline, call_wWinMain, .{});
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Program Files\zig\lib\std\start.zig:361:67: note: called from here
    const result: std.os.windows.INT = initEventLoopAndCallWinMain();                                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~

How can I get my code to build?

floral wind
#

You'll probably have an easier time using something like zigwin32(https://github.com/marlersoft/zigwin32) than starting from raw cImport. (Plus most windows projects benefit a lot from #define WIN32_LEAN_AND_MEAN to prune how many subsystems are included).

#

Other than that, it looks like the compiler is telling you what you need to do. Doing raw Windows will require a lot of casting.

inner fog
floral wind
#

Ah didn't notice that. This appears to be an issue with the provided start.zig that I can't answer -- perhaps a bug,

inner fog
#

I'll have to take a look at zigwin32.

floral wind
#

That project is generated from another project. To see how to consume zigwin32, look in the issues for one asking about how to use it.

fickle vessel
#

The HINSTANCE type you used came from the @cImport, and is most likely defined differently in stdlib

#

You should use std.os.windows types when possible

#

Also using the headers is a bit annoying and also doesn't cover some parts of win32

#

I'd suggest either zigwin32 or for maximum flexibility writing the definitions manually

sterile leaf
#

FYI, you don't actually need to use wWinMain; you can get all of its arguments from syscalls anyway.
I would probably just use main as normal, and do that instead.

#

Indeed, given the start.zig bug you might have, that might be better anyway. 😄

flat sluice
#

what does wWinMain do anyways?

fickle vessel
#

I'm no expert but as far as I'm aware it's pretty much the Windows version of main() for GUI applications

#

You're not forced to use it but microsoft does recommend it when you don't need a "console"

#

the w prefix means it's "wide" (i.e uses utf16)