#@ptrFromInt requires aligned address

1 messages · Page 1 of 1 (latest)

fiery river
#

Hey guys, I'm playing with DirectInput, and I'm stuck because of a macro.

#define MAKEDIPROP(prop)    (*(const GUID *)(prop))
#define DIPROP_BUFFERSIZE       MAKEDIPROP(1)

I'm trying something like that:

const key = @as(*win32.zig.Guid, @ptrFromInt(@as(usize, 1)));

but got:

error: pointer type '*win32.zig.Guid' requires aligned address
wraith root
#

the natural alignment of Guid is larger than 1, so the address cannot itself be 1. you've gotta do something like

@as(*align(1) win32.zig.Guid, @ptrFromInt(1));
fiery river
#

Thanks for the answer, unfortunately, I have to use the pointer, and I an not cast it back to align(4).

wraith root
#

of course you cant, the address isn't a multiple of 4

#

what API requires it be aligned to 4?

fiery river
#

IDirectInputDevice8W.SetProperty

#

right now, my workaround is to cast the pointer function.

pub inline fn DirectInputDevice8_SetProperty(device: *IDirectInputDevice8W, param0: usize, param1: ?*DIPROPHEADER) win32.foundation.HRESULT {
    const SetPropertyType = *const fn (self: *const IDirectInputDevice8W, param0: usize, param1: ?*DIPROPHEADER) win32.foundation.HRESULT;

    const func: SetPropertyType = @ptrCast(device.vtable.SetProperty);

    return func(device, param0, param1);
}
wraith root
#
  1. why aren't you taking a guid ref type for the first parameter?
  2. why does vtable.SetProperty not have the right proto in the first place?
fiery river
#

I'm not sure, it's the way Win32 / Direct X works I guess.

#

They have a generic api, and do hacks to make it work.

wraith root
#

i mean, direct X isn't written in Zig, where does the SetProperty come from?

fiery river
#

The bindings are correct.

#

It's just Microsoft is doing weird c/cpp things.