-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
std.os.windows.LPCWSTR has wrong alignment requirements #19560
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
Comments
For extern functions that take multipurposed pointers, perhaps const std = @import("std");
pub extern "user32" fn LoadIconW(
?std.os.windows.HINSTANCE,
*const anyopaque,
) callconv(std.os.windows.WINAPI) ?std.os.windows.HICON;
pub fn main() !void {
const IDI_HAND: usize = 32513;
_ = LoadIconW(null, @ptrFromInt(IDI_HAND));
} |
I tend to agree that this should probably be dealt with on the function binding side rather than changing https://ziggit.dev/t/how-to-reimplement-win32-makeintresource-macro-in-zig/3359/4 Maybe there's a place for a |
I'm already doing this as a workaround in my own code. However it seems to me that Windows API, being essentially C/C++-centric, doesn't impose any alignment requirements on the pointers in general, at the very least not on the type level. That is, C/C++'s |
That's a fair argument, |
|
Fair enough. I guess then it's a matter of changing the |
It seems to me Upd: tried unions, obviously they don't work since their argument passing conversion is different. Doh. Upd2: actually they do work, the convention is the same, at least on x64, but there is a pitfall. The following does not work:
The problem is not obvious at the first sight. One cannot use |
Zig Version
0.12.0-dev.3193+4ba4f94c9
Steps to Reproduce and Observed Behavior
Occasionally, in Windows API, integer values need to be passed as
LPCWSTR
parameters to functions. Examples includeCreateWindowExW
(passing anATOM
value instead of string aslpClassName
argument),LoadIconW
(passing an integer identifier, such asIDI_APPLICATION
, instead of a string aslpIconName
argument)These values are not required to be even. However casting them to
std.os.windows.LPCWSTR
causes compile errors or runtime panics if the values are not even. E.g.causes compilation error
pointer type '[*:0]const u16' requires aligned address
.Expected Behavior
Being able to cast odd integer values to
std.os.windows.LPCWSTR
.Probably also being able to do the same for
std.os.windows.LPWSTR
.Proposed solution: change the definition(s) of
LPCWSTR
(andLPWSTR
) toThe text was updated successfully, but these errors were encountered: