Skip to content

Commit aa26b64

Browse files
Windows: Rework kernel32 apis
To facilitate ziglang#1840, this commit slims `std.windows.kernel32` to only have the functions needed by the standard library. Since this will break projects that relied on these, I offer two solutions: - Make an argument as to why certain functions should be added back in. Note that they may just be wrappers around `ntdll` APIs, which would go against ziglang#1840. If necessary I'll add them back in *and* make wrappers in `std.windows` for it. - Maintain your own list of APIs. This is the option taken by bun[1], where they wrap functions with tracing. - Use `zigwin32`. I've also added TODO comments that specify which functions can be reimplemented using `ntdll` APIs in the future. Other changes: - Group functions into groups (I/O, process management etc.). - Synchronize definitions against Microsoft documentation to use the proper parameter types/names. - Break all functions with parameters over multiple lines.
1 parent 72809bf commit aa26b64

File tree

3 files changed

+530
-334
lines changed

3 files changed

+530
-334
lines changed

lib/std/debug.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1918,7 +1918,7 @@ pub const DebugInfo = struct {
19181918
@memcpy(name_buffer[0..4], &[_]u16{ '\\', '?', '?', '\\' });
19191919

19201920
const process_handle = windows.kernel32.GetCurrentProcess();
1921-
const len = windows.kernel32.K32GetModuleFileNameExW(
1921+
const len = windows.kernel32.GetModuleFileNameExW(
19221922
process_handle,
19231923
module.handle,
19241924
@ptrCast(&name_buffer[4]),

lib/std/os/windows.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,8 @@ pub const GetCurrentDirectoryError = error{
725725
/// The result is a slice of `buffer`, indexed from 0.
726726
/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
727727
pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {
728-
var wtf16le_buf: [PATH_MAX_WIDE]u16 = undefined;
729-
const result = kernel32.GetCurrentDirectoryW(wtf16le_buf.len, &wtf16le_buf);
728+
var wtf16le_buf: [PATH_MAX_WIDE:0]u16 = undefined;
729+
const result = kernel32.GetCurrentDirectoryW(wtf16le_buf.len + 1, &wtf16le_buf);
730730
if (result == 0) {
731731
switch (kernel32.GetLastError()) {
732732
else => |err| return unexpectedError(err),
@@ -2764,7 +2764,7 @@ pub fn loadWinsockExtensionFunction(comptime T: type, sock: ws2_32.SOCKET, guid:
27642764
pub fn unexpectedError(err: Win32Error) UnexpectedError {
27652765
if (std.posix.unexpected_error_tracing) {
27662766
// 614 is the length of the longest windows error description
2767-
var buf_wstr: [614]WCHAR = undefined;
2767+
var buf_wstr: [614:0]WCHAR = undefined;
27682768
const len = kernel32.FormatMessageW(
27692769
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
27702770
null,

0 commit comments

Comments
 (0)