Skip to content

Commit

Permalink
Windows: Rework kernel32 apis
Browse files Browse the repository at this point in the history
To facilitate #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 #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.
  • Loading branch information
The-King-of-Toasters committed May 29, 2024
1 parent 72809bf commit 44b364c
Show file tree
Hide file tree
Showing 3 changed files with 532 additions and 334 deletions.
2 changes: 1 addition & 1 deletion lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ pub const DebugInfo = struct {
@memcpy(name_buffer[0..4], &[_]u16{ '\\', '?', '?', '\\' });

const process_handle = windows.kernel32.GetCurrentProcess();
const len = windows.kernel32.K32GetModuleFileNameExW(
const len = windows.kernel32.GetModuleFileNameExW(
process_handle,
module.handle,
@ptrCast(&name_buffer[4]),
Expand Down
6 changes: 3 additions & 3 deletions lib/std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ pub const GetCurrentDirectoryError = error{
/// The result is a slice of `buffer`, indexed from 0.
/// The result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
pub fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8 {
var wtf16le_buf: [PATH_MAX_WIDE]u16 = undefined;
const result = kernel32.GetCurrentDirectoryW(wtf16le_buf.len, &wtf16le_buf);
var wtf16le_buf: [PATH_MAX_WIDE:0]u16 = undefined;
const result = kernel32.GetCurrentDirectoryW(wtf16le_buf.len + 1, &wtf16le_buf);
if (result == 0) {
switch (kernel32.GetLastError()) {
else => |err| return unexpectedError(err),
Expand Down Expand Up @@ -2764,7 +2764,7 @@ pub fn loadWinsockExtensionFunction(comptime T: type, sock: ws2_32.SOCKET, guid:
pub fn unexpectedError(err: Win32Error) UnexpectedError {
if (std.posix.unexpected_error_tracing) {
// 614 is the length of the longest windows error description
var buf_wstr: [614]WCHAR = undefined;
var buf_wstr: [614:0]WCHAR = undefined;
const len = kernel32.FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
null,
Expand Down
Loading

0 comments on commit 44b364c

Please sign in to comment.