1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
const std = @import("std");
const windows = std.os.windows;
// NOTE: The Windows part of the Zig stdlib is currently in the process of
// having most of its features removed, with the ultimate goal of switching to
// serve as a support for higher-level functionality offered in places like
// `std.Io` only. As such this file serves as a "bridge" between type
// information (mostly coming from stdlib) and manually-defined constants and
// external functions.
// Utility functions
pub const GetCurrentProcessId = windows.GetCurrentProcessId;
pub const GetLastError = windows.GetLastError;
pub const unexpectedError = windows.unexpectedError;
pub const unexpectedStatus = windows.unexpectedStatus;
// Primitive types
pub const BOOL = windows.BOOL;
pub const COORD = windows.COORD;
pub const DWORD = windows.DWORD;
pub const DWORD_PTR = windows.DWORD_PTR;
pub const HANDLE = windows.HANDLE;
pub const HINSTANCE = windows.HINSTANCE;
pub const HPCON = windows.LPVOID;
pub const HRESULT = c_long;
pub const LARGE_INTEGER = windows.LARGE_INTEGER;
pub const LPCWSTR = windows.LPCWSTR;
pub const LPSTR = windows.LPSTR;
pub const LPVOID = windows.LPVOID;
pub const LPWSTR = windows.LPWSTR;
pub const PVOID = windows.PVOID;
pub const SIZE_T = windows.SIZE_T;
pub const UINT = windows.UINT;
pub const ULONG = windows.ULONG;
pub const ULONG_PTR = windows.ULONG_PTR;
pub const UNICODE_STRING = windows.UNICODE_STRING;
// Structs and opaque types
pub const LPPROC_THREAD_ATTRIBUTE_LIST = ?*anyopaque;
pub const SECURITY_ATTRIBUTES = windows.SECURITY_ATTRIBUTES;
pub const STARTF_USESTDHANDLES = windows.STARTF_USESTDHANDLES;
pub const STARTUPINFOW = windows.STARTUPINFOW;
pub const OVERLAPPED = extern struct {
Internal: ULONG_PTR,
InternalHigh: ULONG_PTR,
DUMMYUNIONNAME: extern union {
DUMMYSTRUCTNAME: extern struct {
Offset: DWORD,
OffsetHigh: DWORD,
},
Pointer: ?PVOID,
},
hEvent: ?HANDLE,
};
pub const PROCESS_INFORMATION = extern struct {
hProcess: HANDLE,
hThread: HANDLE,
dwProcessId: DWORD,
dwThreadId: DWORD,
};
pub const STARTUPINFOEX = extern struct {
StartupInfo: windows.STARTUPINFOW,
lpAttributeList: LPPROC_THREAD_ATTRIBUTE_LIST,
};
// Well-known constant values
pub const INFINITE = 4294967295;
pub const INVALID_HANDLE_VALUE = windows.INVALID_HANDLE_VALUE;
pub const MAX_PATH = windows.MAX_PATH;
pub const FALSE: windows.BOOL = .fromBool(false);
pub const TRUE: windows.BOOL = .fromBool(true);
// Bit-field and enum constant values
pub const CREATE_UNICODE_ENVIRONMENT = 0x00000400;
pub const EXTENDED_STARTUPINFO_PRESENT = 0x00080000;
pub const FILE_ATTRIBUTE_NORMAL = 0x80;
pub const FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000;
pub const FILE_FLAG_OVERLAPPED = 0x40000000;
pub const FILE_NON_DIRECTORY_FILE = 0x00000040;
pub const FILE_SHARE_READ = 0x00000001;
pub const GENERIC_READ = 0x80000000;
pub const HANDLE_FLAG_INHERIT = 0x00000001;
pub const MEM_COMMIT = 0x1000;
pub const MEM_RELEASE = 0x8000;
pub const MEM_RESERVE = 0x2000;
pub const OPEN_EXISTING = 3; // Known as FILE_OPEN in Windows docs
pub const PAGE_READWRITE = 0x04;
pub const PIPE_ACCESS_OUTBOUND = 0x00000002;
pub const PIPE_TYPE_BYTE = 0x00000000;
pub const PROC_THREAD_ATTRIBUTE_ADDITIVE = 0x00040000;
pub const PROC_THREAD_ATTRIBUTE_INPUT = 0x00020000;
pub const PROC_THREAD_ATTRIBUTE_NUMBER = 0x0000FFFF;
pub const PROC_THREAD_ATTRIBUTE_THREAD = 0x00010000;
pub const S_OK = 0;
pub const WAIT_FAILED = 0xFFFFFFFF;
pub const PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE = ProcThreadAttributeValue(
.ProcThreadAttributePseudoConsole,
false,
true,
false,
);
// Types needed for ntdll calls
pub const ACCESS_MASK = windows.ACCESS_MASK;
pub const IO_STATUS_BLOCK = windows.IO_STATUS_BLOCK;
pub const NTSTATUS = windows.NTSTATUS;
pub const OBJECT_ATTRIBUTES = windows.OBJECT.ATTRIBUTES;
// Exported functions by library
pub const exp = struct {
pub const kernel32 = struct {
pub extern "kernel32" fn CreatePipe(
hReadPipe: *HANDLE,
hWritePipe: *HANDLE,
lpPipeAttributes: ?*const SECURITY_ATTRIBUTES,
nSize: DWORD,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn CreatePseudoConsole(
size: COORD,
hInput: HANDLE,
hOutput: HANDLE,
dwFlags: DWORD,
phPC: *HPCON,
) callconv(.winapi) HRESULT;
pub extern "kernel32" fn ResizePseudoConsole(
hPC: HPCON,
size: COORD,
) callconv(.winapi) HRESULT;
pub extern "kernel32" fn ClosePseudoConsole(hPC: HPCON) callconv(.winapi) void;
pub extern "kernel32" fn InitializeProcThreadAttributeList(
lpAttributeList: LPPROC_THREAD_ATTRIBUTE_LIST,
dwAttributeCount: DWORD,
dwFlags: DWORD,
lpSize: *SIZE_T,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn UpdateProcThreadAttribute(
lpAttributeList: LPPROC_THREAD_ATTRIBUTE_LIST,
dwFlags: DWORD,
Attribute: DWORD_PTR,
lpValue: PVOID,
cbSize: SIZE_T,
lpPreviousValue: ?PVOID,
lpReturnSize: ?*SIZE_T,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn PeekNamedPipe(
hNamedPipe: HANDLE,
lpBuffer: ?LPVOID,
nBufferSize: DWORD,
lpBytesRead: ?*DWORD,
lpTotalBytesAvail: ?*DWORD,
lpBytesLeftThisMessage: ?*DWORD,
) callconv(.winapi) BOOL;
// Duplicated here because lpCommandLine is not marked optional in zig std
pub extern "kernel32" 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,
) callconv(.winapi) BOOL;
/// https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcomputernamea
pub extern "kernel32" fn GetComputerNameA(
lpBuffer: LPSTR,
nSize: *DWORD,
) callconv(.winapi) BOOL;
/// https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw
pub extern "kernel32" fn GetTempPathW(
nBufferLength: DWORD,
lpBuffer: LPWSTR,
) callconv(.winapi) DWORD;
pub extern "kernel32" fn SetHandleInformation(
hObject: HANDLE,
dwMask: DWORD,
dwFlags: DWORD,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn CreateFileW(
lpFileName: LPCWSTR,
dwDesiredAccess: DWORD,
dwShareMode: DWORD,
lpSecurityAttributes: ?*SECURITY_ATTRIBUTES,
dwCreationDisposition: DWORD,
dwFlagsAndAttributes: DWORD,
hTemplateFile: ?HANDLE,
) callconv(.winapi) HANDLE;
pub extern "kernel32" fn CreateNamedPipeW(
lpName: LPCWSTR,
dwOpenMode: DWORD,
dwPipeMode: DWORD,
nMaxInstances: DWORD,
nOutBufferSize: DWORD,
nInBufferSize: DWORD,
nDefaultTimeOut: DWORD,
lpSecurityAttributes: ?*const SECURITY_ATTRIBUTES,
) callconv(.winapi) HANDLE;
pub extern "kernel32" fn CloseHandle(
hObject: HANDLE,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn VirtualAlloc(
lpAddress: ?LPVOID,
dwSize: SIZE_T,
flAllocationType: DWORD,
flProtect: DWORD,
) callconv(.winapi) ?LPVOID;
pub extern "kernel32" fn VirtualFree(
lpAddress: ?LPVOID,
dwSize: SIZE_T,
dwFreeType: DWORD,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn WaitForSingleObject(
hHandle: HANDLE,
dwMilliseconds: DWORD,
) callconv(.winapi) DWORD;
pub extern "kernel32" fn GetExitCodeProcess(
hProcess: HANDLE,
lpExitCode: *DWORD,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn TerminateProcess(
hProcess: HANDLE,
uExitCode: UINT,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn CancelIoEx(
hFile: HANDLE,
lpOverlapped: ?*OVERLAPPED,
) callconv(.winapi) BOOL;
pub extern "kernel32" fn ReadFile(
hFile: HANDLE,
lpBuffer: LPVOID,
nNumberOfBytesToRead: DWORD,
lpNumberOfBytesRead: ?*DWORD,
lpOverlapped: ?*OVERLAPPED,
) callconv(.winapi) BOOL;
};
pub const ntdll = struct {
pub extern "ntdll" fn NtCreateFile(
FileHandle: *HANDLE,
DesiredAccess: ACCESS_MASK,
ObjectAttributes: *OBJECT_ATTRIBUTES,
IoStatusBlock: *IO_STATUS_BLOCK,
AllocationSize: ?*LARGE_INTEGER,
FileAttributes: ULONG,
ShareAccess: ULONG,
CreateDisposition: ULONG,
CreateOptions: ULONG,
EaBuffer: ?*anyopaque,
EaLength: ULONG,
) callconv(.winapi) NTSTATUS;
};
};
pub const ProcThreadAttributeNumber = enum(DWORD) {
ProcThreadAttributePseudoConsole = 22,
_,
};
/// Corresponds to the ProcThreadAttributeValue define in WinBase.h
pub fn ProcThreadAttributeValue(
comptime attribute: ProcThreadAttributeNumber,
comptime thread: bool,
comptime input: bool,
comptime additive: bool,
) DWORD {
return (@intFromEnum(attribute) & PROC_THREAD_ATTRIBUTE_NUMBER) |
(if (thread) PROC_THREAD_ATTRIBUTE_THREAD else 0) |
(if (input) PROC_THREAD_ATTRIBUTE_INPUT else 0) |
(if (additive) PROC_THREAD_ATTRIBUTE_ADDITIVE else 0);
}
|