Safety Not Guaranteed: Calling Windows APIs using Unsafe & Syscall
summary
Shows how to call Windows APIs from Go without cgo by using syscall and unsafe, golang.org/x/sys/windows and the make_windows_syscall generator, with concrete examples like CreateJobObject / GetExtendedTcpTable and CredEnumerate. Explains mapping C types to Go, utf16 string conversions, the rule about keeping unsafe-to-uintptr conversions inside the syscall argument list, handling three levels of indirection for pointer arrays, a buffer-resize loop for variable-sized results, and builds working netstat and credential-enumeration tools (repo linked).
AI-written summary · about
chapters (27)
- 0:00Intro
- 2:06Windows Containers 2016
- 4:16No Syscalls on Windows
- 7:22Pointer Arithmetic
- 8:14Pointer Rules
- 9:04Bad Pointers
- 11:07Finding DLL and Call Signatures
- 14:12Loading a DLL Procedure
- 14:41Calling Windows APIs Mapping C Types
- 16:27Primitive C-Type mappings
- 17:19Mapping Parameters
- 17:37Go string to string
- 19:46Struct Types
- 22:37Inspecting Return Value
- 23:15CreateJobObject
- 23:45Locking memory down during syscall
- 25:18mkwinsyscall - source
- 28:19Memory Management
- 32:24Example: Designing For Unmanged Memory - Free
- 33:46Example: Designing For Unmanged Memory- Iterating
- 35:21Improvement - Copy & Free
- 35:54Example: Copy & Free Design - Cred Enumerate
- 36:48Example: Copy & Free Design - Credential/Attribute
- 37:28Managed Memory: Generic Example
- 39:29Example: GetExtendedTcp Table - Syscall Loop
- 42:43Example: GetExtendedTcp Table - Table to Rows
- 43:52Full Code Available
related talks
34mHigh-load CGo: calling nonblocking C/C++ code without surprise ☠️
53mStupid Gopher Tricks
06mLightning Talk: Go-ing Native: Supercharging Windows Support In Go
24m"Shattered Mirror: An Introduction to Reflect and Unsafe in Go"
57mHacking with Go
18mShattered Mirror: An Introduction to Reflect and Unsafe
52mGo Go Java Developer
12mAdventure in learning Go for a C# dev!
