This file contains thin wrappers around Windows-specific APIs, with these specific goals in mind:
- Convert “errno”-style error codes into Zig errors.
- When null-terminated or UTF16LE byte buffers are required, provide APIs which accept slices as well as APIs which accept null-terminated UTF16LE byte buffers.
Types
- ACCESS_MASK
- ACTIVATION_CONTEXT_DATA
- ALTERNATIVE_ARCHITECTURE_TYPE
- ASSEMBLY_STORAGE_MAP
- ATOM
- BOOL
- BOOLEAN
- BSTR
- BYTE
- BY_HANDLE_FILE_INFORMATION
- CHAR
- CLIENT_ID
- CONDITION_VARIABLE
- CONSOLE_SCREEN_BUFFER_INFO
- COORD
- CRITICAL_SECTION
- CURDIR
- DEVICE_TYPE
- DWORD
- DWORD64
- DWORD_PTR
- DeleteFileOptions
- ENUM_PAGE_FILE_INFORMATION
- EXCEPTION_DISPOSITION
- EXCEPTION_POINTERS
- EXCEPTION_RECORD
- EXCEPTION_REGISTRATION_RECORD
- EXCEPTION_ROUTINE
- FARPROC
- FILETIME
- FILE_ACCESS_INFORMATION
- FILE_ALIGNMENT_INFORMATION
- FILE_ALL_INFORMATION
- FILE_BASIC_INFORMATION
- FILE_BOTH_DIRECTORY_INFORMATION
- FILE_BOTH_DIR_INFORMATION
- FILE_DIRECTORY_INFORMATION
- FILE_DISPOSITION_INFORMATION
- FILE_DISPOSITION_INFORMATION_EX
- FILE_EA_INFORMATION
- FILE_END_OF_FILE_INFORMATION
- FILE_FS_DEVICE_INFORMATION
- FILE_INFORMATION_CLASS
- FILE_INFO_BY_HANDLE_CLASS
- FILE_INTERNAL_INFORMATION
- FILE_MODE_INFORMATION
- FILE_NAME_INFO
- FILE_NAME_INFORMATION
- FILE_NOTIFY_INFORMATION
- FILE_POSITION_INFORMATION
- FILE_RENAME_INFORMATION
- FILE_STANDARD_INFORMATION
- FLOAT
- FLS_CALLBACK_INFO
- FS_INFORMATION_CLASS
- FileInformationIterator
- GUID
- GetFinalPathNameByHandleFormat
- GetQueuedCompletionStatusResult
- HANDLE
- HANDLER_ROUTINE
- HBRUSH
- HCRYPTPROV
- HCURSOR
- HDC
- HGLRC
- HICON
- HINSTANCE
- HKEY
- HLOCAL
- HMENU
- HMODULE
- HRESULT
- HWND
- IMAGE_TLS_DIRECTORY
- IMAGE_TLS_DIRECTORY32
- IMAGE_TLS_DIRECTORY64
- INIT_ONCE
- INIT_ONCE_FN
- INT
- IO_APC_ROUTINE
- IO_STATUS_BLOCK
- KAFFINITY
- KNOWNFOLDERID
- KPRIORITY
- KSYSTEM_TIME
- KUSER_SHARED_DATA
- LANGID
- LARGE_INTEGER
- LDR_DATA_TABLE_ENTRY
- LIST_ENTRY
- LONG
- LONGLONG
- LONG_PTR
- LPARAM
- LPCSTR
- LPCVOID
- LPCWSTR
- LPOVERLAPPED_COMPLETION_ROUTINE
- LPSTR
- LPTHREAD_START_ROUTINE
- LPVOID
- LPWSTR
- LRESULT
- LSTATUS
- MEMORY_BASIC_INFORMATION
- MODULEENTRY32
- MODULEINFO
- MOUNTMGR_MOUNT_POINT
- MOUNTMGR_MOUNT_POINTS
- MOUNT_POINT_REPARSE_BUFFER
- NTSTATUS
- NT_PRODUCT_TYPE
- NT_TIB
- NamespacePrefix
- OBJECT_ATTRIBUTES
- OBJECT_INFORMATION_CLASS
- OBJECT_NAME_INFORMATION
- OSVERSIONINFOW
- OVERLAPPED
- OVERLAPPED_ENTRY
- OpenFileOptions
- PCWSTR
- PEB
- PEB_LDR_DATA
- PENUM_PAGE_FILE_CALLBACKA
- PENUM_PAGE_FILE_CALLBACKW
- PERFORMANCE_INFORMATION
- PF
- PIMAGE_TLS_CALLBACK
- PMEMORY_BASIC_INFORMATION
- POINT
- PPS_POST_PROCESS_INIT_ROUTINE
- PROCESSINFOCLASS
- PROCESS_BASIC_INFORMATION
- PROCESS_INFORMATION
- PROCESS_MEMORY_COUNTERS
- PROCESS_MEMORY_COUNTERS_EX
- PSAPI_WS_WATCH_INFORMATION
- PSAPI_WS_WATCH_INFORMATION_EX
- PTHREAD_START_ROUTINE
- PVOID
- PWSTR
- PathSpace
- RECT
- REGSAM
- REPARSE_DATA_BUFFER
- RTL_BITMAP
- RTL_CRITICAL_SECTION
- RTL_CRITICAL_SECTION_DEBUG
- RTL_DRIVE_LETTER_CURDIR
- RTL_OSVERSIONINFOW
- RTL_QUERY_REGISTRY_ROUTINE
- RTL_QUERY_REGISTRY_TABLE
- RTL_RUN_ONCE
- RTL_USER_PROCESS_PARAMETERS
- SECTION_INHERIT
- SECURITY_ATTRIBUTES
- SHORT
- SIZE_T
- SMALL_RECT
- SRWLOCK
- STARTUPINFOW
- SYMBOLIC_LINK_REPARSE_BUFFER
- SYSTEM_BASIC_INFORMATION
- SYSTEM_INFO
- SYSTEM_INFORMATION_CLASS
- TEB
- THREADINFOCLASS
- THREAD_BASIC_INFORMATION
- TransferType
- UCHAR
- UINT
- ULARGE_INTEGER
- ULONG
- ULONG64
- ULONGLONG
- ULONG_PTR
- UNICODE_STRING
- UNWIND_HISTORY_TABLE
- UNWIND_HISTORY_TABLE_ENTRY
- USHORT
- UnprefixedPathType
- VECTORED_EXCEPTION_HANDLER
- VM_COUNTERS
- WCHAR
- WIN32_FIND_DATAW
- WORD
- WPARAM
- Win32Error
- XSTATE_CONFIGURATION
- XSTATE_FEATURE
- va_list
Functions
fn CTL_CODE(deviceType: u16, function: u12, method: TransferType, access: u2) DWORD
https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/defining-i-o-co…
https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/defining-i-o-control-codes
fn CreateEventEx(attributes: ?*SECURITY_ATTRIBUTES, name: []const u8, flags: DWORD, desired_access: DWORD) !HANDLE
No documentation provided.
fn CreateEventExW(attributes: ?*SECURITY_ATTRIBUTES, nameW: [*:0]const u16, flags: DWORD, desired_access: DWORD) !HANDLE
No documentation provided.
fn CreateIoCompletionPort(file_handle: HANDLE, existing_completion_port: ?HANDLE, completion_key: usize, concurrent_thread_count: DWORD) CreateIoCompletionPortError!HANDLE
No documentation provided.
fn CreatePipe(rd: *HANDLE, wr: *HANDLE, sattr: *const SECURITY_ATTRIBUTES) CreatePipeError!void
No documentation provided.
fn CreateProcessW(lpApplicationName: ?LPWSTR, lpCommandLine: LPWSTR, lpProcessAttributes: ?*SECURITY_ATTRIBUTES, lpThreadAttributes: ?*SECURITY_ATTRIBUTES, bInheritHandles: BOOL, dwCreationFlags: DWORD, lpEnvironment: ?*anyopaque, lpCurrentDirectory: ?LPWSTR, lpStartupInfo: *STARTUPINFOW, lpProcessInformation: *PROCESS_INFORMATION) CreateProcessError!void
No documentation provided.
fn CreateSymbolicLink(dir: ?HANDLE, sym_link_path: []const u16, target_path: []const u16, is_directory: bool) CreateSymbolicLinkError!void
Needs either:
SeCreateSymbolicLinkPrivilege
privilege or- Developer mode…
Needs either:
SeCreateSymbolicLinkPrivilege
privilege or- Developer mode on Windows 10 otherwise fails with
error.AccessDenied
. In which casesym_link_path
may still be created on the file system but will lack reparse processing data applied to it.
fn DeleteFile(sub_path_w: []const u16, options: DeleteFileOptions) DeleteFileError!void
No documentation provided.
fn DeviceIoControl(h: HANDLE, ioControlCode: ULONG, in: ?[]const u8, out: ?[]u8) DeviceIoControlError!void
A Zig wrapper around
NtDeviceIoControlFile
andNtFsControlFile
syscalls. It…A Zig wrapper around
NtDeviceIoControlFile
andNtFsControlFile
syscalls. It implements similar behavior toDeviceIoControl
and is meant to serve as a direct substitute for that call. TODO work out if we need to expose other arguments to the underlying syscalls.fn GetCurrentDirectory(buffer: []u8) GetCurrentDirectoryError![]u8
The result is a slice of
buffer
, indexed from 0.fn GetEnvironmentVariableW(lpName: LPWSTR, lpBuffer: [*]u16, nSize: DWORD) GetEnvironmentVariableError!DWORD
No documentation provided.
fn GetFileAttributesW(lpFileName: [*:0]const u16) GetFileAttributesError!DWORD
No documentation provided.
fn GetFinalPathNameByHandle(hFile: HANDLE, fmt: GetFinalPathNameByHandleFormat, out_buffer: []u16) GetFinalPathNameByHandleError![]u16
Returns canonical (normalized) path of handle. Use `GetFinalPathNameByHandleFor…
Returns canonical (normalized) path of handle. Use
GetFinalPathNameByHandleFormat
to specify whether the path is meant to include NT or DOS volume name (e.g.,\Device\HarddiskVolume0\foo.txt
versusC:\foo.txt
). If DOS volume name format is selected, note that this function does not prepend\\?\
prefix to the resultant path.fn GetModuleFileNameW(hModule: ?HMODULE, buf_ptr: [*]u16, buf_len: DWORD) GetModuleFileNameError![:0]u16
No documentation provided.
fn GetOverlappedResult(h: HANDLE, overlapped: *OVERLAPPED, wait: bool) !DWORD
No documentation provided.
fn GetProcessMemoryInfo(hProcess: HANDLE) GetProcessMemoryInfoError!VM_COUNTERS
No documentation provided.
fn GetQueuedCompletionStatus(completion_port: HANDLE, bytes_transferred_count: *DWORD, lpCompletionKey: *usize, lpOverlapped: *?*OVERLAPPED, dwMilliseconds: DWORD) GetQueuedCompletionStatusResult
No documentation provided.
fn GetQueuedCompletionStatusEx(completion_port: HANDLE, completion_port_entries: []OVERLAPPED_ENTRY, timeout_ms: ?DWORD, alertable: bool) GetQueuedCompletionStatusError!u32
No documentation provided.
fn InitOnceExecuteOnce(InitOnce: *INIT_ONCE, InitFn: INIT_ONCE_FN, Parameter: ?*anyopaque, Context: ?*anyopaque) void
No documentation provided.
fn LockFile(FileHandle: HANDLE, Event: ?HANDLE, ApcRoutine: ?*IO_APC_ROUTINE, ApcContext: ?*anyopaque, IoStatusBlock: *IO_STATUS_BLOCK, ByteOffset: *const LARGE_INTEGER, Length: *const LARGE_INTEGER, Key: ?*ULONG, FailImmediately: BOOLEAN, ExclusiveLock: BOOLEAN) !void
No documentation provided.
fn MoveFileEx(old_path: []const u8, new_path: []const u8, flags: DWORD) MoveFileError!void
No documentation provided.
fn MoveFileExW(old_path: [*:0]const u16, new_path: [*:0]const u16, flags: DWORD) MoveFileError!void
No documentation provided.
fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HANDLE
No documentation provided.
fn PostQueuedCompletionStatus(completion_port: HANDLE, bytes_transferred_count: DWORD, completion_key: usize, lpOverlapped: ?*OVERLAPPED) PostQueuedCompletionStatusError!void
No documentation provided.
fn ProcessBaseAddress(handle: HANDLE) ProcessBaseAddressError!HMODULE
Returns the base address of the process loaded into memory.
fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64, io_mode: std.io.ModeOverride) ReadFileError!usize
If buffer’s length exceeds what a Windows DWORD integer can hold, it will be bro…
If buffer’s length exceeds what a Windows DWORD integer can hold, it will be broken into multiple non-atomic reads.
fn ReadLink(dir: ?HANDLE, sub_path_w: []const u16, out_buffer: []u8) ReadLinkError![]u8
No documentation provided.
fn ReadProcessMemory(handle: HANDLE, addr: ?LPVOID, buffer: []u8) ReadMemoryError![]u8
No documentation provided.
fn RtlGenRandom(output: []u8) RtlGenRandomError!void
Call RtlGenRandom() instead of CryptGetRandom() on Windows https://github.com/r…
Call RtlGenRandom() instead of CryptGetRandom() on Windows https://github.com/rust-lang-nursery/rand/issues/111 https://bugzilla.mozilla.org/show_bug.cgi?id=504270
fn SetConsoleCtrlHandler(handler_routine: ?HANDLER_ROUTINE, add: bool) !void
No documentation provided.
fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) SetConsoleTextAttributeError!void
No documentation provided.
fn SetCurrentDirectory(path_name: []const u16) SetCurrentDirectoryError!void
No documentation provided.
fn SetFilePointerEx_BEGIN(handle: HANDLE, offset: u64) SetFilePointerError!void
The SetFilePointerEx function with the
dwMoveMethod
parameter set to `FILE_BEG…The SetFilePointerEx function with the
dwMoveMethod
parameter set toFILE_BEGIN
.fn SetFilePointerEx_CURRENT(handle: HANDLE, offset: i64) SetFilePointerError!void
The SetFilePointerEx function with the
dwMoveMethod
parameter set to `FILE_CUR…The SetFilePointerEx function with the
dwMoveMethod
parameter set toFILE_CURRENT
.fn SetFilePointerEx_CURRENT_get(handle: HANDLE) SetFilePointerError!u64
The SetFilePointerEx function with parameters to get the current offset.
fn SetFilePointerEx_END(handle: HANDLE, offset: i64) SetFilePointerError!void
The SetFilePointerEx function with the
dwMoveMethod
parameter set to `FILE_END…The SetFilePointerEx function with the
dwMoveMethod
parameter set toFILE_END
.fn SetFileTime(hFile: HANDLE, lpCreationTime: ?*const FILETIME, lpLastAccessTime: ?*const FILETIME, lpLastWriteTime: ?*const FILETIME) SetFileTimeError!void
No documentation provided.
fn SetHandleInformation(h: HANDLE, mask: DWORD, flags: DWORD) SetHandleInformationError!void
No documentation provided.
fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError!void
No documentation provided.
fn UnlockFile(FileHandle: HANDLE, IoStatusBlock: *IO_STATUS_BLOCK, ByteOffset: *const LARGE_INTEGER, Length: *const LARGE_INTEGER, Key: ?*ULONG) !void
No documentation provided.
fn VirtualAlloc(addr: ?LPVOID, size: usize, alloc_type: DWORD, flProtect: DWORD) VirtualAllocError!LPVOID
No documentation provided.
fn VirtualProtect(lpAddress: ?LPVOID, dwSize: SIZE_T, flNewProtect: DWORD, lpflOldProtect: *DWORD) VirtualProtectError!void
No documentation provided.
fn VirtualProtectEx(handle: HANDLE, addr: ?LPVOID, size: SIZE_T, new_prot: DWORD) VirtualProtectError!DWORD
No documentation provided.
fn VirtualQuery(lpAddress: ?LPVOID, lpBuffer: PMEMORY_BASIC_INFORMATION, dwLength: SIZE_T) VirtualQueryError!SIZE_T
No documentation provided.
fn WSAIoctl(s: ws2_32.SOCKET, dwIoControlCode: DWORD, inBuffer: ?[]const u8, outBuffer: []u8, overlapped: ?*OVERLAPPED, completionRoutine: ?ws2_32.LPWSAOVERLAPPED_COMPLETION_ROUTINE) !DWORD
No documentation provided.
fn WSASocketW(af: i32, socket_type: i32, protocol: i32, protocolInfo: ?*ws2_32.WSAPROTOCOL_INFOW, g: ws2_32.GROUP, dwFlags: DWORD) !ws2_32.SOCKET
Microsoft requires WSAStartup to be called to initialize, or else WSASocketW wi…
Microsoft requires WSAStartup to be called to initialize, or else WSASocketW will return WSANOTINITIALISED. Since this is a standard library, we do not have the luxury of putting initialization code anywhere, because we would not want to pay the cost of calling WSAStartup if there ended up being no networking. Also, if Zig code is used as a library, Zig is not in charge of the start code, and we couldn’t put in any initialization code even if we wanted to. The documentation for WSAStartup mentions that there must be a matching WSACleanup call. It is not possible for the Zig Standard Library to honor this for the same reason - there is nowhere to put deinitialization code. So, API users of the zig std lib have two options:
- (recommended) The simple, cross-platform way: just call
WSASocketW
and don’t worry about it. Zig will call WSAStartup() in a thread-safe manner and never deinitialize networking. This is ideal for an application which has the capability to do networking. - The getting-your-hands-dirty way: call
WSAStartup()
before doing networking, so that the error handling code for WSANOTINITIALISED never gets run, which then allows the application or library to callWSACleanup()
. This could make sense for a library, which has init and deinit functions for the whole library’s lifetime.
- (recommended) The simple, cross-platform way: just call
fn WaitForMultipleObjectsEx(handles: []const HANDLE, waitAll: bool, milliseconds: DWORD, alertable: bool) !u32
No documentation provided.
fn WaitForSingleObject(handle: HANDLE, milliseconds: DWORD) WaitForSingleObjectError!void
No documentation provided.
fn WaitForSingleObjectEx(handle: HANDLE, milliseconds: DWORD, alertable: bool) WaitForSingleObjectError!void
No documentation provided.
fn WriteFile(handle: HANDLE, bytes: []const u8, offset: ?u64, io_mode: std.io.ModeOverride) WriteFileError!usize
No documentation provided.
fn WriteProcessMemory(handle: HANDLE, addr: ?LPVOID, buffer: []const u8) WriteMemoryError!usize
No documentation provided.
fn accept(s: ws2_32.SOCKET, name: ?*ws2_32.sockaddr, namelen: ?*ws2_32.socklen_t) ws2_32.SOCKET
No documentation provided.
fn bind(s: ws2_32.SOCKET, name: *const ws2_32.sockaddr, namelen: ws2_32.socklen_t) i32
No documentation provided.
fn cStrToPrefixedFileW(s: [*:0]const u8) !PathSpace
Same as
sliceToPrefixedFileW
but accepts a pointer to a null-terminated path.fn eqlIgnoreCaseUtf8(a: []const u8, b: []const u8) bool
Compares two UTF-8 strings using the equivalent functionality of `RtlEqualUnico…
Compares two UTF-8 strings using the equivalent functionality of
RtlEqualUnicodeString
(with case insensitive comparison enabled). This function can be called on any target. Assumesa
andb
are valid UTF-8.fn eqlIgnoreCaseWTF16(a: []const u16, b: []const u16) bool
Compares two WTF16 strings using the equivalent functionality of `RtlEqualUnico…
Compares two WTF16 strings using the equivalent functionality of
RtlEqualUnicodeString
(with case insensitive comparison enabled). This function can be called on any target.fn fromSysTime(hns: i64) i128
A file time is a 64-bit value that represents the number of 100-nanosecond inte…
A file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC). This function returns the number of nanoseconds since the canonical epoch, which is the POSIX one (Jan 01, 1970 AD).
fn getUnprefixedPathType(comptime T: type, path: []const T) UnprefixedPathType
Get the path type of a path that is known to not have any namespace prefixes (`…
Get the path type of a path that is known to not have any namespace prefixes (
\\?\
,\\.\
,\??\
).fn getpeername(s: ws2_32.SOCKET, name: *ws2_32.sockaddr, namelen: *ws2_32.socklen_t) i32
No documentation provided.
fn getsockname(s: ws2_32.SOCKET, name: *ws2_32.sockaddr, namelen: *ws2_32.socklen_t) i32
No documentation provided.
fn loadWinsockExtensionFunction(comptime T: type, sock: ws2_32.SOCKET, guid: GUID) !T
Loads a Winsock extension function in runtime specified by a GUID.
fn nanoSecondsToFileTime(ns: i128) FILETIME
Converts a number of nanoseconds since the POSIX epoch to a Windows FILETIME.
fn normalizePath(comptime T: type, path: []T) RemoveDotDirsError!usize
Normalizes a Windows path with the following steps: 1) convert all forward …
Normalizes a Windows path with the following steps: 1) convert all forward slashes to back slashes 2) collapse duplicate back slashes 3) remove ‘.’ and ‘..’ directory parts Returns the length of the new path.
fn recvfrom(s: ws2_32.SOCKET, buf: [*]u8, len: usize, flags: u32, from: ?*ws2_32.sockaddr, from_len: ?*ws2_32.socklen_t) i32
No documentation provided.
fn removeDotDirsSanitized(comptime T: type, path: []T) RemoveDotDirsError!usize
Removes ‘.’ and ‘..’ path components from a “sanitized relative path”. A “sanit…
Removes ‘.’ and ‘..’ path components from a “sanitized relative path”. A “sanitized path” is one where: 1) all forward slashes have been replaced with back slashes 2) all repeating back slashes have been collapsed 3) the path is a relative one (does not start with a back slash)
fn sendto(s: ws2_32.SOCKET, buf: [*]const u8, len: usize, flags: u32, to: ?*const ws2_32.sockaddr, to_len: ws2_32.socklen_t) i32
No documentation provided.
fn sliceToPrefixedFileW(path: []const u8) !PathSpace
Same as
wToPrefixedFileW
but accepts a UTF-8 encoded path.fn unexpectedError(err: Win32Error) std.os.UnexpectedError
Call this when you made a windows DLL call or something that does SetLastError …
Call this when you made a windows DLL call or something that does SetLastError and you get an unexpected error.
fn unexpectedStatus(status: NTSTATUS) std.os.UnexpectedError
Call this when you made a windows NtDll call and you get an unexpected status.
fn wToPrefixedFileW(path: [:0]const u16) !PathSpace
Converts the
path
to WTF16, null-terminated. If the path contains any namespa…Converts the
path
to WTF16, null-terminated. If the path contains any namespace prefix, or is anything but a relative path (rooted, drive relative, etc) the result will have the NT-style prefix\??\
.Similar to RtlDosPathNameToNtPathName_U with a few differences:
- Does not allocate on the heap.
- Relative paths are kept as relative unless they contain too many .. components, in which case they are treated as drive-relative and resolved against the CWD.
- Special case device names like COM1, NUL, etc are not handled specially (TODO)
- . and space are not stripped from the end of relative paths (potential TODO)
Values
Error Sets
- CreateIoCompletionPortError
- CreatePipeError
- CreateProcessError
- CreateSymbolicLinkError
- DeleteFileError
- DeviceIoControlError
- GetCurrentDirectoryError
- GetEnvironmentStringsError
- GetEnvironmentVariableError
- GetFileAttributesError
- GetFileSizeError
- GetFinalPathNameByHandleError
- GetProcessMemoryInfoError
- GetStdHandleError
- LoadLibraryError
- MoveFileError
- OpenError
- PostQueuedCompletionStatusError
- ReadFileError
- ReadLinkError
- ReadMemoryError
- RemoveDotDirsError
- RtlGenRandomError
- SetConsoleTextAttributeError
- SetCurrentDirectoryError
- SetFilePointerError
- SetFileTimeError
- SetHandleInformationError
- TerminateProcessError
- VirtualAllocError
- VirtualProtectError
- VirtualQueryError
- WaitForSingleObjectError
- WriteFileError
- WriteMemoryError