Ptr alignment issue when cImport windows.h

This seems like C compiler semantics and Zig semantics interacting badly, so the unaligned “address” -1 is accepted by a C compiler but not Zig. There might be a good language-level solution to this, like making all [*c] pointers align(1). I don’t think there’s a good workaround for this except to redefine redeclare the SetWindowPos function:

const WINAPI: std.builtin.CallingConvention = if (@import("builtin").cpu.arch == .x86) .Stdcall else .C;
extern fn SetWindowPos(
    hWnd: c.HWND
    hWndInsertAfter: ?*align(1) c.struct_HWND__,
    x: c_int,
    y: c_int,
    cx: c_int,
    cy: c_int,
    flags: c.UINT,
) callconv(WINAPI) c.BOOL;

The best way for you might be to use the lovely zigwin32.

3 Likes