| ... | ... | @@ -153,6 +153,24 @@ pub fn OpenFile(sub_path_w: []const u16, options: OpenFileOptions) OpenError!HAN |
| 153 | 153 | } |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | pub fn GetCurrentProcess() HANDLE { |
| 157 | const process_pseudo_handle: usize = @bitCast(@as(isize, -1)); |
| 158 | return @ptrFromInt(process_pseudo_handle); |
| 159 | } |
| 160 | |
| 161 | pub fn GetCurrentProcessId() DWORD { |
| 162 | return @truncate(@intFromPtr(teb().ClientId.UniqueProcess)); |
| 163 | } |
| 164 | |
| 165 | pub fn GetCurrentThread() HANDLE { |
| 166 | const thread_pseudo_handle: usize = @bitCast(@as(isize, -2)); |
| 167 | return @ptrFromInt(thread_pseudo_handle); |
| 168 | } |
| 169 | |
| 170 | pub fn GetCurrentThreadId() DWORD { |
| 171 | return @truncate(@intFromPtr(teb().ClientId.UniqueThread)); |
| 172 | } |
| 173 | |
| 156 | 174 | pub const CreatePipeError = error{ Unexpected, SystemResources }; |
| 157 | 175 | |
| 158 | 176 | var npfs: ?HANDLE = null; |
| ... | ... | @@ -259,12 +277,12 @@ pub fn CreatePipe(rd: *HANDLE, wr: *HANDLE, sattr: *const SECURITY_ATTRIBUTES) C |
| 259 | 277 | var write: HANDLE = undefined; |
| 260 | 278 | switch (ntdll.NtCreateFile( |
| 261 | 279 | &write, |
| 262 | | FILE_GENERIC_WRITE, |
| 280 | GENERIC_WRITE | SYNCHRONIZE | FILE_READ_ATTRIBUTES, |
| 263 | 281 | &attrs, |
| 264 | 282 | &iosb, |
| 265 | 283 | null, |
| 266 | 284 | 0, |
| 267 | | FILE_SHARE_READ, |
| 285 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 268 | 286 | FILE_OPEN, |
| 269 | 287 | FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, |
| 270 | 288 | null, |
| ... | ... | @@ -4309,7 +4327,11 @@ pub const THREAD_BASIC_INFORMATION = extern struct { |
| 4309 | 4327 | }; |
| 4310 | 4328 | |
| 4311 | 4329 | pub const TEB = extern struct { |
| 4312 | | Reserved1: [12]PVOID, |
| 4330 | NtTib: NT_TIB, |
| 4331 | EnvironmentPointer: PVOID, |
| 4332 | ClientId: CLIENT_ID, |
| 4333 | ActiveRpcHandle: PVOID, |
| 4334 | ThreadLocalStoragePointer: PVOID, |
| 4313 | 4335 | ProcessEnvironmentBlock: *PEB, |
| 4314 | 4336 | Reserved2: [399]PVOID, |
| 4315 | 4337 | Reserved3: [1952]u8, |
| ... | ... | @@ -4321,6 +4343,25 @@ pub const TEB = extern struct { |
| 4321 | 4343 | TlsExpansionSlots: PVOID, |
| 4322 | 4344 | }; |
| 4323 | 4345 | |
| 4346 | comptime { |
| 4347 | // Offsets taken from WinDbg info and Geoff Chappell[1] (RIP) |
| 4348 | // [1]: https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/api/pebteb/teb/index.htm |
| 4349 | assert(@offsetOf(TEB, "NtTib") == 0x00); |
| 4350 | if (@sizeOf(usize) == 4) { |
| 4351 | assert(@offsetOf(TEB, "EnvironmentPointer") == 0x1C); |
| 4352 | assert(@offsetOf(TEB, "ClientId") == 0x20); |
| 4353 | assert(@offsetOf(TEB, "ActiveRpcHandle") == 0x28); |
| 4354 | assert(@offsetOf(TEB, "ThreadLocalStoragePointer") == 0x2C); |
| 4355 | assert(@offsetOf(TEB, "ProcessEnvironmentBlock") == 0x30); |
| 4356 | } else if (@sizeOf(usize) == 8) { |
| 4357 | assert(@offsetOf(TEB, "EnvironmentPointer") == 0x38); |
| 4358 | assert(@offsetOf(TEB, "ClientId") == 0x40); |
| 4359 | assert(@offsetOf(TEB, "ActiveRpcHandle") == 0x50); |
| 4360 | assert(@offsetOf(TEB, "ThreadLocalStoragePointer") == 0x58); |
| 4361 | assert(@offsetOf(TEB, "ProcessEnvironmentBlock") == 0x60); |
| 4362 | } |
| 4363 | } |
| 4364 | |
| 4324 | 4365 | pub const EXCEPTION_REGISTRATION_RECORD = extern struct { |
| 4325 | 4366 | Next: ?*EXCEPTION_REGISTRATION_RECORD, |
| 4326 | 4367 | Handler: ?*EXCEPTION_DISPOSITION, |