forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/windows/syscall_windows.go

3369 lines
84 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Windows system calls.
package windows
import (
errorspkg "errors"
"fmt"
"runtime"
"sync"
"syscall"
"time"
"unicode/utf16"
"unsafe"
)
type Handle uintptr
2024-02-18 10:42:21 +00:00
type HWND uintptr
const (
InvalidHandle = ^Handle(0)
InvalidHWND = ^HWND(0)
2024-02-18 10:42:21 +00:00
// Flags for DefineDosDevice.
2024-02-18 10:42:21 +00:00
DDD_EXACT_MATCH_ON_REMOVE = 0x00000004
DDD_NO_BROADCAST_SYSTEM = 0x00000008
DDD_RAW_TARGET_PATH = 0x00000001
DDD_REMOVE_DEFINITION = 0x00000002
2024-02-18 10:42:21 +00:00
// Return values for GetDriveType.
DRIVE_UNKNOWN = 0
2024-02-18 10:42:21 +00:00
DRIVE_NO_ROOT_DIR = 1
DRIVE_REMOVABLE = 2
DRIVE_FIXED = 3
DRIVE_REMOTE = 4
DRIVE_CDROM = 5
DRIVE_RAMDISK = 6
2024-02-18 10:42:21 +00:00
// File system flags from GetVolumeInformation and GetVolumeInformationByHandle.
FILE_CASE_SENSITIVE_SEARCH = 0x00000001
FILE_CASE_PRESERVED_NAMES = 0x00000002
FILE_FILE_COMPRESSION = 0x00000010
FILE_DAX_VOLUME = 0x20000000
FILE_NAMED_STREAMS = 0x00040000
FILE_PERSISTENT_ACLS = 0x00000008
FILE_READ_ONLY_VOLUME = 0x00080000
FILE_SEQUENTIAL_WRITE_ONCE = 0x00100000
FILE_SUPPORTS_ENCRYPTION = 0x00020000
2024-02-18 10:42:21 +00:00
FILE_SUPPORTS_EXTENDED_ATTRIBUTES = 0x00800000
FILE_SUPPORTS_HARD_LINKS = 0x00400000
FILE_SUPPORTS_OBJECT_IDS = 0x00010000
FILE_SUPPORTS_OPEN_BY_FILE_ID = 0x01000000
FILE_SUPPORTS_REPARSE_POINTS = 0x00000080
FILE_SUPPORTS_SPARSE_FILES = 0x00000040
FILE_SUPPORTS_TRANSACTIONS = 0x00200000
FILE_SUPPORTS_USN_JOURNAL = 0x02000000
FILE_UNICODE_ON_DISK = 0x00000004
FILE_VOLUME_IS_COMPRESSED = 0x00008000
FILE_VOLUME_QUOTAS = 0x00000020
2024-02-18 10:42:21 +00:00
// Flags for LockFileEx.
2024-02-18 10:42:21 +00:00
LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
2024-02-18 10:42:21 +00:00
// Return value of SleepEx and other APC functions
2024-02-18 10:42:21 +00:00
WAIT_IO_COMPLETION = 0x000000C0
)
// StringToUTF16 is deprecated. Use UTF16FromString instead.
2024-02-18 10:42:21 +00:00
// If s contains a NUL byte this function panics instead of
2024-02-18 10:42:21 +00:00
// returning an error.
2024-02-18 10:42:21 +00:00
func StringToUTF16(s string) []uint16 {
2024-02-18 10:42:21 +00:00
a, err := UTF16FromString(s)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
panic("windows: string with NUL passed to StringToUTF16")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return a
2024-02-18 10:42:21 +00:00
}
// UTF16FromString returns the UTF-16 encoding of the UTF-8 string
2024-02-18 10:42:21 +00:00
// s, with a terminating NUL added. If s contains a NUL byte at any
2024-02-18 10:42:21 +00:00
// location, it returns (nil, syscall.EINVAL).
2024-02-18 10:42:21 +00:00
func UTF16FromString(s string) ([]uint16, error) {
2024-02-18 10:42:21 +00:00
return syscall.UTF16FromString(s)
2024-02-18 10:42:21 +00:00
}
// UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,
2024-02-18 10:42:21 +00:00
// with a terminating NUL and any bytes after the NUL removed.
2024-02-18 10:42:21 +00:00
func UTF16ToString(s []uint16) string {
2024-02-18 10:42:21 +00:00
return syscall.UTF16ToString(s)
2024-02-18 10:42:21 +00:00
}
// StringToUTF16Ptr is deprecated. Use UTF16PtrFromString instead.
2024-02-18 10:42:21 +00:00
// If s contains a NUL byte this function panics instead of
2024-02-18 10:42:21 +00:00
// returning an error.
2024-02-18 10:42:21 +00:00
func StringToUTF16Ptr(s string) *uint16 { return &StringToUTF16(s)[0] }
// UTF16PtrFromString returns pointer to the UTF-16 encoding of
2024-02-18 10:42:21 +00:00
// the UTF-8 string s, with a terminating NUL added. If s
2024-02-18 10:42:21 +00:00
// contains a NUL byte at any location, it returns (nil, syscall.EINVAL).
2024-02-18 10:42:21 +00:00
func UTF16PtrFromString(s string) (*uint16, error) {
2024-02-18 10:42:21 +00:00
a, err := UTF16FromString(s)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return &a[0], nil
2024-02-18 10:42:21 +00:00
}
// UTF16PtrToString takes a pointer to a UTF-16 sequence and returns the corresponding UTF-8 encoded string.
2024-02-18 10:42:21 +00:00
// If the pointer is nil, it returns the empty string. It assumes that the UTF-16 sequence is terminated
2024-02-18 10:42:21 +00:00
// at a zero word; if the zero word is not present, the program may crash.
2024-02-18 10:42:21 +00:00
func UTF16PtrToString(p *uint16) string {
2024-02-18 10:42:21 +00:00
if p == nil {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if *p == 0 {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
// Find NUL terminator.
2024-02-18 10:42:21 +00:00
n := 0
2024-02-18 10:42:21 +00:00
for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; n++ {
2024-02-18 10:42:21 +00:00
ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p))
2024-02-18 10:42:21 +00:00
}
2024-05-14 13:07:09 +00:00
return UTF16ToString(unsafe.Slice(p, n))
2024-02-18 10:42:21 +00:00
}
func Getpagesize() int { return 4096 }
// NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention.
2024-02-18 10:42:21 +00:00
// This is useful when interoperating with Windows code requiring callbacks.
2024-02-18 10:42:21 +00:00
// The argument is expected to be a function with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr.
2024-02-18 10:42:21 +00:00
func NewCallback(fn interface{}) uintptr {
2024-02-18 10:42:21 +00:00
return syscall.NewCallback(fn)
2024-02-18 10:42:21 +00:00
}
// NewCallbackCDecl converts a Go function to a function pointer conforming to the cdecl calling convention.
2024-02-18 10:42:21 +00:00
// This is useful when interoperating with Windows code requiring callbacks.
2024-02-18 10:42:21 +00:00
// The argument is expected to be a function with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr.
2024-02-18 10:42:21 +00:00
func NewCallbackCDecl(fn interface{}) uintptr {
2024-02-18 10:42:21 +00:00
return syscall.NewCallbackCDecl(fn)
2024-02-18 10:42:21 +00:00
}
// windows api calls
//sys GetLastError() (lasterr error)
2024-02-18 10:42:21 +00:00
//sys LoadLibrary(libname string) (handle Handle, err error) = LoadLibraryW
2024-02-18 10:42:21 +00:00
//sys LoadLibraryEx(libname string, zero Handle, flags uintptr) (handle Handle, err error) = LoadLibraryExW
2024-02-18 10:42:21 +00:00
//sys FreeLibrary(handle Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys GetProcAddress(module Handle, procname string) (proc uintptr, err error)
2024-02-18 10:42:21 +00:00
//sys GetModuleFileName(module Handle, filename *uint16, size uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
2024-02-18 10:42:21 +00:00
//sys GetModuleHandleEx(flags uint32, moduleName *uint16, module *Handle) (err error) = kernel32.GetModuleHandleExW
2024-02-18 10:42:21 +00:00
//sys SetDefaultDllDirectories(directoryFlags uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys AddDllDirectory(path *uint16) (cookie uintptr, err error) = kernel32.AddDllDirectory
2024-02-18 10:42:21 +00:00
//sys RemoveDllDirectory(cookie uintptr) (err error) = kernel32.RemoveDllDirectory
2024-02-18 10:42:21 +00:00
//sys SetDllDirectory(path string) (err error) = kernel32.SetDllDirectoryW
2024-02-18 10:42:21 +00:00
//sys GetVersion() (ver uint32, err error)
2024-02-18 10:42:21 +00:00
//sys FormatMessage(flags uint32, msgsrc uintptr, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) = FormatMessageW
2024-02-18 10:42:21 +00:00
//sys ExitProcess(exitcode uint32)
2024-02-18 10:42:21 +00:00
//sys IsWow64Process(handle Handle, isWow64 *bool) (err error) = IsWow64Process
2024-02-18 10:42:21 +00:00
//sys IsWow64Process2(handle Handle, processMachine *uint16, nativeMachine *uint16) (err error) = IsWow64Process2?
2024-02-18 10:42:21 +00:00
//sys CreateFile(name *uint16, access uint32, mode uint32, sa *SecurityAttributes, createmode uint32, attrs uint32, templatefile Handle) (handle Handle, err error) [failretval==InvalidHandle] = CreateFileW
2024-02-18 10:42:21 +00:00
//sys CreateNamedPipe(name *uint16, flags uint32, pipeMode uint32, maxInstances uint32, outSize uint32, inSize uint32, defaultTimeout uint32, sa *SecurityAttributes) (handle Handle, err error) [failretval==InvalidHandle] = CreateNamedPipeW
2024-02-18 10:42:21 +00:00
//sys ConnectNamedPipe(pipe Handle, overlapped *Overlapped) (err error)
2024-05-14 13:07:09 +00:00
//sys DisconnectNamedPipe(pipe Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys GetNamedPipeInfo(pipe Handle, flags *uint32, outSize *uint32, inSize *uint32, maxInstances *uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys GetNamedPipeHandleState(pipe Handle, state *uint32, curInstances *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32, userName *uint16, maxUserNameSize uint32) (err error) = GetNamedPipeHandleStateW
2024-02-18 10:42:21 +00:00
//sys SetNamedPipeHandleState(pipe Handle, state *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32) (err error) = SetNamedPipeHandleState
2024-02-18 10:42:21 +00:00
//sys readFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error) = ReadFile
2024-02-18 10:42:21 +00:00
//sys writeFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error) = WriteFile
2024-02-18 10:42:21 +00:00
//sys GetOverlappedResult(handle Handle, overlapped *Overlapped, done *uint32, wait bool) (err error)
2024-02-18 10:42:21 +00:00
//sys SetFilePointer(handle Handle, lowoffset int32, highoffsetptr *int32, whence uint32) (newlowoffset uint32, err error) [failretval==0xffffffff]
2024-02-18 10:42:21 +00:00
//sys CloseHandle(handle Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys GetStdHandle(stdhandle uint32) (handle Handle, err error) [failretval==InvalidHandle]
2024-02-18 10:42:21 +00:00
//sys SetStdHandle(stdhandle uint32, handle Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys findFirstFile1(name *uint16, data *win32finddata1) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstFileW
2024-02-18 10:42:21 +00:00
//sys findNextFile1(handle Handle, data *win32finddata1) (err error) = FindNextFileW
2024-02-18 10:42:21 +00:00
//sys FindClose(handle Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys GetFileInformationByHandle(handle Handle, data *ByHandleFileInformation) (err error)
2024-02-18 10:42:21 +00:00
//sys GetFileInformationByHandleEx(handle Handle, class uint32, outBuffer *byte, outBufferLen uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys SetFileInformationByHandle(handle Handle, class uint32, inBuffer *byte, inBufferLen uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys GetCurrentDirectory(buflen uint32, buf *uint16) (n uint32, err error) = GetCurrentDirectoryW
2024-02-18 10:42:21 +00:00
//sys SetCurrentDirectory(path *uint16) (err error) = SetCurrentDirectoryW
2024-02-18 10:42:21 +00:00
//sys CreateDirectory(path *uint16, sa *SecurityAttributes) (err error) = CreateDirectoryW
2024-02-18 10:42:21 +00:00
//sys RemoveDirectory(path *uint16) (err error) = RemoveDirectoryW
2024-02-18 10:42:21 +00:00
//sys DeleteFile(path *uint16) (err error) = DeleteFileW
2024-02-18 10:42:21 +00:00
//sys MoveFile(from *uint16, to *uint16) (err error) = MoveFileW
2024-02-18 10:42:21 +00:00
//sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW
2024-02-18 10:42:21 +00:00
//sys LockFileEx(file Handle, flags uint32, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *Overlapped) (err error)
2024-02-18 10:42:21 +00:00
//sys UnlockFileEx(file Handle, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *Overlapped) (err error)
2024-02-18 10:42:21 +00:00
//sys GetComputerName(buf *uint16, n *uint32) (err error) = GetComputerNameW
2024-02-18 10:42:21 +00:00
//sys GetComputerNameEx(nametype uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW
2024-02-18 10:42:21 +00:00
//sys SetEndOfFile(handle Handle) (err error)
2024-05-14 13:07:09 +00:00
//sys SetFileValidData(handle Handle, validDataLength int64) (err error)
2024-02-18 10:42:21 +00:00
//sys GetSystemTimeAsFileTime(time *Filetime)
2024-02-18 10:42:21 +00:00
//sys GetSystemTimePreciseAsFileTime(time *Filetime)
2024-02-18 10:42:21 +00:00
//sys GetTimeZoneInformation(tzi *Timezoneinformation) (rc uint32, err error) [failretval==0xffffffff]
2024-02-18 10:42:21 +00:00
//sys CreateIoCompletionPort(filehandle Handle, cphandle Handle, key uintptr, threadcnt uint32) (handle Handle, err error)
2024-02-18 10:42:21 +00:00
//sys GetQueuedCompletionStatus(cphandle Handle, qty *uint32, key *uintptr, overlapped **Overlapped, timeout uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys PostQueuedCompletionStatus(cphandle Handle, qty uint32, key uintptr, overlapped *Overlapped) (err error)
2024-02-18 10:42:21 +00:00
//sys CancelIo(s Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys CancelIoEx(s Handle, o *Overlapped) (err error)
2024-02-18 10:42:21 +00:00
//sys CreateProcess(appName *uint16, commandLine *uint16, procSecurity *SecurityAttributes, threadSecurity *SecurityAttributes, inheritHandles bool, creationFlags uint32, env *uint16, currentDir *uint16, startupInfo *StartupInfo, outProcInfo *ProcessInformation) (err error) = CreateProcessW
2024-02-18 10:42:21 +00:00
//sys CreateProcessAsUser(token Token, appName *uint16, commandLine *uint16, procSecurity *SecurityAttributes, threadSecurity *SecurityAttributes, inheritHandles bool, creationFlags uint32, env *uint16, currentDir *uint16, startupInfo *StartupInfo, outProcInfo *ProcessInformation) (err error) = advapi32.CreateProcessAsUserW
2024-02-18 10:42:21 +00:00
//sys initializeProcThreadAttributeList(attrlist *ProcThreadAttributeList, attrcount uint32, flags uint32, size *uintptr) (err error) = InitializeProcThreadAttributeList
2024-02-18 10:42:21 +00:00
//sys deleteProcThreadAttributeList(attrlist *ProcThreadAttributeList) = DeleteProcThreadAttributeList
2024-02-18 10:42:21 +00:00
//sys updateProcThreadAttribute(attrlist *ProcThreadAttributeList, flags uint32, attr uintptr, value unsafe.Pointer, size uintptr, prevvalue unsafe.Pointer, returnedsize *uintptr) (err error) = UpdateProcThreadAttribute
2024-02-18 10:42:21 +00:00
//sys OpenProcess(desiredAccess uint32, inheritHandle bool, processId uint32) (handle Handle, err error)
2024-02-18 10:42:21 +00:00
//sys ShellExecute(hwnd Handle, verb *uint16, file *uint16, args *uint16, cwd *uint16, showCmd int32) (err error) [failretval<=32] = shell32.ShellExecuteW
2024-02-18 10:42:21 +00:00
//sys GetWindowThreadProcessId(hwnd HWND, pid *uint32) (tid uint32, err error) = user32.GetWindowThreadProcessId
2024-02-18 10:42:21 +00:00
//sys GetShellWindow() (shellWindow HWND) = user32.GetShellWindow
2024-02-18 10:42:21 +00:00
//sys MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret int32, err error) [failretval==0] = user32.MessageBoxW
2024-02-18 10:42:21 +00:00
//sys ExitWindowsEx(flags uint32, reason uint32) (err error) = user32.ExitWindowsEx
2024-02-18 10:42:21 +00:00
//sys shGetKnownFolderPath(id *KNOWNFOLDERID, flags uint32, token Token, path **uint16) (ret error) = shell32.SHGetKnownFolderPath
2024-02-18 10:42:21 +00:00
//sys TerminateProcess(handle Handle, exitcode uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys GetExitCodeProcess(handle Handle, exitcode *uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys getStartupInfo(startupInfo *StartupInfo) = GetStartupInfoW
2024-02-18 10:42:21 +00:00
//sys GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error)
2024-02-18 10:42:21 +00:00
//sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff]
2024-02-18 10:42:21 +00:00
//sys waitForMultipleObjects(count uint32, handles uintptr, waitAll bool, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff] = WaitForMultipleObjects
2024-02-18 10:42:21 +00:00
//sys GetTempPath(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPathW
2024-02-18 10:42:21 +00:00
//sys CreatePipe(readhandle *Handle, writehandle *Handle, sa *SecurityAttributes, size uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys GetFileType(filehandle Handle) (n uint32, err error)
2024-02-18 10:42:21 +00:00
//sys CryptAcquireContext(provhandle *Handle, container *uint16, provider *uint16, provtype uint32, flags uint32) (err error) = advapi32.CryptAcquireContextW
2024-02-18 10:42:21 +00:00
//sys CryptReleaseContext(provhandle Handle, flags uint32) (err error) = advapi32.CryptReleaseContext
2024-02-18 10:42:21 +00:00
//sys CryptGenRandom(provhandle Handle, buflen uint32, buf *byte) (err error) = advapi32.CryptGenRandom
2024-02-18 10:42:21 +00:00
//sys GetEnvironmentStrings() (envs *uint16, err error) [failretval==nil] = kernel32.GetEnvironmentStringsW
2024-02-18 10:42:21 +00:00
//sys FreeEnvironmentStrings(envs *uint16) (err error) = kernel32.FreeEnvironmentStringsW
2024-02-18 10:42:21 +00:00
//sys GetEnvironmentVariable(name *uint16, buffer *uint16, size uint32) (n uint32, err error) = kernel32.GetEnvironmentVariableW
2024-02-18 10:42:21 +00:00
//sys SetEnvironmentVariable(name *uint16, value *uint16) (err error) = kernel32.SetEnvironmentVariableW
2024-02-18 10:42:21 +00:00
//sys ExpandEnvironmentStrings(src *uint16, dst *uint16, size uint32) (n uint32, err error) = kernel32.ExpandEnvironmentStringsW
2024-02-18 10:42:21 +00:00
//sys CreateEnvironmentBlock(block **uint16, token Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
2024-02-18 10:42:21 +00:00
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
2024-02-18 10:42:21 +00:00
//sys getTickCount64() (ms uint64) = kernel32.GetTickCount64
2024-02-18 10:42:21 +00:00
//sys GetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error)
2024-02-18 10:42:21 +00:00
//sys SetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error)
2024-02-18 10:42:21 +00:00
//sys GetFileAttributes(name *uint16) (attrs uint32, err error) [failretval==INVALID_FILE_ATTRIBUTES] = kernel32.GetFileAttributesW
2024-02-18 10:42:21 +00:00
//sys SetFileAttributes(name *uint16, attrs uint32) (err error) = kernel32.SetFileAttributesW
2024-02-18 10:42:21 +00:00
//sys GetFileAttributesEx(name *uint16, level uint32, info *byte) (err error) = kernel32.GetFileAttributesExW
2024-02-18 10:42:21 +00:00
//sys GetCommandLine() (cmd *uint16) = kernel32.GetCommandLineW
2024-02-18 10:42:21 +00:00
//sys commandLineToArgv(cmd *uint16, argc *int32) (argv **uint16, err error) [failretval==nil] = shell32.CommandLineToArgvW
2024-02-18 10:42:21 +00:00
//sys LocalFree(hmem Handle) (handle Handle, err error) [failretval!=0]
2024-02-18 10:42:21 +00:00
//sys LocalAlloc(flags uint32, length uint32) (ptr uintptr, err error)
2024-02-18 10:42:21 +00:00
//sys SetHandleInformation(handle Handle, mask uint32, flags uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys FlushFileBuffers(handle Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys GetFullPathName(path *uint16, buflen uint32, buf *uint16, fname **uint16) (n uint32, err error) = kernel32.GetFullPathNameW
2024-02-18 10:42:21 +00:00
//sys GetLongPathName(path *uint16, buf *uint16, buflen uint32) (n uint32, err error) = kernel32.GetLongPathNameW
2024-02-18 10:42:21 +00:00
//sys GetShortPathName(longpath *uint16, shortpath *uint16, buflen uint32) (n uint32, err error) = kernel32.GetShortPathNameW
2024-02-18 10:42:21 +00:00
//sys GetFinalPathNameByHandle(file Handle, filePath *uint16, filePathSize uint32, flags uint32) (n uint32, err error) = kernel32.GetFinalPathNameByHandleW
2024-02-18 10:42:21 +00:00
//sys CreateFileMapping(fhandle Handle, sa *SecurityAttributes, prot uint32, maxSizeHigh uint32, maxSizeLow uint32, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateFileMappingW
2024-02-18 10:42:21 +00:00
//sys MapViewOfFile(handle Handle, access uint32, offsetHigh uint32, offsetLow uint32, length uintptr) (addr uintptr, err error)
2024-02-18 10:42:21 +00:00
//sys UnmapViewOfFile(addr uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys FlushViewOfFile(addr uintptr, length uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys VirtualLock(addr uintptr, length uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys VirtualUnlock(addr uintptr, length uintptr) (err error)
2024-02-18 10:42:21 +00:00
//sys VirtualAlloc(address uintptr, size uintptr, alloctype uint32, protect uint32) (value uintptr, err error) = kernel32.VirtualAlloc
2024-02-18 10:42:21 +00:00
//sys VirtualFree(address uintptr, size uintptr, freetype uint32) (err error) = kernel32.VirtualFree
2024-02-18 10:42:21 +00:00
//sys VirtualProtect(address uintptr, size uintptr, newprotect uint32, oldprotect *uint32) (err error) = kernel32.VirtualProtect
2024-02-18 10:42:21 +00:00
//sys VirtualProtectEx(process Handle, address uintptr, size uintptr, newProtect uint32, oldProtect *uint32) (err error) = kernel32.VirtualProtectEx
2024-02-18 10:42:21 +00:00
//sys VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQuery
2024-02-18 10:42:21 +00:00
//sys VirtualQueryEx(process Handle, address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQueryEx
2024-02-18 10:42:21 +00:00
//sys ReadProcessMemory(process Handle, baseAddress uintptr, buffer *byte, size uintptr, numberOfBytesRead *uintptr) (err error) = kernel32.ReadProcessMemory
2024-02-18 10:42:21 +00:00
//sys WriteProcessMemory(process Handle, baseAddress uintptr, buffer *byte, size uintptr, numberOfBytesWritten *uintptr) (err error) = kernel32.WriteProcessMemory
2024-02-18 10:42:21 +00:00
//sys TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) = mswsock.TransmitFile
2024-02-18 10:42:21 +00:00
//sys ReadDirectoryChanges(handle Handle, buf *byte, buflen uint32, watchSubTree bool, mask uint32, retlen *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) = kernel32.ReadDirectoryChangesW
2024-02-18 10:42:21 +00:00
//sys FindFirstChangeNotification(path string, watchSubtree bool, notifyFilter uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.FindFirstChangeNotificationW
2024-02-18 10:42:21 +00:00
//sys FindNextChangeNotification(handle Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys FindCloseChangeNotification(handle Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys CertOpenSystemStore(hprov Handle, name *uint16) (store Handle, err error) = crypt32.CertOpenSystemStoreW
2024-02-18 10:42:21 +00:00
//sys CertOpenStore(storeProvider uintptr, msgAndCertEncodingType uint32, cryptProv uintptr, flags uint32, para uintptr) (handle Handle, err error) = crypt32.CertOpenStore
2024-02-18 10:42:21 +00:00
//sys CertEnumCertificatesInStore(store Handle, prevContext *CertContext) (context *CertContext, err error) [failretval==nil] = crypt32.CertEnumCertificatesInStore
2024-02-18 10:42:21 +00:00
//sys CertAddCertificateContextToStore(store Handle, certContext *CertContext, addDisposition uint32, storeContext **CertContext) (err error) = crypt32.CertAddCertificateContextToStore
2024-02-18 10:42:21 +00:00
//sys CertCloseStore(store Handle, flags uint32) (err error) = crypt32.CertCloseStore
2024-02-18 10:42:21 +00:00
//sys CertDeleteCertificateFromStore(certContext *CertContext) (err error) = crypt32.CertDeleteCertificateFromStore
2024-02-18 10:42:21 +00:00
//sys CertDuplicateCertificateContext(certContext *CertContext) (dupContext *CertContext) = crypt32.CertDuplicateCertificateContext
2024-02-18 10:42:21 +00:00
//sys PFXImportCertStore(pfx *CryptDataBlob, password *uint16, flags uint32) (store Handle, err error) = crypt32.PFXImportCertStore
2024-02-18 10:42:21 +00:00
//sys CertGetCertificateChain(engine Handle, leaf *CertContext, time *Filetime, additionalStore Handle, para *CertChainPara, flags uint32, reserved uintptr, chainCtx **CertChainContext) (err error) = crypt32.CertGetCertificateChain
2024-02-18 10:42:21 +00:00
//sys CertFreeCertificateChain(ctx *CertChainContext) = crypt32.CertFreeCertificateChain
2024-02-18 10:42:21 +00:00
//sys CertCreateCertificateContext(certEncodingType uint32, certEncoded *byte, encodedLen uint32) (context *CertContext, err error) [failretval==nil] = crypt32.CertCreateCertificateContext
2024-02-18 10:42:21 +00:00
//sys CertFreeCertificateContext(ctx *CertContext) (err error) = crypt32.CertFreeCertificateContext
2024-02-18 10:42:21 +00:00
//sys CertVerifyCertificateChainPolicy(policyOID uintptr, chain *CertChainContext, para *CertChainPolicyPara, status *CertChainPolicyStatus) (err error) = crypt32.CertVerifyCertificateChainPolicy
2024-02-18 10:42:21 +00:00
//sys CertGetNameString(certContext *CertContext, nameType uint32, flags uint32, typePara unsafe.Pointer, name *uint16, size uint32) (chars uint32) = crypt32.CertGetNameStringW
2024-02-18 10:42:21 +00:00
//sys CertFindExtension(objId *byte, countExtensions uint32, extensions *CertExtension) (ret *CertExtension) = crypt32.CertFindExtension
2024-02-18 10:42:21 +00:00
//sys CertFindCertificateInStore(store Handle, certEncodingType uint32, findFlags uint32, findType uint32, findPara unsafe.Pointer, prevCertContext *CertContext) (cert *CertContext, err error) [failretval==nil] = crypt32.CertFindCertificateInStore
2024-02-18 10:42:21 +00:00
//sys CertFindChainInStore(store Handle, certEncodingType uint32, findFlags uint32, findType uint32, findPara unsafe.Pointer, prevChainContext *CertChainContext) (certchain *CertChainContext, err error) [failretval==nil] = crypt32.CertFindChainInStore
2024-02-18 10:42:21 +00:00
//sys CryptAcquireCertificatePrivateKey(cert *CertContext, flags uint32, parameters unsafe.Pointer, cryptProvOrNCryptKey *Handle, keySpec *uint32, callerFreeProvOrNCryptKey *bool) (err error) = crypt32.CryptAcquireCertificatePrivateKey
2024-02-18 10:42:21 +00:00
//sys CryptQueryObject(objectType uint32, object unsafe.Pointer, expectedContentTypeFlags uint32, expectedFormatTypeFlags uint32, flags uint32, msgAndCertEncodingType *uint32, contentType *uint32, formatType *uint32, certStore *Handle, msg *Handle, context *unsafe.Pointer) (err error) = crypt32.CryptQueryObject
2024-02-18 10:42:21 +00:00
//sys CryptDecodeObject(encodingType uint32, structType *byte, encodedBytes *byte, lenEncodedBytes uint32, flags uint32, decoded unsafe.Pointer, decodedLen *uint32) (err error) = crypt32.CryptDecodeObject
2024-02-18 10:42:21 +00:00
//sys CryptProtectData(dataIn *DataBlob, name *uint16, optionalEntropy *DataBlob, reserved uintptr, promptStruct *CryptProtectPromptStruct, flags uint32, dataOut *DataBlob) (err error) = crypt32.CryptProtectData
2024-02-18 10:42:21 +00:00
//sys CryptUnprotectData(dataIn *DataBlob, name **uint16, optionalEntropy *DataBlob, reserved uintptr, promptStruct *CryptProtectPromptStruct, flags uint32, dataOut *DataBlob) (err error) = crypt32.CryptUnprotectData
2024-02-18 10:42:21 +00:00
//sys WinVerifyTrustEx(hwnd HWND, actionId *GUID, data *WinTrustData) (ret error) = wintrust.WinVerifyTrustEx
2024-02-18 10:42:21 +00:00
//sys RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno error) = advapi32.RegOpenKeyExW
2024-02-18 10:42:21 +00:00
//sys RegCloseKey(key Handle) (regerrno error) = advapi32.RegCloseKey
2024-02-18 10:42:21 +00:00
//sys RegQueryInfoKey(key Handle, class *uint16, classLen *uint32, reserved *uint32, subkeysLen *uint32, maxSubkeyLen *uint32, maxClassLen *uint32, valuesLen *uint32, maxValueNameLen *uint32, maxValueLen *uint32, saLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegQueryInfoKeyW
2024-02-18 10:42:21 +00:00
//sys RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegEnumKeyExW
2024-02-18 10:42:21 +00:00
//sys RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) = advapi32.RegQueryValueExW
2024-02-18 10:42:21 +00:00
//sys RegNotifyChangeKeyValue(key Handle, watchSubtree bool, notifyFilter uint32, event Handle, asynchronous bool) (regerrno error) = advapi32.RegNotifyChangeKeyValue
2024-02-18 10:42:21 +00:00
//sys GetCurrentProcessId() (pid uint32) = kernel32.GetCurrentProcessId
2024-02-18 10:42:21 +00:00
//sys ProcessIdToSessionId(pid uint32, sessionid *uint32) (err error) = kernel32.ProcessIdToSessionId
2024-02-18 10:42:21 +00:00
//sys ClosePseudoConsole(console Handle) = kernel32.ClosePseudoConsole
2024-02-18 10:42:21 +00:00
//sys createPseudoConsole(size uint32, in Handle, out Handle, flags uint32, pconsole *Handle) (hr error) = kernel32.CreatePseudoConsole
2024-02-18 10:42:21 +00:00
//sys GetConsoleMode(console Handle, mode *uint32) (err error) = kernel32.GetConsoleMode
2024-02-18 10:42:21 +00:00
//sys SetConsoleMode(console Handle, mode uint32) (err error) = kernel32.SetConsoleMode
2024-02-18 10:42:21 +00:00
//sys GetConsoleScreenBufferInfo(console Handle, info *ConsoleScreenBufferInfo) (err error) = kernel32.GetConsoleScreenBufferInfo
2024-02-18 10:42:21 +00:00
//sys setConsoleCursorPosition(console Handle, position uint32) (err error) = kernel32.SetConsoleCursorPosition
2024-02-18 10:42:21 +00:00
//sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW
2024-02-18 10:42:21 +00:00
//sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW
2024-02-18 10:42:21 +00:00
//sys resizePseudoConsole(pconsole Handle, size uint32) (hr error) = kernel32.ResizePseudoConsole
2024-02-18 10:42:21 +00:00
//sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot
2024-02-18 10:42:21 +00:00
//sys Module32First(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW
2024-02-18 10:42:21 +00:00
//sys Module32Next(snapshot Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32NextW
2024-02-18 10:42:21 +00:00
//sys Process32First(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32FirstW
2024-02-18 10:42:21 +00:00
//sys Process32Next(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32NextW
2024-02-18 10:42:21 +00:00
//sys Thread32First(snapshot Handle, threadEntry *ThreadEntry32) (err error)
2024-02-18 10:42:21 +00:00
//sys Thread32Next(snapshot Handle, threadEntry *ThreadEntry32) (err error)
2024-02-18 10:42:21 +00:00
//sys DeviceIoControl(handle Handle, ioControlCode uint32, inBuffer *byte, inBufferSize uint32, outBuffer *byte, outBufferSize uint32, bytesReturned *uint32, overlapped *Overlapped) (err error)
2024-02-18 10:42:21 +00:00
// This function returns 1 byte BOOLEAN rather than the 4 byte BOOL.
2024-02-18 10:42:21 +00:00
//sys CreateSymbolicLink(symlinkfilename *uint16, targetfilename *uint16, flags uint32) (err error) [failretval&0xff==0] = CreateSymbolicLinkW
2024-02-18 10:42:21 +00:00
//sys CreateHardLink(filename *uint16, existingfilename *uint16, reserved uintptr) (err error) [failretval&0xff==0] = CreateHardLinkW
2024-02-18 10:42:21 +00:00
//sys GetCurrentThreadId() (id uint32)
2024-02-18 10:42:21 +00:00
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateEventW
2024-02-18 10:42:21 +00:00
//sys CreateEventEx(eventAttrs *SecurityAttributes, name *uint16, flags uint32, desiredAccess uint32) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateEventExW
2024-02-18 10:42:21 +00:00
//sys OpenEvent(desiredAccess uint32, inheritHandle bool, name *uint16) (handle Handle, err error) = kernel32.OpenEventW
2024-02-18 10:42:21 +00:00
//sys SetEvent(event Handle) (err error) = kernel32.SetEvent
2024-02-18 10:42:21 +00:00
//sys ResetEvent(event Handle) (err error) = kernel32.ResetEvent
2024-02-18 10:42:21 +00:00
//sys PulseEvent(event Handle) (err error) = kernel32.PulseEvent
2024-02-18 10:42:21 +00:00
//sys CreateMutex(mutexAttrs *SecurityAttributes, initialOwner bool, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateMutexW
2024-02-18 10:42:21 +00:00
//sys CreateMutexEx(mutexAttrs *SecurityAttributes, name *uint16, flags uint32, desiredAccess uint32) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateMutexExW
2024-02-18 10:42:21 +00:00
//sys OpenMutex(desiredAccess uint32, inheritHandle bool, name *uint16) (handle Handle, err error) = kernel32.OpenMutexW
2024-02-18 10:42:21 +00:00
//sys ReleaseMutex(mutex Handle) (err error) = kernel32.ReleaseMutex
2024-02-18 10:42:21 +00:00
//sys SleepEx(milliseconds uint32, alertable bool) (ret uint32) = kernel32.SleepEx
2024-02-18 10:42:21 +00:00
//sys CreateJobObject(jobAttr *SecurityAttributes, name *uint16) (handle Handle, err error) = kernel32.CreateJobObjectW
2024-02-18 10:42:21 +00:00
//sys AssignProcessToJobObject(job Handle, process Handle) (err error) = kernel32.AssignProcessToJobObject
2024-02-18 10:42:21 +00:00
//sys TerminateJobObject(job Handle, exitCode uint32) (err error) = kernel32.TerminateJobObject
2024-02-18 10:42:21 +00:00
//sys SetErrorMode(mode uint32) (ret uint32) = kernel32.SetErrorMode
2024-02-18 10:42:21 +00:00
//sys ResumeThread(thread Handle) (ret uint32, err error) [failretval==0xffffffff] = kernel32.ResumeThread
2024-02-18 10:42:21 +00:00
//sys SetPriorityClass(process Handle, priorityClass uint32) (err error) = kernel32.SetPriorityClass
2024-02-18 10:42:21 +00:00
//sys GetPriorityClass(process Handle) (ret uint32, err error) = kernel32.GetPriorityClass
2024-02-18 10:42:21 +00:00
//sys QueryInformationJobObject(job Handle, JobObjectInformationClass int32, JobObjectInformation uintptr, JobObjectInformationLength uint32, retlen *uint32) (err error) = kernel32.QueryInformationJobObject
2024-02-18 10:42:21 +00:00
//sys SetInformationJobObject(job Handle, JobObjectInformationClass uint32, JobObjectInformation uintptr, JobObjectInformationLength uint32) (ret int, err error)
2024-02-18 10:42:21 +00:00
//sys GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupID uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys GetProcessId(process Handle) (id uint32, err error)
2024-02-18 10:42:21 +00:00
//sys QueryFullProcessImageName(proc Handle, flags uint32, exeName *uint16, size *uint32) (err error) = kernel32.QueryFullProcessImageNameW
2024-02-18 10:42:21 +00:00
//sys OpenThread(desiredAccess uint32, inheritHandle bool, threadId uint32) (handle Handle, err error)
2024-02-18 10:42:21 +00:00
//sys SetProcessPriorityBoost(process Handle, disable bool) (err error) = kernel32.SetProcessPriorityBoost
2024-02-18 10:42:21 +00:00
//sys GetProcessWorkingSetSizeEx(hProcess Handle, lpMinimumWorkingSetSize *uintptr, lpMaximumWorkingSetSize *uintptr, flags *uint32)
2024-02-18 10:42:21 +00:00
//sys SetProcessWorkingSetSizeEx(hProcess Handle, dwMinimumWorkingSetSize uintptr, dwMaximumWorkingSetSize uintptr, flags uint32) (err error)
2024-05-14 13:07:09 +00:00
//sys ClearCommBreak(handle Handle) (err error)
2024-05-14 13:07:09 +00:00
//sys ClearCommError(handle Handle, lpErrors *uint32, lpStat *ComStat) (err error)
2024-05-14 13:07:09 +00:00
//sys EscapeCommFunction(handle Handle, dwFunc uint32) (err error)
2024-05-14 13:07:09 +00:00
//sys GetCommState(handle Handle, lpDCB *DCB) (err error)
2024-05-14 13:07:09 +00:00
//sys GetCommModemStatus(handle Handle, lpModemStat *uint32) (err error)
2024-02-18 10:42:21 +00:00
//sys GetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
2024-05-14 13:07:09 +00:00
//sys PurgeComm(handle Handle, dwFlags uint32) (err error)
2024-05-14 13:07:09 +00:00
//sys SetCommBreak(handle Handle) (err error)
2024-05-14 13:07:09 +00:00
//sys SetCommMask(handle Handle, dwEvtMask uint32) (err error)
2024-05-14 13:07:09 +00:00
//sys SetCommState(handle Handle, lpDCB *DCB) (err error)
2024-02-18 10:42:21 +00:00
//sys SetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
2024-05-14 13:07:09 +00:00
//sys SetupComm(handle Handle, dwInQueue uint32, dwOutQueue uint32) (err error)
2024-05-14 13:07:09 +00:00
//sys WaitCommEvent(handle Handle, lpEvtMask *uint32, lpOverlapped *Overlapped) (err error)
2024-02-18 10:42:21 +00:00
//sys GetActiveProcessorCount(groupNumber uint16) (ret uint32)
2024-02-18 10:42:21 +00:00
//sys GetMaximumProcessorCount(groupNumber uint16) (ret uint32)
2024-02-18 10:42:21 +00:00
//sys EnumWindows(enumFunc uintptr, param unsafe.Pointer) (err error) = user32.EnumWindows
2024-02-18 10:42:21 +00:00
//sys EnumChildWindows(hwnd HWND, enumFunc uintptr, param unsafe.Pointer) = user32.EnumChildWindows
2024-02-18 10:42:21 +00:00
//sys GetClassName(hwnd HWND, className *uint16, maxCount int32) (copied int32, err error) = user32.GetClassNameW
2024-02-18 10:42:21 +00:00
//sys GetDesktopWindow() (hwnd HWND) = user32.GetDesktopWindow
2024-02-18 10:42:21 +00:00
//sys GetForegroundWindow() (hwnd HWND) = user32.GetForegroundWindow
2024-02-18 10:42:21 +00:00
//sys IsWindow(hwnd HWND) (isWindow bool) = user32.IsWindow
2024-02-18 10:42:21 +00:00
//sys IsWindowUnicode(hwnd HWND) (isUnicode bool) = user32.IsWindowUnicode
2024-02-18 10:42:21 +00:00
//sys IsWindowVisible(hwnd HWND) (isVisible bool) = user32.IsWindowVisible
2024-02-18 10:42:21 +00:00
//sys GetGUIThreadInfo(thread uint32, info *GUIThreadInfo) (err error) = user32.GetGUIThreadInfo
2024-02-18 10:42:21 +00:00
//sys GetLargePageMinimum() (size uintptr)
// Volume Management Functions
2024-02-18 10:42:21 +00:00
//sys DefineDosDevice(flags uint32, deviceName *uint16, targetPath *uint16) (err error) = DefineDosDeviceW
2024-02-18 10:42:21 +00:00
//sys DeleteVolumeMountPoint(volumeMountPoint *uint16) (err error) = DeleteVolumeMountPointW
2024-02-18 10:42:21 +00:00
//sys FindFirstVolume(volumeName *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeW
2024-02-18 10:42:21 +00:00
//sys FindFirstVolumeMountPoint(rootPathName *uint16, volumeMountPoint *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeMountPointW
2024-02-18 10:42:21 +00:00
//sys FindNextVolume(findVolume Handle, volumeName *uint16, bufferLength uint32) (err error) = FindNextVolumeW
2024-02-18 10:42:21 +00:00
//sys FindNextVolumeMountPoint(findVolumeMountPoint Handle, volumeMountPoint *uint16, bufferLength uint32) (err error) = FindNextVolumeMountPointW
2024-02-18 10:42:21 +00:00
//sys FindVolumeClose(findVolume Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys FindVolumeMountPointClose(findVolumeMountPoint Handle) (err error)
2024-02-18 10:42:21 +00:00
//sys GetDiskFreeSpaceEx(directoryName *uint16, freeBytesAvailableToCaller *uint64, totalNumberOfBytes *uint64, totalNumberOfFreeBytes *uint64) (err error) = GetDiskFreeSpaceExW
2024-02-18 10:42:21 +00:00
//sys GetDriveType(rootPathName *uint16) (driveType uint32) = GetDriveTypeW
2024-02-18 10:42:21 +00:00
//sys GetLogicalDrives() (drivesBitMask uint32, err error) [failretval==0]
2024-02-18 10:42:21 +00:00
//sys GetLogicalDriveStrings(bufferLength uint32, buffer *uint16) (n uint32, err error) [failretval==0] = GetLogicalDriveStringsW
2024-02-18 10:42:21 +00:00
//sys GetVolumeInformation(rootPathName *uint16, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationW
2024-02-18 10:42:21 +00:00
//sys GetVolumeInformationByHandle(file Handle, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationByHandleW
2024-02-18 10:42:21 +00:00
//sys GetVolumeNameForVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16, bufferlength uint32) (err error) = GetVolumeNameForVolumeMountPointW
2024-02-18 10:42:21 +00:00
//sys GetVolumePathName(fileName *uint16, volumePathName *uint16, bufferLength uint32) (err error) = GetVolumePathNameW
2024-02-18 10:42:21 +00:00
//sys GetVolumePathNamesForVolumeName(volumeName *uint16, volumePathNames *uint16, bufferLength uint32, returnLength *uint32) (err error) = GetVolumePathNamesForVolumeNameW
2024-02-18 10:42:21 +00:00
//sys QueryDosDevice(deviceName *uint16, targetPath *uint16, max uint32) (n uint32, err error) [failretval==0] = QueryDosDeviceW
2024-02-18 10:42:21 +00:00
//sys SetVolumeLabel(rootPathName *uint16, volumeName *uint16) (err error) = SetVolumeLabelW
2024-02-18 10:42:21 +00:00
//sys SetVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16) (err error) = SetVolumeMountPointW
2024-02-18 10:42:21 +00:00
//sys InitiateSystemShutdownEx(machineName *uint16, message *uint16, timeout uint32, forceAppsClosed bool, rebootAfterShutdown bool, reason uint32) (err error) = advapi32.InitiateSystemShutdownExW
2024-02-18 10:42:21 +00:00
//sys SetProcessShutdownParameters(level uint32, flags uint32) (err error) = kernel32.SetProcessShutdownParameters
2024-02-18 10:42:21 +00:00
//sys GetProcessShutdownParameters(level *uint32, flags *uint32) (err error) = kernel32.GetProcessShutdownParameters
2024-02-18 10:42:21 +00:00
//sys clsidFromString(lpsz *uint16, pclsid *GUID) (ret error) = ole32.CLSIDFromString
2024-02-18 10:42:21 +00:00
//sys stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int32) (chars int32) = ole32.StringFromGUID2
2024-02-18 10:42:21 +00:00
//sys coCreateGuid(pguid *GUID) (ret error) = ole32.CoCreateGuid
2024-02-18 10:42:21 +00:00
//sys CoTaskMemFree(address unsafe.Pointer) = ole32.CoTaskMemFree
2024-02-18 10:42:21 +00:00
//sys CoInitializeEx(reserved uintptr, coInit uint32) (ret error) = ole32.CoInitializeEx
2024-02-18 10:42:21 +00:00
//sys CoUninitialize() = ole32.CoUninitialize
2024-02-18 10:42:21 +00:00
//sys CoGetObject(name *uint16, bindOpts *BIND_OPTS3, guid *GUID, functionTable **uintptr) (ret error) = ole32.CoGetObject
2024-02-18 10:42:21 +00:00
//sys getProcessPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetProcessPreferredUILanguages
2024-02-18 10:42:21 +00:00
//sys getThreadPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetThreadPreferredUILanguages
2024-02-18 10:42:21 +00:00
//sys getUserPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetUserPreferredUILanguages
2024-02-18 10:42:21 +00:00
//sys getSystemPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetSystemPreferredUILanguages
2024-02-18 10:42:21 +00:00
//sys findResource(module Handle, name uintptr, resType uintptr) (resInfo Handle, err error) = kernel32.FindResourceW
2024-02-18 10:42:21 +00:00
//sys SizeofResource(module Handle, resInfo Handle) (size uint32, err error) = kernel32.SizeofResource
2024-02-18 10:42:21 +00:00
//sys LoadResource(module Handle, resInfo Handle) (resData Handle, err error) = kernel32.LoadResource
2024-02-18 10:42:21 +00:00
//sys LockResource(resData Handle) (addr uintptr, err error) = kernel32.LockResource
// Version APIs
2024-02-18 10:42:21 +00:00
//sys GetFileVersionInfoSize(filename string, zeroHandle *Handle) (bufSize uint32, err error) = version.GetFileVersionInfoSizeW
2024-02-18 10:42:21 +00:00
//sys GetFileVersionInfo(filename string, handle uint32, bufSize uint32, buffer unsafe.Pointer) (err error) = version.GetFileVersionInfoW
2024-02-18 10:42:21 +00:00
//sys VerQueryValue(block unsafe.Pointer, subBlock string, pointerToBufferPointer unsafe.Pointer, bufSize *uint32) (err error) = version.VerQueryValueW
// Process Status API (PSAPI)
2024-02-18 10:42:21 +00:00
//sys enumProcesses(processIds *uint32, nSize uint32, bytesReturned *uint32) (err error) = psapi.EnumProcesses
2024-02-18 10:42:21 +00:00
//sys EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) = psapi.EnumProcessModules
2024-02-18 10:42:21 +00:00
//sys EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) = psapi.EnumProcessModulesEx
2024-02-18 10:42:21 +00:00
//sys GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) = psapi.GetModuleInformation
2024-02-18 10:42:21 +00:00
//sys GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) = psapi.GetModuleFileNameExW
2024-02-18 10:42:21 +00:00
//sys GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) = psapi.GetModuleBaseNameW
2024-02-18 10:42:21 +00:00
//sys QueryWorkingSetEx(process Handle, pv uintptr, cb uint32) (err error) = psapi.QueryWorkingSetEx
// NT Native APIs
2024-02-18 10:42:21 +00:00
//sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb
2024-02-18 10:42:21 +00:00
//sys rtlGetVersion(info *OsVersionInfoEx) (ntstatus error) = ntdll.RtlGetVersion
2024-02-18 10:42:21 +00:00
//sys rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers
2024-02-18 10:42:21 +00:00
//sys RtlGetCurrentPeb() (peb *PEB) = ntdll.RtlGetCurrentPeb
2024-02-18 10:42:21 +00:00
//sys RtlInitUnicodeString(destinationString *NTUnicodeString, sourceString *uint16) = ntdll.RtlInitUnicodeString
2024-02-18 10:42:21 +00:00
//sys RtlInitString(destinationString *NTString, sourceString *byte) = ntdll.RtlInitString
2024-02-18 10:42:21 +00:00
//sys NtCreateFile(handle *Handle, access uint32, oa *OBJECT_ATTRIBUTES, iosb *IO_STATUS_BLOCK, allocationSize *int64, attributes uint32, share uint32, disposition uint32, options uint32, eabuffer uintptr, ealength uint32) (ntstatus error) = ntdll.NtCreateFile
2024-02-18 10:42:21 +00:00
//sys NtCreateNamedPipeFile(pipe *Handle, access uint32, oa *OBJECT_ATTRIBUTES, iosb *IO_STATUS_BLOCK, share uint32, disposition uint32, options uint32, typ uint32, readMode uint32, completionMode uint32, maxInstances uint32, inboundQuota uint32, outputQuota uint32, timeout *int64) (ntstatus error) = ntdll.NtCreateNamedPipeFile
2024-02-18 10:42:21 +00:00
//sys NtSetInformationFile(handle Handle, iosb *IO_STATUS_BLOCK, inBuffer *byte, inBufferLen uint32, class uint32) (ntstatus error) = ntdll.NtSetInformationFile
2024-02-18 10:42:21 +00:00
//sys RtlDosPathNameToNtPathName(dosName *uint16, ntName *NTUnicodeString, ntFileNamePart *uint16, relativeName *RTL_RELATIVE_NAME) (ntstatus error) = ntdll.RtlDosPathNameToNtPathName_U_WithStatus
2024-02-18 10:42:21 +00:00
//sys RtlDosPathNameToRelativeNtPathName(dosName *uint16, ntName *NTUnicodeString, ntFileNamePart *uint16, relativeName *RTL_RELATIVE_NAME) (ntstatus error) = ntdll.RtlDosPathNameToRelativeNtPathName_U_WithStatus
2024-02-18 10:42:21 +00:00
//sys RtlDefaultNpAcl(acl **ACL) (ntstatus error) = ntdll.RtlDefaultNpAcl
2024-02-18 10:42:21 +00:00
//sys NtQueryInformationProcess(proc Handle, procInfoClass int32, procInfo unsafe.Pointer, procInfoLen uint32, retLen *uint32) (ntstatus error) = ntdll.NtQueryInformationProcess
2024-02-18 10:42:21 +00:00
//sys NtSetInformationProcess(proc Handle, procInfoClass int32, procInfo unsafe.Pointer, procInfoLen uint32) (ntstatus error) = ntdll.NtSetInformationProcess
2024-02-18 10:42:21 +00:00
//sys NtQuerySystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32, retLen *uint32) (ntstatus error) = ntdll.NtQuerySystemInformation
2024-02-18 10:42:21 +00:00
//sys NtSetSystemInformation(sysInfoClass int32, sysInfo unsafe.Pointer, sysInfoLen uint32) (ntstatus error) = ntdll.NtSetSystemInformation
2024-02-18 10:42:21 +00:00
//sys RtlAddFunctionTable(functionTable *RUNTIME_FUNCTION, entryCount uint32, baseAddress uintptr) (ret bool) = ntdll.RtlAddFunctionTable
2024-02-18 10:42:21 +00:00
//sys RtlDeleteFunctionTable(functionTable *RUNTIME_FUNCTION) (ret bool) = ntdll.RtlDeleteFunctionTable
// Desktop Window Manager API (Dwmapi)
2024-02-18 10:42:21 +00:00
//sys DwmGetWindowAttribute(hwnd HWND, attribute uint32, value unsafe.Pointer, size uint32) (ret error) = dwmapi.DwmGetWindowAttribute
2024-02-18 10:42:21 +00:00
//sys DwmSetWindowAttribute(hwnd HWND, attribute uint32, value unsafe.Pointer, size uint32) (ret error) = dwmapi.DwmSetWindowAttribute
// Windows Multimedia API
2024-02-18 10:42:21 +00:00
//sys TimeBeginPeriod (period uint32) (err error) [failretval != 0] = winmm.timeBeginPeriod
2024-02-18 10:42:21 +00:00
//sys TimeEndPeriod (period uint32) (err error) [failretval != 0] = winmm.timeEndPeriod
// syscall interface implementation for other packages
// GetCurrentProcess returns the handle for the current process.
2024-02-18 10:42:21 +00:00
// It is a pseudo handle that does not need to be closed.
2024-02-18 10:42:21 +00:00
// The returned error is always nil.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: use CurrentProcess for the same Handle without the nil
2024-02-18 10:42:21 +00:00
// error.
2024-02-18 10:42:21 +00:00
func GetCurrentProcess() (Handle, error) {
2024-02-18 10:42:21 +00:00
return CurrentProcess(), nil
2024-02-18 10:42:21 +00:00
}
// CurrentProcess returns the handle for the current process.
2024-02-18 10:42:21 +00:00
// It is a pseudo handle that does not need to be closed.
2024-02-18 10:42:21 +00:00
func CurrentProcess() Handle { return Handle(^uintptr(1 - 1)) }
// GetCurrentThread returns the handle for the current thread.
2024-02-18 10:42:21 +00:00
// It is a pseudo handle that does not need to be closed.
2024-02-18 10:42:21 +00:00
// The returned error is always nil.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: use CurrentThread for the same Handle without the nil
2024-02-18 10:42:21 +00:00
// error.
2024-02-18 10:42:21 +00:00
func GetCurrentThread() (Handle, error) {
2024-02-18 10:42:21 +00:00
return CurrentThread(), nil
2024-02-18 10:42:21 +00:00
}
// CurrentThread returns the handle for the current thread.
2024-02-18 10:42:21 +00:00
// It is a pseudo handle that does not need to be closed.
2024-02-18 10:42:21 +00:00
func CurrentThread() Handle { return Handle(^uintptr(2 - 1)) }
// GetProcAddressByOrdinal retrieves the address of the exported
2024-02-18 10:42:21 +00:00
// function from module by ordinal.
2024-02-18 10:42:21 +00:00
func GetProcAddressByOrdinal(module Handle, ordinal uintptr) (proc uintptr, err error) {
2024-02-18 10:42:21 +00:00
r0, _, e1 := syscall.Syscall(procGetProcAddress.Addr(), 2, uintptr(module), ordinal, 0)
2024-02-18 10:42:21 +00:00
proc = uintptr(r0)
2024-02-18 10:42:21 +00:00
if proc == 0 {
2024-02-18 10:42:21 +00:00
err = errnoErr(e1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func Exit(code int) { ExitProcess(uint32(code)) }
func makeInheritSa() *SecurityAttributes {
2024-02-18 10:42:21 +00:00
var sa SecurityAttributes
2024-02-18 10:42:21 +00:00
sa.Length = uint32(unsafe.Sizeof(sa))
2024-02-18 10:42:21 +00:00
sa.InheritHandle = 1
2024-02-18 10:42:21 +00:00
return &sa
2024-02-18 10:42:21 +00:00
}
func Open(path string, mode int, perm uint32) (fd Handle, err error) {
2024-02-18 10:42:21 +00:00
if len(path) == 0 {
2024-02-18 10:42:21 +00:00
return InvalidHandle, ERROR_FILE_NOT_FOUND
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pathp, err := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return InvalidHandle, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var access uint32
2024-02-18 10:42:21 +00:00
switch mode & (O_RDONLY | O_WRONLY | O_RDWR) {
2024-02-18 10:42:21 +00:00
case O_RDONLY:
2024-02-18 10:42:21 +00:00
access = GENERIC_READ
2024-02-18 10:42:21 +00:00
case O_WRONLY:
2024-02-18 10:42:21 +00:00
access = GENERIC_WRITE
2024-02-18 10:42:21 +00:00
case O_RDWR:
2024-02-18 10:42:21 +00:00
access = GENERIC_READ | GENERIC_WRITE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if mode&O_CREAT != 0 {
2024-02-18 10:42:21 +00:00
access |= GENERIC_WRITE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if mode&O_APPEND != 0 {
2024-02-18 10:42:21 +00:00
access &^= GENERIC_WRITE
2024-02-18 10:42:21 +00:00
access |= FILE_APPEND_DATA
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE)
2024-02-18 10:42:21 +00:00
var sa *SecurityAttributes
2024-02-18 10:42:21 +00:00
if mode&O_CLOEXEC == 0 {
2024-02-18 10:42:21 +00:00
sa = makeInheritSa()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var createmode uint32
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case mode&(O_CREAT|O_EXCL) == (O_CREAT | O_EXCL):
2024-02-18 10:42:21 +00:00
createmode = CREATE_NEW
2024-02-18 10:42:21 +00:00
case mode&(O_CREAT|O_TRUNC) == (O_CREAT | O_TRUNC):
2024-02-18 10:42:21 +00:00
createmode = CREATE_ALWAYS
2024-02-18 10:42:21 +00:00
case mode&O_CREAT == O_CREAT:
2024-02-18 10:42:21 +00:00
createmode = OPEN_ALWAYS
2024-02-18 10:42:21 +00:00
case mode&O_TRUNC == O_TRUNC:
2024-02-18 10:42:21 +00:00
createmode = TRUNCATE_EXISTING
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
createmode = OPEN_EXISTING
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var attrs uint32 = FILE_ATTRIBUTE_NORMAL
2024-02-18 10:42:21 +00:00
if perm&S_IWRITE == 0 {
2024-02-18 10:42:21 +00:00
attrs = FILE_ATTRIBUTE_READONLY
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
h, e := CreateFile(pathp, access, sharemode, sa, createmode, attrs, 0)
2024-02-18 10:42:21 +00:00
return h, e
2024-02-18 10:42:21 +00:00
}
func Read(fd Handle, p []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
var done uint32
2024-02-18 10:42:21 +00:00
e := ReadFile(fd, p, &done, nil)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
if e == ERROR_BROKEN_PIPE {
2024-02-18 10:42:21 +00:00
// NOTE(brainman): work around ERROR_BROKEN_PIPE is returned on reading EOF from stdin
2024-02-18 10:42:21 +00:00
return 0, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return int(done), nil
2024-02-18 10:42:21 +00:00
}
func Write(fd Handle, p []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
raceReleaseMerge(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var done uint32
2024-02-18 10:42:21 +00:00
e := WriteFile(fd, p, &done, nil)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return int(done), nil
2024-02-18 10:42:21 +00:00
}
func ReadFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error {
2024-02-18 10:42:21 +00:00
err := readFile(fd, p, done, overlapped)
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
if *done > 0 {
2024-02-18 10:42:21 +00:00
raceWriteRange(unsafe.Pointer(&p[0]), int(*done))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
raceAcquire(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func WriteFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error {
2024-02-18 10:42:21 +00:00
if raceenabled {
2024-02-18 10:42:21 +00:00
raceReleaseMerge(unsafe.Pointer(&ioSync))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err := writeFile(fd, p, done, overlapped)
2024-02-18 10:42:21 +00:00
if raceenabled && *done > 0 {
2024-02-18 10:42:21 +00:00
raceReadRange(unsafe.Pointer(&p[0]), int(*done))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
var ioSync int64
func Seek(fd Handle, offset int64, whence int) (newoffset int64, err error) {
2024-02-18 10:42:21 +00:00
var w uint32
2024-02-18 10:42:21 +00:00
switch whence {
2024-02-18 10:42:21 +00:00
case 0:
2024-02-18 10:42:21 +00:00
w = FILE_BEGIN
2024-02-18 10:42:21 +00:00
case 1:
2024-02-18 10:42:21 +00:00
w = FILE_CURRENT
2024-02-18 10:42:21 +00:00
case 2:
2024-02-18 10:42:21 +00:00
w = FILE_END
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hi := int32(offset >> 32)
2024-02-18 10:42:21 +00:00
lo := int32(offset)
2024-02-18 10:42:21 +00:00
// use GetFileType to check pipe, pipe can't do seek
2024-02-18 10:42:21 +00:00
ft, _ := GetFileType(fd)
2024-02-18 10:42:21 +00:00
if ft == FILE_TYPE_PIPE {
2024-02-18 10:42:21 +00:00
return 0, syscall.EPIPE
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
rlo, e := SetFilePointer(fd, lo, &hi, w)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return int64(hi)<<32 + int64(rlo), nil
2024-02-18 10:42:21 +00:00
}
func Close(fd Handle) (err error) {
2024-02-18 10:42:21 +00:00
return CloseHandle(fd)
2024-02-18 10:42:21 +00:00
}
var (
Stdin = getStdHandle(STD_INPUT_HANDLE)
2024-02-18 10:42:21 +00:00
Stdout = getStdHandle(STD_OUTPUT_HANDLE)
2024-02-18 10:42:21 +00:00
Stderr = getStdHandle(STD_ERROR_HANDLE)
)
func getStdHandle(stdhandle uint32) (fd Handle) {
2024-02-18 10:42:21 +00:00
r, _ := GetStdHandle(stdhandle)
2024-02-18 10:42:21 +00:00
return r
2024-02-18 10:42:21 +00:00
}
const ImplementsGetwd = true
func Getwd() (wd string, err error) {
2024-02-18 10:42:21 +00:00
b := make([]uint16, 300)
2024-02-18 10:42:21 +00:00
n, e := GetCurrentDirectory(uint32(len(b)), &b[0])
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(utf16.Decode(b[0:n])), nil
2024-02-18 10:42:21 +00:00
}
func Chdir(path string) (err error) {
2024-02-18 10:42:21 +00:00
pathp, err := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return SetCurrentDirectory(pathp)
2024-02-18 10:42:21 +00:00
}
func Mkdir(path string, mode uint32) (err error) {
2024-02-18 10:42:21 +00:00
pathp, err := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return CreateDirectory(pathp, nil)
2024-02-18 10:42:21 +00:00
}
func Rmdir(path string) (err error) {
2024-02-18 10:42:21 +00:00
pathp, err := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return RemoveDirectory(pathp)
2024-02-18 10:42:21 +00:00
}
func Unlink(path string) (err error) {
2024-02-18 10:42:21 +00:00
pathp, err := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return DeleteFile(pathp)
2024-02-18 10:42:21 +00:00
}
func Rename(oldpath, newpath string) (err error) {
2024-02-18 10:42:21 +00:00
from, err := UTF16PtrFromString(oldpath)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
to, err := UTF16PtrFromString(newpath)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING)
2024-02-18 10:42:21 +00:00
}
func ComputerName() (name string, err error) {
2024-02-18 10:42:21 +00:00
var n uint32 = MAX_COMPUTERNAME_LENGTH + 1
2024-02-18 10:42:21 +00:00
b := make([]uint16, n)
2024-02-18 10:42:21 +00:00
e := GetComputerName(&b[0], &n)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(utf16.Decode(b[0:n])), nil
2024-02-18 10:42:21 +00:00
}
func DurationSinceBoot() time.Duration {
2024-02-18 10:42:21 +00:00
return time.Duration(getTickCount64()) * time.Millisecond
2024-02-18 10:42:21 +00:00
}
func Ftruncate(fd Handle, length int64) (err error) {
2024-02-18 10:42:21 +00:00
curoffset, e := Seek(fd, 0, 1)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer Seek(fd, curoffset, 0)
2024-02-18 10:42:21 +00:00
_, e = Seek(fd, length, 0)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e = SetEndOfFile(fd)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func Gettimeofday(tv *Timeval) (err error) {
2024-02-18 10:42:21 +00:00
var ft Filetime
2024-02-18 10:42:21 +00:00
GetSystemTimeAsFileTime(&ft)
2024-02-18 10:42:21 +00:00
*tv = NsecToTimeval(ft.Nanoseconds())
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func Pipe(p []Handle) (err error) {
2024-02-18 10:42:21 +00:00
if len(p) != 2 {
2024-02-18 10:42:21 +00:00
return syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var r, w Handle
2024-02-18 10:42:21 +00:00
e := CreatePipe(&r, &w, makeInheritSa(), 0)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p[0] = r
2024-02-18 10:42:21 +00:00
p[1] = w
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func Utimes(path string, tv []Timeval) (err error) {
2024-02-18 10:42:21 +00:00
if len(tv) != 2 {
2024-02-18 10:42:21 +00:00
return syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pathp, e := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
h, e := CreateFile(pathp,
2024-02-18 10:42:21 +00:00
FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
2024-02-18 10:42:21 +00:00
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer CloseHandle(h)
2024-02-18 10:42:21 +00:00
a := NsecToFiletime(tv[0].Nanoseconds())
2024-02-18 10:42:21 +00:00
w := NsecToFiletime(tv[1].Nanoseconds())
2024-02-18 10:42:21 +00:00
return SetFileTime(h, nil, &a, &w)
2024-02-18 10:42:21 +00:00
}
func UtimesNano(path string, ts []Timespec) (err error) {
2024-02-18 10:42:21 +00:00
if len(ts) != 2 {
2024-02-18 10:42:21 +00:00
return syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
pathp, e := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
h, e := CreateFile(pathp,
2024-02-18 10:42:21 +00:00
FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
2024-02-18 10:42:21 +00:00
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer CloseHandle(h)
2024-02-18 10:42:21 +00:00
a := NsecToFiletime(TimespecToNsec(ts[0]))
2024-02-18 10:42:21 +00:00
w := NsecToFiletime(TimespecToNsec(ts[1]))
2024-02-18 10:42:21 +00:00
return SetFileTime(h, nil, &a, &w)
2024-02-18 10:42:21 +00:00
}
func Fsync(fd Handle) (err error) {
2024-02-18 10:42:21 +00:00
return FlushFileBuffers(fd)
2024-02-18 10:42:21 +00:00
}
func Chmod(path string, mode uint32) (err error) {
2024-02-18 10:42:21 +00:00
p, e := UTF16PtrFromString(path)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
attrs, e := GetFileAttributes(p)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if mode&S_IWRITE != 0 {
2024-02-18 10:42:21 +00:00
attrs &^= FILE_ATTRIBUTE_READONLY
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
attrs |= FILE_ATTRIBUTE_READONLY
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return SetFileAttributes(p, attrs)
2024-02-18 10:42:21 +00:00
}
func LoadGetSystemTimePreciseAsFileTime() error {
2024-02-18 10:42:21 +00:00
return procGetSystemTimePreciseAsFileTime.Find()
2024-02-18 10:42:21 +00:00
}
func LoadCancelIoEx() error {
2024-02-18 10:42:21 +00:00
return procCancelIoEx.Find()
2024-02-18 10:42:21 +00:00
}
func LoadSetFileCompletionNotificationModes() error {
2024-02-18 10:42:21 +00:00
return procSetFileCompletionNotificationModes.Find()
2024-02-18 10:42:21 +00:00
}
func WaitForMultipleObjects(handles []Handle, waitAll bool, waitMilliseconds uint32) (event uint32, err error) {
2024-02-18 10:42:21 +00:00
// Every other win32 array API takes arguments as "pointer, count", except for this function. So we
2024-02-18 10:42:21 +00:00
// can't declare it as a usual [] type, because mksyscall will use the opposite order. We therefore
2024-02-18 10:42:21 +00:00
// trivially stub this ourselves.
var handlePtr *Handle
2024-02-18 10:42:21 +00:00
if len(handles) > 0 {
2024-02-18 10:42:21 +00:00
handlePtr = &handles[0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return waitForMultipleObjects(uint32(len(handles)), uintptr(unsafe.Pointer(handlePtr)), waitAll, waitMilliseconds)
2024-02-18 10:42:21 +00:00
}
// net api calls
const socket_error = uintptr(^uint32(0))
//sys WSAStartup(verreq uint32, data *WSAData) (sockerr error) = ws2_32.WSAStartup
2024-02-18 10:42:21 +00:00
//sys WSACleanup() (err error) [failretval==socket_error] = ws2_32.WSACleanup
2024-02-18 10:42:21 +00:00
//sys WSAIoctl(s Handle, iocc uint32, inbuf *byte, cbif uint32, outbuf *byte, cbob uint32, cbbr *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) [failretval==socket_error] = ws2_32.WSAIoctl
2024-02-18 10:42:21 +00:00
//sys WSALookupServiceBegin(querySet *WSAQUERYSET, flags uint32, handle *Handle) (err error) [failretval==socket_error] = ws2_32.WSALookupServiceBeginW
2024-02-18 10:42:21 +00:00
//sys WSALookupServiceNext(handle Handle, flags uint32, size *int32, querySet *WSAQUERYSET) (err error) [failretval==socket_error] = ws2_32.WSALookupServiceNextW
2024-02-18 10:42:21 +00:00
//sys WSALookupServiceEnd(handle Handle) (err error) [failretval==socket_error] = ws2_32.WSALookupServiceEnd
2024-02-18 10:42:21 +00:00
//sys socket(af int32, typ int32, protocol int32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.socket
2024-02-18 10:42:21 +00:00
//sys sendto(s Handle, buf []byte, flags int32, to unsafe.Pointer, tolen int32) (err error) [failretval==socket_error] = ws2_32.sendto
2024-02-18 10:42:21 +00:00
//sys recvfrom(s Handle, buf []byte, flags int32, from *RawSockaddrAny, fromlen *int32) (n int32, err error) [failretval==-1] = ws2_32.recvfrom
2024-02-18 10:42:21 +00:00
//sys Setsockopt(s Handle, level int32, optname int32, optval *byte, optlen int32) (err error) [failretval==socket_error] = ws2_32.setsockopt
2024-02-18 10:42:21 +00:00
//sys Getsockopt(s Handle, level int32, optname int32, optval *byte, optlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockopt
2024-02-18 10:42:21 +00:00
//sys bind(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.bind
2024-02-18 10:42:21 +00:00
//sys connect(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.connect
2024-02-18 10:42:21 +00:00
//sys getsockname(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockname
2024-02-18 10:42:21 +00:00
//sys getpeername(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getpeername
2024-02-18 10:42:21 +00:00
//sys listen(s Handle, backlog int32) (err error) [failretval==socket_error] = ws2_32.listen
2024-02-18 10:42:21 +00:00
//sys shutdown(s Handle, how int32) (err error) [failretval==socket_error] = ws2_32.shutdown
2024-02-18 10:42:21 +00:00
//sys Closesocket(s Handle) (err error) [failretval==socket_error] = ws2_32.closesocket
2024-02-18 10:42:21 +00:00
//sys AcceptEx(ls Handle, as Handle, buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, recvd *uint32, overlapped *Overlapped) (err error) = mswsock.AcceptEx
2024-02-18 10:42:21 +00:00
//sys GetAcceptExSockaddrs(buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, lrsa **RawSockaddrAny, lrsalen *int32, rrsa **RawSockaddrAny, rrsalen *int32) = mswsock.GetAcceptExSockaddrs
2024-02-18 10:42:21 +00:00
//sys WSARecv(s Handle, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSARecv
2024-02-18 10:42:21 +00:00
//sys WSASend(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSASend
2024-02-18 10:42:21 +00:00
//sys WSARecvFrom(s Handle, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, from *RawSockaddrAny, fromlen *int32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSARecvFrom
2024-02-18 10:42:21 +00:00
//sys WSASendTo(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to *RawSockaddrAny, tolen int32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSASendTo
2024-02-18 10:42:21 +00:00
//sys WSASocket(af int32, typ int32, protocol int32, protoInfo *WSAProtocolInfo, group uint32, flags uint32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.WSASocketW
2024-02-18 10:42:21 +00:00
//sys GetHostByName(name string) (h *Hostent, err error) [failretval==nil] = ws2_32.gethostbyname
2024-02-18 10:42:21 +00:00
//sys GetServByName(name string, proto string) (s *Servent, err error) [failretval==nil] = ws2_32.getservbyname
2024-02-18 10:42:21 +00:00
//sys Ntohs(netshort uint16) (u uint16) = ws2_32.ntohs
2024-02-18 10:42:21 +00:00
//sys GetProtoByName(name string) (p *Protoent, err error) [failretval==nil] = ws2_32.getprotobyname
2024-02-18 10:42:21 +00:00
//sys DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status error) = dnsapi.DnsQuery_W
2024-02-18 10:42:21 +00:00
//sys DnsRecordListFree(rl *DNSRecord, freetype uint32) = dnsapi.DnsRecordListFree
2024-02-18 10:42:21 +00:00
//sys DnsNameCompare(name1 *uint16, name2 *uint16) (same bool) = dnsapi.DnsNameCompare_W
2024-02-18 10:42:21 +00:00
//sys GetAddrInfoW(nodename *uint16, servicename *uint16, hints *AddrinfoW, result **AddrinfoW) (sockerr error) = ws2_32.GetAddrInfoW
2024-02-18 10:42:21 +00:00
//sys FreeAddrInfoW(addrinfo *AddrinfoW) = ws2_32.FreeAddrInfoW
2024-02-18 10:42:21 +00:00
//sys GetIfEntry(pIfRow *MibIfRow) (errcode error) = iphlpapi.GetIfEntry
2024-02-18 10:42:21 +00:00
//sys GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode error) = iphlpapi.GetAdaptersInfo
2024-02-18 10:42:21 +00:00
//sys SetFileCompletionNotificationModes(handle Handle, flags uint8) (err error) = kernel32.SetFileCompletionNotificationModes
2024-02-18 10:42:21 +00:00
//sys WSAEnumProtocols(protocols *int32, protocolBuffer *WSAProtocolInfo, bufferLength *uint32) (n int32, err error) [failretval==-1] = ws2_32.WSAEnumProtocolsW
2024-02-18 10:42:21 +00:00
//sys WSAGetOverlappedResult(h Handle, o *Overlapped, bytes *uint32, wait bool, flags *uint32) (err error) = ws2_32.WSAGetOverlappedResult
2024-02-18 10:42:21 +00:00
//sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
2024-02-18 10:42:21 +00:00
//sys GetACP() (acp uint32) = kernel32.GetACP
2024-02-18 10:42:21 +00:00
//sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
2024-02-18 10:42:21 +00:00
//sys getBestInterfaceEx(sockaddr unsafe.Pointer, pdwBestIfIndex *uint32) (errcode error) = iphlpapi.GetBestInterfaceEx
// For testing: clients can set this flag to force
2024-02-18 10:42:21 +00:00
// creation of IPv6 sockets to return EAFNOSUPPORT.
2024-02-18 10:42:21 +00:00
var SocketDisableIPv6 bool
type RawSockaddrInet4 struct {
Family uint16
Port uint16
Addr [4]byte /* in_addr */
Zero [8]uint8
2024-02-18 10:42:21 +00:00
}
type RawSockaddrInet6 struct {
Family uint16
Port uint16
2024-02-18 10:42:21 +00:00
Flowinfo uint32
Addr [16]byte /* in6_addr */
2024-02-18 10:42:21 +00:00
Scope_id uint32
}
type RawSockaddr struct {
Family uint16
Data [14]int8
2024-02-18 10:42:21 +00:00
}
type RawSockaddrAny struct {
Addr RawSockaddr
Pad [100]int8
2024-02-18 10:42:21 +00:00
}
type Sockaddr interface {
sockaddr() (ptr unsafe.Pointer, len int32, err error) // lowercase; only we can define Sockaddrs
2024-02-18 10:42:21 +00:00
}
type SockaddrInet4 struct {
Port int
2024-02-18 10:42:21 +00:00
Addr [4]byte
raw RawSockaddrInet4
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, int32, error) {
2024-02-18 10:42:21 +00:00
if sa.Port < 0 || sa.Port > 0xFFFF {
2024-02-18 10:42:21 +00:00
return nil, 0, syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_INET
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
2024-02-18 10:42:21 +00:00
p[0] = byte(sa.Port >> 8)
2024-02-18 10:42:21 +00:00
p[1] = byte(sa.Port)
2024-02-18 10:42:21 +00:00
sa.raw.Addr = sa.Addr
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil
2024-02-18 10:42:21 +00:00
}
type SockaddrInet6 struct {
Port int
2024-02-18 10:42:21 +00:00
ZoneId uint32
Addr [16]byte
raw RawSockaddrInet6
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, int32, error) {
2024-02-18 10:42:21 +00:00
if sa.Port < 0 || sa.Port > 0xFFFF {
2024-02-18 10:42:21 +00:00
return nil, 0, syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_INET6
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
2024-02-18 10:42:21 +00:00
p[0] = byte(sa.Port >> 8)
2024-02-18 10:42:21 +00:00
p[1] = byte(sa.Port)
2024-02-18 10:42:21 +00:00
sa.raw.Scope_id = sa.ZoneId
2024-02-18 10:42:21 +00:00
sa.raw.Addr = sa.Addr
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil
2024-02-18 10:42:21 +00:00
}
type RawSockaddrUnix struct {
Family uint16
Path [UNIX_PATH_MAX]int8
2024-02-18 10:42:21 +00:00
}
type SockaddrUnix struct {
Name string
raw RawSockaddrUnix
2024-02-18 10:42:21 +00:00
}
func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) {
2024-02-18 10:42:21 +00:00
name := sa.Name
2024-02-18 10:42:21 +00:00
n := len(name)
2024-02-18 10:42:21 +00:00
if n > len(sa.raw.Path) {
2024-02-18 10:42:21 +00:00
return nil, 0, syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n == len(sa.raw.Path) && name[0] != '@' {
2024-02-18 10:42:21 +00:00
return nil, 0, syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.raw.Family = AF_UNIX
2024-02-18 10:42:21 +00:00
for i := 0; i < n; i++ {
2024-02-18 10:42:21 +00:00
sa.raw.Path[i] = int8(name[i])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// length is family (uint16), name, NUL.
2024-02-18 10:42:21 +00:00
sl := int32(2)
2024-02-18 10:42:21 +00:00
if n > 0 {
2024-02-18 10:42:21 +00:00
sl += int32(n) + 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sa.raw.Path[0] == '@' || (sa.raw.Path[0] == 0 && sl > 3) {
2024-02-18 10:42:21 +00:00
// Check sl > 3 so we don't change unnamed socket behavior.
2024-02-18 10:42:21 +00:00
sa.raw.Path[0] = 0
2024-02-18 10:42:21 +00:00
// Don't count trailing NUL for abstract address.
2024-02-18 10:42:21 +00:00
sl--
2024-02-18 10:42:21 +00:00
}
return unsafe.Pointer(&sa.raw), sl, nil
2024-02-18 10:42:21 +00:00
}
type RawSockaddrBth struct {
AddressFamily [2]byte
BtAddr [8]byte
2024-02-18 10:42:21 +00:00
ServiceClassId [16]byte
Port [4]byte
2024-02-18 10:42:21 +00:00
}
type SockaddrBth struct {
BtAddr uint64
2024-02-18 10:42:21 +00:00
ServiceClassId GUID
Port uint32
2024-02-18 10:42:21 +00:00
raw RawSockaddrBth
}
func (sa *SockaddrBth) sockaddr() (unsafe.Pointer, int32, error) {
2024-02-18 10:42:21 +00:00
family := AF_BTH
2024-02-18 10:42:21 +00:00
sa.raw = RawSockaddrBth{
AddressFamily: *(*[2]byte)(unsafe.Pointer(&family)),
BtAddr: *(*[8]byte)(unsafe.Pointer(&sa.BtAddr)),
Port: *(*[4]byte)(unsafe.Pointer(&sa.Port)),
2024-02-18 10:42:21 +00:00
ServiceClassId: *(*[16]byte)(unsafe.Pointer(&sa.ServiceClassId)),
}
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil
2024-02-18 10:42:21 +00:00
}
func (rsa *RawSockaddrAny) Sockaddr() (Sockaddr, error) {
2024-02-18 10:42:21 +00:00
switch rsa.Addr.Family {
2024-02-18 10:42:21 +00:00
case AF_UNIX:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrUnix)
2024-02-18 10:42:21 +00:00
if pp.Path[0] == 0 {
2024-02-18 10:42:21 +00:00
// "Abstract" Unix domain socket.
2024-02-18 10:42:21 +00:00
// Rewrite leading NUL as @ for textual display.
2024-02-18 10:42:21 +00:00
// (This is the standard convention.)
2024-02-18 10:42:21 +00:00
// Not friendly to overwrite in place,
2024-02-18 10:42:21 +00:00
// but the callers below don't care.
2024-02-18 10:42:21 +00:00
pp.Path[0] = '@'
2024-02-18 10:42:21 +00:00
}
// Assume path ends at NUL.
2024-02-18 10:42:21 +00:00
// This is not technically the Linux semantics for
2024-02-18 10:42:21 +00:00
// abstract Unix domain sockets--they are supposed
2024-02-18 10:42:21 +00:00
// to be uninterpreted fixed-size binary blobs--but
2024-02-18 10:42:21 +00:00
// everyone uses this convention.
2024-02-18 10:42:21 +00:00
n := 0
2024-02-18 10:42:21 +00:00
for n < len(pp.Path) && pp.Path[n] != 0 {
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sa.Name = string(unsafe.Slice((*byte)(unsafe.Pointer(&pp.Path[0])), n))
2024-02-18 10:42:21 +00:00
return sa, nil
case AF_INET:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrInet4)
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&pp.Port))
2024-02-18 10:42:21 +00:00
sa.Port = int(p[0])<<8 + int(p[1])
2024-02-18 10:42:21 +00:00
sa.Addr = pp.Addr
2024-02-18 10:42:21 +00:00
return sa, nil
case AF_INET6:
2024-02-18 10:42:21 +00:00
pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
2024-02-18 10:42:21 +00:00
sa := new(SockaddrInet6)
2024-02-18 10:42:21 +00:00
p := (*[2]byte)(unsafe.Pointer(&pp.Port))
2024-02-18 10:42:21 +00:00
sa.Port = int(p[0])<<8 + int(p[1])
2024-02-18 10:42:21 +00:00
sa.ZoneId = pp.Scope_id
2024-02-18 10:42:21 +00:00
sa.Addr = pp.Addr
2024-02-18 10:42:21 +00:00
return sa, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil, syscall.EAFNOSUPPORT
2024-02-18 10:42:21 +00:00
}
func Socket(domain, typ, proto int) (fd Handle, err error) {
2024-02-18 10:42:21 +00:00
if domain == AF_INET6 && SocketDisableIPv6 {
2024-02-18 10:42:21 +00:00
return InvalidHandle, syscall.EAFNOSUPPORT
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return socket(int32(domain), int32(typ), int32(proto))
2024-02-18 10:42:21 +00:00
}
func SetsockoptInt(fd Handle, level, opt int, value int) (err error) {
2024-02-18 10:42:21 +00:00
v := int32(value)
2024-02-18 10:42:21 +00:00
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&v)), int32(unsafe.Sizeof(v)))
2024-02-18 10:42:21 +00:00
}
func Bind(fd Handle, sa Sockaddr) (err error) {
2024-02-18 10:42:21 +00:00
ptr, n, err := sa.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return bind(fd, ptr, n)
2024-02-18 10:42:21 +00:00
}
func Connect(fd Handle, sa Sockaddr) (err error) {
2024-02-18 10:42:21 +00:00
ptr, n, err := sa.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return connect(fd, ptr, n)
2024-02-18 10:42:21 +00:00
}
func GetBestInterfaceEx(sa Sockaddr, pdwBestIfIndex *uint32) (err error) {
2024-02-18 10:42:21 +00:00
ptr, _, err := sa.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return getBestInterfaceEx(ptr, pdwBestIfIndex)
2024-02-18 10:42:21 +00:00
}
func Getsockname(fd Handle) (sa Sockaddr, err error) {
2024-02-18 10:42:21 +00:00
var rsa RawSockaddrAny
2024-02-18 10:42:21 +00:00
l := int32(unsafe.Sizeof(rsa))
2024-02-18 10:42:21 +00:00
if err = getsockname(fd, &rsa, &l); err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return rsa.Sockaddr()
2024-02-18 10:42:21 +00:00
}
func Getpeername(fd Handle) (sa Sockaddr, err error) {
2024-02-18 10:42:21 +00:00
var rsa RawSockaddrAny
2024-02-18 10:42:21 +00:00
l := int32(unsafe.Sizeof(rsa))
2024-02-18 10:42:21 +00:00
if err = getpeername(fd, &rsa, &l); err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return rsa.Sockaddr()
2024-02-18 10:42:21 +00:00
}
func Listen(s Handle, n int) (err error) {
2024-02-18 10:42:21 +00:00
return listen(s, int32(n))
2024-02-18 10:42:21 +00:00
}
func Shutdown(fd Handle, how int) (err error) {
2024-02-18 10:42:21 +00:00
return shutdown(fd, int32(how))
2024-02-18 10:42:21 +00:00
}
func WSASendto(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to Sockaddr, overlapped *Overlapped, croutine *byte) (err error) {
2024-02-18 10:42:21 +00:00
var rsa unsafe.Pointer
2024-02-18 10:42:21 +00:00
var l int32
2024-02-18 10:42:21 +00:00
if to != nil {
2024-02-18 10:42:21 +00:00
rsa, l, err = to.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return WSASendTo(s, bufs, bufcnt, sent, flags, (*RawSockaddrAny)(unsafe.Pointer(rsa)), l, overlapped, croutine)
2024-02-18 10:42:21 +00:00
}
func LoadGetAddrInfo() error {
2024-02-18 10:42:21 +00:00
return procGetAddrInfoW.Find()
2024-02-18 10:42:21 +00:00
}
var connectExFunc struct {
once sync.Once
2024-02-18 10:42:21 +00:00
addr uintptr
err error
2024-02-18 10:42:21 +00:00
}
func LoadConnectEx() error {
2024-02-18 10:42:21 +00:00
connectExFunc.once.Do(func() {
2024-02-18 10:42:21 +00:00
var s Handle
2024-02-18 10:42:21 +00:00
s, connectExFunc.err = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
2024-02-18 10:42:21 +00:00
if connectExFunc.err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer CloseHandle(s)
2024-02-18 10:42:21 +00:00
var n uint32
2024-02-18 10:42:21 +00:00
connectExFunc.err = WSAIoctl(s,
2024-02-18 10:42:21 +00:00
SIO_GET_EXTENSION_FUNCTION_POINTER,
2024-02-18 10:42:21 +00:00
(*byte)(unsafe.Pointer(&WSAID_CONNECTEX)),
2024-02-18 10:42:21 +00:00
uint32(unsafe.Sizeof(WSAID_CONNECTEX)),
2024-02-18 10:42:21 +00:00
(*byte)(unsafe.Pointer(&connectExFunc.addr)),
2024-02-18 10:42:21 +00:00
uint32(unsafe.Sizeof(connectExFunc.addr)),
2024-02-18 10:42:21 +00:00
&n, nil, 0)
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
return connectExFunc.err
2024-02-18 10:42:21 +00:00
}
func connectEx(s Handle, name unsafe.Pointer, namelen int32, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) (err error) {
2024-02-18 10:42:21 +00:00
r1, _, e1 := syscall.Syscall9(connectExFunc.addr, 7, uintptr(s), uintptr(name), uintptr(namelen), uintptr(unsafe.Pointer(sendBuf)), uintptr(sendDataLen), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), 0, 0)
2024-02-18 10:42:21 +00:00
if r1 == 0 {
2024-02-18 10:42:21 +00:00
if e1 != 0 {
2024-02-18 10:42:21 +00:00
err = error(e1)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
err = syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func ConnectEx(fd Handle, sa Sockaddr, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) error {
2024-02-18 10:42:21 +00:00
err := LoadConnectEx()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return errorspkg.New("failed to find ConnectEx: " + err.Error())
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
ptr, n, err := sa.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return connectEx(fd, ptr, n, sendBuf, sendDataLen, bytesSent, overlapped)
2024-02-18 10:42:21 +00:00
}
var sendRecvMsgFunc struct {
once sync.Once
2024-02-18 10:42:21 +00:00
sendAddr uintptr
2024-02-18 10:42:21 +00:00
recvAddr uintptr
err error
2024-02-18 10:42:21 +00:00
}
func loadWSASendRecvMsg() error {
2024-02-18 10:42:21 +00:00
sendRecvMsgFunc.once.Do(func() {
2024-02-18 10:42:21 +00:00
var s Handle
2024-02-18 10:42:21 +00:00
s, sendRecvMsgFunc.err = Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
2024-02-18 10:42:21 +00:00
if sendRecvMsgFunc.err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer CloseHandle(s)
2024-02-18 10:42:21 +00:00
var n uint32
2024-02-18 10:42:21 +00:00
sendRecvMsgFunc.err = WSAIoctl(s,
2024-02-18 10:42:21 +00:00
SIO_GET_EXTENSION_FUNCTION_POINTER,
2024-02-18 10:42:21 +00:00
(*byte)(unsafe.Pointer(&WSAID_WSARECVMSG)),
2024-02-18 10:42:21 +00:00
uint32(unsafe.Sizeof(WSAID_WSARECVMSG)),
2024-02-18 10:42:21 +00:00
(*byte)(unsafe.Pointer(&sendRecvMsgFunc.recvAddr)),
2024-02-18 10:42:21 +00:00
uint32(unsafe.Sizeof(sendRecvMsgFunc.recvAddr)),
2024-02-18 10:42:21 +00:00
&n, nil, 0)
2024-02-18 10:42:21 +00:00
if sendRecvMsgFunc.err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
sendRecvMsgFunc.err = WSAIoctl(s,
2024-02-18 10:42:21 +00:00
SIO_GET_EXTENSION_FUNCTION_POINTER,
2024-02-18 10:42:21 +00:00
(*byte)(unsafe.Pointer(&WSAID_WSASENDMSG)),
2024-02-18 10:42:21 +00:00
uint32(unsafe.Sizeof(WSAID_WSASENDMSG)),
2024-02-18 10:42:21 +00:00
(*byte)(unsafe.Pointer(&sendRecvMsgFunc.sendAddr)),
2024-02-18 10:42:21 +00:00
uint32(unsafe.Sizeof(sendRecvMsgFunc.sendAddr)),
2024-02-18 10:42:21 +00:00
&n, nil, 0)
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
return sendRecvMsgFunc.err
2024-02-18 10:42:21 +00:00
}
func WSASendMsg(fd Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlapped *Overlapped, croutine *byte) error {
2024-02-18 10:42:21 +00:00
err := loadWSASendRecvMsg()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.sendAddr, 6, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(flags), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)))
2024-02-18 10:42:21 +00:00
if r1 == socket_error {
2024-02-18 10:42:21 +00:00
err = errnoErr(e1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
func WSARecvMsg(fd Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *Overlapped, croutine *byte) error {
2024-02-18 10:42:21 +00:00
err := loadWSASendRecvMsg()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.recvAddr, 5, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(unsafe.Pointer(bytesReceived)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)), 0)
2024-02-18 10:42:21 +00:00
if r1 == socket_error {
2024-02-18 10:42:21 +00:00
err = errnoErr(e1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Invented structures to support what package os expects.
2024-02-18 10:42:21 +00:00
type Rusage struct {
CreationTime Filetime
ExitTime Filetime
KernelTime Filetime
UserTime Filetime
2024-02-18 10:42:21 +00:00
}
type WaitStatus struct {
ExitCode uint32
}
func (w WaitStatus) Exited() bool { return true }
func (w WaitStatus) ExitStatus() int { return int(w.ExitCode) }
func (w WaitStatus) Signal() Signal { return -1 }
func (w WaitStatus) CoreDump() bool { return false }
func (w WaitStatus) Stopped() bool { return false }
func (w WaitStatus) Continued() bool { return false }
func (w WaitStatus) StopSignal() Signal { return -1 }
func (w WaitStatus) Signaled() bool { return false }
func (w WaitStatus) TrapCause() int { return -1 }
// Timespec is an invented structure on Windows, but here for
2024-02-18 10:42:21 +00:00
// consistency with the corresponding package for other operating systems.
2024-02-18 10:42:21 +00:00
type Timespec struct {
Sec int64
2024-02-18 10:42:21 +00:00
Nsec int64
}
func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }
func NsecToTimespec(nsec int64) (ts Timespec) {
2024-02-18 10:42:21 +00:00
ts.Sec = nsec / 1e9
2024-02-18 10:42:21 +00:00
ts.Nsec = nsec % 1e9
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// TODO(brainman): fix all needed for net
func Accept(fd Handle) (nfd Handle, sa Sockaddr, err error) { return 0, nil, syscall.EWINDOWS }
func Recvfrom(fd Handle, p []byte, flags int) (n int, from Sockaddr, err error) {
2024-02-18 10:42:21 +00:00
var rsa RawSockaddrAny
2024-02-18 10:42:21 +00:00
l := int32(unsafe.Sizeof(rsa))
2024-02-18 10:42:21 +00:00
n32, err := recvfrom(fd, p, int32(flags), &rsa, &l)
2024-02-18 10:42:21 +00:00
n = int(n32)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
from, err = rsa.Sockaddr()
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func Sendto(fd Handle, p []byte, flags int, to Sockaddr) (err error) {
2024-02-18 10:42:21 +00:00
ptr, l, err := to.sockaddr()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sendto(fd, p, int32(flags), ptr, l)
2024-02-18 10:42:21 +00:00
}
func SetsockoptTimeval(fd Handle, level, opt int, tv *Timeval) (err error) { return syscall.EWINDOWS }
// The Linger struct is wrong but we only noticed after Go 1.
2024-02-18 10:42:21 +00:00
// sysLinger is the real system call structure.
// BUG(brainman): The definition of Linger is not appropriate for direct use
2024-02-18 10:42:21 +00:00
// with Setsockopt and Getsockopt.
2024-02-18 10:42:21 +00:00
// Use SetsockoptLinger instead.
type Linger struct {
Onoff int32
2024-02-18 10:42:21 +00:00
Linger int32
}
type sysLinger struct {
Onoff uint16
2024-02-18 10:42:21 +00:00
Linger uint16
}
type IPMreq struct {
Multiaddr [4]byte /* in_addr */
2024-02-18 10:42:21 +00:00
Interface [4]byte /* in_addr */
2024-02-18 10:42:21 +00:00
}
type IPv6Mreq struct {
Multiaddr [16]byte /* in6_addr */
2024-02-18 10:42:21 +00:00
Interface uint32
}
func GetsockoptInt(fd Handle, level, opt int) (int, error) {
2024-02-18 10:42:21 +00:00
v := int32(0)
2024-02-18 10:42:21 +00:00
l := int32(unsafe.Sizeof(v))
2024-02-18 10:42:21 +00:00
err := Getsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&v)), &l)
2024-02-18 10:42:21 +00:00
return int(v), err
2024-02-18 10:42:21 +00:00
}
func SetsockoptLinger(fd Handle, level, opt int, l *Linger) (err error) {
2024-02-18 10:42:21 +00:00
sys := sysLinger{Onoff: uint16(l.Onoff), Linger: uint16(l.Linger)}
2024-02-18 10:42:21 +00:00
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&sys)), int32(unsafe.Sizeof(sys)))
2024-02-18 10:42:21 +00:00
}
func SetsockoptInet4Addr(fd Handle, level, opt int, value [4]byte) (err error) {
2024-02-18 10:42:21 +00:00
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&value[0])), 4)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
func SetsockoptIPMreq(fd Handle, level, opt int, mreq *IPMreq) (err error) {
2024-02-18 10:42:21 +00:00
return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(mreq)), int32(unsafe.Sizeof(*mreq)))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
func SetsockoptIPv6Mreq(fd Handle, level, opt int, mreq *IPv6Mreq) (err error) {
2024-02-18 10:42:21 +00:00
return syscall.EWINDOWS
2024-02-18 10:42:21 +00:00
}
func EnumProcesses(processIds []uint32, bytesReturned *uint32) error {
2024-02-18 10:42:21 +00:00
// EnumProcesses syscall expects the size parameter to be in bytes, but the code generated with mksyscall uses
2024-02-18 10:42:21 +00:00
// the length of the processIds slice instead. Hence, this wrapper function is added to fix the discrepancy.
2024-02-18 10:42:21 +00:00
var p *uint32
2024-02-18 10:42:21 +00:00
if len(processIds) > 0 {
2024-02-18 10:42:21 +00:00
p = &processIds[0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
size := uint32(len(processIds) * 4)
2024-02-18 10:42:21 +00:00
return enumProcesses(p, size, bytesReturned)
2024-02-18 10:42:21 +00:00
}
func Getpid() (pid int) { return int(GetCurrentProcessId()) }
func FindFirstFile(name *uint16, data *Win32finddata) (handle Handle, err error) {
2024-02-18 10:42:21 +00:00
// NOTE(rsc): The Win32finddata struct is wrong for the system call:
2024-02-18 10:42:21 +00:00
// the two paths are each one uint16 short. Use the correct struct,
2024-02-18 10:42:21 +00:00
// a win32finddata1, and then copy the results out.
2024-02-18 10:42:21 +00:00
// There is no loss of expressivity here, because the final
2024-02-18 10:42:21 +00:00
// uint16, if it is used, is supposed to be a NUL, and Go doesn't need that.
2024-02-18 10:42:21 +00:00
// For Go 1.1, we might avoid the allocation of win32finddata1 here
2024-02-18 10:42:21 +00:00
// by adding a final Bug [2]uint16 field to the struct and then
2024-02-18 10:42:21 +00:00
// adjusting the fields in the result directly.
2024-02-18 10:42:21 +00:00
var data1 win32finddata1
2024-02-18 10:42:21 +00:00
handle, err = findFirstFile1(name, &data1)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
copyFindData(data, &data1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func FindNextFile(handle Handle, data *Win32finddata) (err error) {
2024-02-18 10:42:21 +00:00
var data1 win32finddata1
2024-02-18 10:42:21 +00:00
err = findNextFile1(handle, &data1)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
copyFindData(data, &data1)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func getProcessEntry(pid int) (*ProcessEntry32, error) {
2024-02-18 10:42:21 +00:00
snapshot, err := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer CloseHandle(snapshot)
2024-02-18 10:42:21 +00:00
var procEntry ProcessEntry32
2024-02-18 10:42:21 +00:00
procEntry.Size = uint32(unsafe.Sizeof(procEntry))
2024-02-18 10:42:21 +00:00
if err = Process32First(snapshot, &procEntry); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
if procEntry.ProcessID == uint32(pid) {
2024-02-18 10:42:21 +00:00
return &procEntry, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = Process32Next(snapshot, &procEntry)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func Getppid() (ppid int) {
2024-02-18 10:42:21 +00:00
pe, err := getProcessEntry(Getpid())
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return -1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return int(pe.ParentProcessID)
2024-02-18 10:42:21 +00:00
}
// TODO(brainman): fix all needed for os
func Fchdir(fd Handle) (err error) { return syscall.EWINDOWS }
2024-02-18 10:42:21 +00:00
func Link(oldpath, newpath string) (err error) { return syscall.EWINDOWS }
func Symlink(path, link string) (err error) { return syscall.EWINDOWS }
func Fchmod(fd Handle, mode uint32) (err error) { return syscall.EWINDOWS }
func Chown(path string, uid int, gid int) (err error) { return syscall.EWINDOWS }
2024-02-18 10:42:21 +00:00
func Lchown(path string, uid int, gid int) (err error) { return syscall.EWINDOWS }
func Fchown(fd Handle, uid int, gid int) (err error) { return syscall.EWINDOWS }
func Getuid() (uid int) { return -1 }
func Geteuid() (euid int) { return -1 }
func Getgid() (gid int) { return -1 }
func Getegid() (egid int) { return -1 }
2024-02-18 10:42:21 +00:00
func Getgroups() (gids []int, err error) { return nil, syscall.EWINDOWS }
type Signal int
func (s Signal) Signal() {}
func (s Signal) String() string {
2024-02-18 10:42:21 +00:00
if 0 <= s && int(s) < len(signals) {
2024-02-18 10:42:21 +00:00
str := signals[s]
2024-02-18 10:42:21 +00:00
if str != "" {
2024-02-18 10:42:21 +00:00
return str
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return "signal " + itoa(int(s))
2024-02-18 10:42:21 +00:00
}
func LoadCreateSymbolicLink() error {
2024-02-18 10:42:21 +00:00
return procCreateSymbolicLinkW.Find()
2024-02-18 10:42:21 +00:00
}
// Readlink returns the destination of the named symbolic link.
2024-02-18 10:42:21 +00:00
func Readlink(path string, buf []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
fd, err := CreateFile(StringToUTF16Ptr(path), GENERIC_READ, 0, nil, OPEN_EXISTING,
2024-02-18 10:42:21 +00:00
FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, 0)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return -1, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer CloseHandle(fd)
rdbbuf := make([]byte, MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
2024-02-18 10:42:21 +00:00
var bytesReturned uint32
2024-02-18 10:42:21 +00:00
err = DeviceIoControl(fd, FSCTL_GET_REPARSE_POINT, nil, 0, &rdbbuf[0], uint32(len(rdbbuf)), &bytesReturned, nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return -1, err
2024-02-18 10:42:21 +00:00
}
rdb := (*reparseDataBuffer)(unsafe.Pointer(&rdbbuf[0]))
2024-02-18 10:42:21 +00:00
var s string
2024-02-18 10:42:21 +00:00
switch rdb.ReparseTag {
2024-02-18 10:42:21 +00:00
case IO_REPARSE_TAG_SYMLINK:
2024-02-18 10:42:21 +00:00
data := (*symbolicLinkReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer))
2024-02-18 10:42:21 +00:00
p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0]))
2024-02-18 10:42:21 +00:00
s = UTF16ToString(p[data.PrintNameOffset/2 : (data.PrintNameLength-data.PrintNameOffset)/2])
2024-02-18 10:42:21 +00:00
case IO_REPARSE_TAG_MOUNT_POINT:
2024-02-18 10:42:21 +00:00
data := (*mountPointReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer))
2024-02-18 10:42:21 +00:00
p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0]))
2024-02-18 10:42:21 +00:00
s = UTF16ToString(p[data.PrintNameOffset/2 : (data.PrintNameLength-data.PrintNameOffset)/2])
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
// the path is not a symlink or junction but another type of reparse
2024-02-18 10:42:21 +00:00
// point
2024-02-18 10:42:21 +00:00
return -1, syscall.ENOENT
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n = copy(buf, []byte(s))
return n, nil
2024-02-18 10:42:21 +00:00
}
// GUIDFromString parses a string in the form of
2024-02-18 10:42:21 +00:00
// "{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" into a GUID.
2024-02-18 10:42:21 +00:00
func GUIDFromString(str string) (GUID, error) {
2024-02-18 10:42:21 +00:00
guid := GUID{}
2024-02-18 10:42:21 +00:00
str16, err := syscall.UTF16PtrFromString(str)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return guid, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = clsidFromString(str16, &guid)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return guid, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return guid, nil
2024-02-18 10:42:21 +00:00
}
// GenerateGUID creates a new random GUID.
2024-02-18 10:42:21 +00:00
func GenerateGUID() (GUID, error) {
2024-02-18 10:42:21 +00:00
guid := GUID{}
2024-02-18 10:42:21 +00:00
err := coCreateGuid(&guid)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return guid, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return guid, nil
2024-02-18 10:42:21 +00:00
}
// String returns the canonical string form of the GUID,
2024-02-18 10:42:21 +00:00
// in the form of "{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}".
2024-02-18 10:42:21 +00:00
func (guid GUID) String() string {
2024-02-18 10:42:21 +00:00
var str [100]uint16
2024-02-18 10:42:21 +00:00
chars := stringFromGUID2(&guid, &str[0], int32(len(str)))
2024-02-18 10:42:21 +00:00
if chars <= 1 {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(utf16.Decode(str[:chars-1]))
2024-02-18 10:42:21 +00:00
}
// KnownFolderPath returns a well-known folder path for the current user, specified by one of
2024-02-18 10:42:21 +00:00
// the FOLDERID_ constants, and chosen and optionally created based on a KF_ flag.
2024-02-18 10:42:21 +00:00
func KnownFolderPath(folderID *KNOWNFOLDERID, flags uint32) (string, error) {
2024-02-18 10:42:21 +00:00
return Token(0).KnownFolderPath(folderID, flags)
2024-02-18 10:42:21 +00:00
}
// KnownFolderPath returns a well-known folder path for the user token, specified by one of
2024-02-18 10:42:21 +00:00
// the FOLDERID_ constants, and chosen and optionally created based on a KF_ flag.
2024-02-18 10:42:21 +00:00
func (t Token) KnownFolderPath(folderID *KNOWNFOLDERID, flags uint32) (string, error) {
2024-02-18 10:42:21 +00:00
var p *uint16
2024-02-18 10:42:21 +00:00
err := shGetKnownFolderPath(folderID, flags, t, &p)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer CoTaskMemFree(unsafe.Pointer(p))
2024-02-18 10:42:21 +00:00
return UTF16PtrToString(p), nil
2024-02-18 10:42:21 +00:00
}
// RtlGetVersion returns the version of the underlying operating system, ignoring
2024-02-18 10:42:21 +00:00
// manifest semantics but is affected by the application compatibility layer.
2024-02-18 10:42:21 +00:00
func RtlGetVersion() *OsVersionInfoEx {
2024-02-18 10:42:21 +00:00
info := &OsVersionInfoEx{}
2024-02-18 10:42:21 +00:00
info.osVersionInfoSize = uint32(unsafe.Sizeof(*info))
2024-02-18 10:42:21 +00:00
// According to documentation, this function always succeeds.
2024-02-18 10:42:21 +00:00
// The function doesn't even check the validity of the
2024-02-18 10:42:21 +00:00
// osVersionInfoSize member. Disassembling ntdll.dll indicates
2024-02-18 10:42:21 +00:00
// that the documentation is indeed correct about that.
2024-02-18 10:42:21 +00:00
_ = rtlGetVersion(info)
2024-02-18 10:42:21 +00:00
return info
2024-02-18 10:42:21 +00:00
}
// RtlGetNtVersionNumbers returns the version of the underlying operating system,
2024-02-18 10:42:21 +00:00
// ignoring manifest semantics and the application compatibility layer.
2024-02-18 10:42:21 +00:00
func RtlGetNtVersionNumbers() (majorVersion, minorVersion, buildNumber uint32) {
2024-02-18 10:42:21 +00:00
rtlGetNtVersionNumbers(&majorVersion, &minorVersion, &buildNumber)
2024-02-18 10:42:21 +00:00
buildNumber &= 0xffff
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// GetProcessPreferredUILanguages retrieves the process preferred UI languages.
2024-02-18 10:42:21 +00:00
func GetProcessPreferredUILanguages(flags uint32) ([]string, error) {
2024-02-18 10:42:21 +00:00
return getUILanguages(flags, getProcessPreferredUILanguages)
2024-02-18 10:42:21 +00:00
}
// GetThreadPreferredUILanguages retrieves the thread preferred UI languages for the current thread.
2024-02-18 10:42:21 +00:00
func GetThreadPreferredUILanguages(flags uint32) ([]string, error) {
2024-02-18 10:42:21 +00:00
return getUILanguages(flags, getThreadPreferredUILanguages)
2024-02-18 10:42:21 +00:00
}
// GetUserPreferredUILanguages retrieves information about the user preferred UI languages.
2024-02-18 10:42:21 +00:00
func GetUserPreferredUILanguages(flags uint32) ([]string, error) {
2024-02-18 10:42:21 +00:00
return getUILanguages(flags, getUserPreferredUILanguages)
2024-02-18 10:42:21 +00:00
}
// GetSystemPreferredUILanguages retrieves the system preferred UI languages.
2024-02-18 10:42:21 +00:00
func GetSystemPreferredUILanguages(flags uint32) ([]string, error) {
2024-02-18 10:42:21 +00:00
return getUILanguages(flags, getSystemPreferredUILanguages)
2024-02-18 10:42:21 +00:00
}
func getUILanguages(flags uint32, f func(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) error) ([]string, error) {
2024-02-18 10:42:21 +00:00
size := uint32(128)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
var numLanguages uint32
2024-02-18 10:42:21 +00:00
buf := make([]uint16, size)
2024-02-18 10:42:21 +00:00
err := f(flags, &numLanguages, &buf[0], &size)
2024-02-18 10:42:21 +00:00
if err == ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf = buf[:size]
2024-02-18 10:42:21 +00:00
if numLanguages == 0 || len(buf) == 0 { // GetProcessPreferredUILanguages may return numLanguages==0 with "\0\0"
2024-02-18 10:42:21 +00:00
return []string{}, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if buf[len(buf)-1] == 0 {
2024-02-18 10:42:21 +00:00
buf = buf[:len(buf)-1] // remove terminating null
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
languages := make([]string, 0, numLanguages)
2024-02-18 10:42:21 +00:00
from := 0
2024-02-18 10:42:21 +00:00
for i, c := range buf {
2024-02-18 10:42:21 +00:00
if c == 0 {
2024-02-18 10:42:21 +00:00
languages = append(languages, string(utf16.Decode(buf[from:i])))
2024-02-18 10:42:21 +00:00
from = i + 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return languages, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
func SetConsoleCursorPosition(console Handle, position Coord) error {
2024-02-18 10:42:21 +00:00
return setConsoleCursorPosition(console, *((*uint32)(unsafe.Pointer(&position))))
2024-02-18 10:42:21 +00:00
}
func GetStartupInfo(startupInfo *StartupInfo) error {
2024-02-18 10:42:21 +00:00
getStartupInfo(startupInfo)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func (s NTStatus) Errno() syscall.Errno {
2024-02-18 10:42:21 +00:00
return rtlNtStatusToDosErrorNoTeb(s)
2024-02-18 10:42:21 +00:00
}
func langID(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) }
func (s NTStatus) Error() string {
2024-02-18 10:42:21 +00:00
b := make([]uint16, 300)
2024-02-18 10:42:21 +00:00
n, err := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_ARGUMENT_ARRAY, modntdll.Handle(), uint32(s), langID(LANG_ENGLISH, SUBLANG_ENGLISH_US), b, nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("NTSTATUS 0x%08x", uint32(s))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// trim terminating \r and \n
2024-02-18 10:42:21 +00:00
for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return string(utf16.Decode(b[:n]))
2024-02-18 10:42:21 +00:00
}
// NewNTUnicodeString returns a new NTUnicodeString structure for use with native
2024-02-18 10:42:21 +00:00
// NT APIs that work over the NTUnicodeString type. Note that most Windows APIs
2024-02-18 10:42:21 +00:00
// do not use NTUnicodeString, and instead UTF16PtrFromString should be used for
2024-02-18 10:42:21 +00:00
// the more common *uint16 string type.
2024-02-18 10:42:21 +00:00
func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
2024-02-18 10:42:21 +00:00
var u NTUnicodeString
2024-02-18 10:42:21 +00:00
s16, err := UTF16PtrFromString(s)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
RtlInitUnicodeString(&u, s16)
2024-02-18 10:42:21 +00:00
return &u, nil
2024-02-18 10:42:21 +00:00
}
// Slice returns a uint16 slice that aliases the data in the NTUnicodeString.
2024-02-18 10:42:21 +00:00
func (s *NTUnicodeString) Slice() []uint16 {
2024-02-18 10:42:21 +00:00
slice := unsafe.Slice(s.Buffer, s.MaximumLength)
2024-02-18 10:42:21 +00:00
return slice[:s.Length]
2024-02-18 10:42:21 +00:00
}
func (s *NTUnicodeString) String() string {
2024-02-18 10:42:21 +00:00
return UTF16ToString(s.Slice())
2024-02-18 10:42:21 +00:00
}
// NewNTString returns a new NTString structure for use with native
2024-02-18 10:42:21 +00:00
// NT APIs that work over the NTString type. Note that most Windows APIs
2024-02-18 10:42:21 +00:00
// do not use NTString, and instead UTF16PtrFromString should be used for
2024-02-18 10:42:21 +00:00
// the more common *uint16 string type.
2024-02-18 10:42:21 +00:00
func NewNTString(s string) (*NTString, error) {
2024-02-18 10:42:21 +00:00
var nts NTString
2024-02-18 10:42:21 +00:00
s8, err := BytePtrFromString(s)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
RtlInitString(&nts, s8)
2024-02-18 10:42:21 +00:00
return &nts, nil
2024-02-18 10:42:21 +00:00
}
// Slice returns a byte slice that aliases the data in the NTString.
2024-02-18 10:42:21 +00:00
func (s *NTString) Slice() []byte {
2024-02-18 10:42:21 +00:00
slice := unsafe.Slice(s.Buffer, s.MaximumLength)
2024-02-18 10:42:21 +00:00
return slice[:s.Length]
2024-02-18 10:42:21 +00:00
}
func (s *NTString) String() string {
2024-02-18 10:42:21 +00:00
return ByteSliceToString(s.Slice())
2024-02-18 10:42:21 +00:00
}
// FindResource resolves a resource of the given name and resource type.
2024-02-18 10:42:21 +00:00
func FindResource(module Handle, name, resType ResourceIDOrString) (Handle, error) {
2024-02-18 10:42:21 +00:00
var namePtr, resTypePtr uintptr
2024-02-18 10:42:21 +00:00
var name16, resType16 *uint16
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
resolvePtr := func(i interface{}, keep **uint16) (uintptr, error) {
2024-02-18 10:42:21 +00:00
switch v := i.(type) {
2024-02-18 10:42:21 +00:00
case string:
2024-02-18 10:42:21 +00:00
*keep, err = UTF16PtrFromString(v)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return uintptr(unsafe.Pointer(*keep)), nil
2024-02-18 10:42:21 +00:00
case ResourceID:
2024-02-18 10:42:21 +00:00
return uintptr(v), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, errorspkg.New("parameter must be a ResourceID or a string")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
namePtr, err = resolvePtr(name, &name16)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
resTypePtr, err = resolvePtr(resType, &resType16)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
resInfo, err := findResource(module, namePtr, resTypePtr)
2024-02-18 10:42:21 +00:00
runtime.KeepAlive(name16)
2024-02-18 10:42:21 +00:00
runtime.KeepAlive(resType16)
2024-02-18 10:42:21 +00:00
return resInfo, err
2024-02-18 10:42:21 +00:00
}
func LoadResourceData(module, resInfo Handle) (data []byte, err error) {
2024-02-18 10:42:21 +00:00
size, err := SizeofResource(module, resInfo)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
resData, err := LoadResource(module, resInfo)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
ptr, err := LockResource(resData)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
data = unsafe.Slice((*byte)(unsafe.Pointer(ptr)), size)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// PSAPI_WORKING_SET_EX_BLOCK contains extended working set information for a page.
2024-02-18 10:42:21 +00:00
type PSAPI_WORKING_SET_EX_BLOCK uint64
// Valid returns the validity of this page.
2024-02-18 10:42:21 +00:00
// If this bit is 1, the subsequent members are valid; otherwise they should be ignored.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) Valid() bool {
2024-02-18 10:42:21 +00:00
return (b & 1) == 1
2024-02-18 10:42:21 +00:00
}
// ShareCount is the number of processes that share this page. The maximum value of this member is 7.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) ShareCount() uint64 {
2024-02-18 10:42:21 +00:00
return b.intField(1, 3)
2024-02-18 10:42:21 +00:00
}
// Win32Protection is the memory protection attributes of the page. For a list of values, see
2024-02-18 10:42:21 +00:00
// https://docs.microsoft.com/en-us/windows/win32/memory/memory-protection-constants
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) Win32Protection() uint64 {
2024-02-18 10:42:21 +00:00
return b.intField(4, 11)
2024-02-18 10:42:21 +00:00
}
// Shared returns the shared status of this page.
2024-02-18 10:42:21 +00:00
// If this bit is 1, the page can be shared.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) Shared() bool {
2024-02-18 10:42:21 +00:00
return (b & (1 << 15)) == 1
2024-02-18 10:42:21 +00:00
}
// Node is the NUMA node. The maximum value of this member is 63.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) Node() uint64 {
2024-02-18 10:42:21 +00:00
return b.intField(16, 6)
2024-02-18 10:42:21 +00:00
}
// Locked returns the locked status of this page.
2024-02-18 10:42:21 +00:00
// If this bit is 1, the virtual page is locked in physical memory.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) Locked() bool {
2024-02-18 10:42:21 +00:00
return (b & (1 << 22)) == 1
2024-02-18 10:42:21 +00:00
}
// LargePage returns the large page status of this page.
2024-02-18 10:42:21 +00:00
// If this bit is 1, the page is a large page.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) LargePage() bool {
2024-02-18 10:42:21 +00:00
return (b & (1 << 23)) == 1
2024-02-18 10:42:21 +00:00
}
// Bad returns the bad status of this page.
2024-02-18 10:42:21 +00:00
// If this bit is 1, the page is has been reported as bad.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) Bad() bool {
2024-02-18 10:42:21 +00:00
return (b & (1 << 31)) == 1
2024-02-18 10:42:21 +00:00
}
// intField extracts an integer field in the PSAPI_WORKING_SET_EX_BLOCK union.
2024-02-18 10:42:21 +00:00
func (b PSAPI_WORKING_SET_EX_BLOCK) intField(start, length int) uint64 {
2024-02-18 10:42:21 +00:00
var mask PSAPI_WORKING_SET_EX_BLOCK
2024-02-18 10:42:21 +00:00
for pos := start; pos < start+length; pos++ {
2024-02-18 10:42:21 +00:00
mask |= (1 << pos)
2024-02-18 10:42:21 +00:00
}
masked := b & mask
2024-02-18 10:42:21 +00:00
return uint64(masked >> start)
2024-02-18 10:42:21 +00:00
}
// PSAPI_WORKING_SET_EX_INFORMATION contains extended working set information for a process.
2024-02-18 10:42:21 +00:00
type PSAPI_WORKING_SET_EX_INFORMATION struct {
2024-02-18 10:42:21 +00:00
// The virtual address.
2024-02-18 10:42:21 +00:00
VirtualAddress Pointer
2024-02-18 10:42:21 +00:00
// A PSAPI_WORKING_SET_EX_BLOCK union that indicates the attributes of the page at VirtualAddress.
2024-02-18 10:42:21 +00:00
VirtualAttributes PSAPI_WORKING_SET_EX_BLOCK
}
// CreatePseudoConsole creates a windows pseudo console.
2024-02-18 10:42:21 +00:00
func CreatePseudoConsole(size Coord, in Handle, out Handle, flags uint32, pconsole *Handle) error {
2024-02-18 10:42:21 +00:00
// We need this wrapper to manually cast Coord to uint32. The autogenerated wrappers only
2024-02-18 10:42:21 +00:00
// accept arguments that can be casted to uintptr, and Coord can't.
2024-02-18 10:42:21 +00:00
return createPseudoConsole(*((*uint32)(unsafe.Pointer(&size))), in, out, flags, pconsole)
2024-02-18 10:42:21 +00:00
}
// ResizePseudoConsole resizes the internal buffers of the pseudo console to the width and height specified in `size`.
2024-02-18 10:42:21 +00:00
func ResizePseudoConsole(pconsole Handle, size Coord) error {
2024-02-18 10:42:21 +00:00
// We need this wrapper to manually cast Coord to uint32. The autogenerated wrappers only
2024-02-18 10:42:21 +00:00
// accept arguments that can be casted to uintptr, and Coord can't.
2024-02-18 10:42:21 +00:00
return resizePseudoConsole(pconsole, *((*uint32)(unsafe.Pointer(&size))))
2024-02-18 10:42:21 +00:00
}
2024-05-14 13:07:09 +00:00
// DCB constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-dcb.
2024-05-14 13:07:09 +00:00
const (
CBR_110 = 110
CBR_300 = 300
CBR_600 = 600
CBR_1200 = 1200
CBR_2400 = 2400
CBR_4800 = 4800
CBR_9600 = 9600
CBR_14400 = 14400
CBR_19200 = 19200
CBR_38400 = 38400
CBR_57600 = 57600
2024-05-14 13:07:09 +00:00
CBR_115200 = 115200
2024-05-14 13:07:09 +00:00
CBR_128000 = 128000
2024-05-14 13:07:09 +00:00
CBR_256000 = 256000
DTR_CONTROL_DISABLE = 0x00000000
DTR_CONTROL_ENABLE = 0x00000010
2024-05-14 13:07:09 +00:00
DTR_CONTROL_HANDSHAKE = 0x00000020
RTS_CONTROL_DISABLE = 0x00000000
RTS_CONTROL_ENABLE = 0x00001000
2024-05-14 13:07:09 +00:00
RTS_CONTROL_HANDSHAKE = 0x00002000
RTS_CONTROL_TOGGLE = 0x00003000
NOPARITY = 0
ODDPARITY = 1
EVENPARITY = 2
MARKPARITY = 3
2024-05-14 13:07:09 +00:00
SPACEPARITY = 4
ONESTOPBIT = 0
2024-05-14 13:07:09 +00:00
ONE5STOPBITS = 1
TWOSTOPBITS = 2
2024-05-14 13:07:09 +00:00
)
// EscapeCommFunction constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-escapecommfunction.
2024-05-14 13:07:09 +00:00
const (
SETXOFF = 1
SETXON = 2
SETRTS = 3
CLRRTS = 4
SETDTR = 5
CLRDTR = 6
2024-05-14 13:07:09 +00:00
SETBREAK = 8
2024-05-14 13:07:09 +00:00
CLRBREAK = 9
)
// PurgeComm constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-purgecomm.
2024-05-14 13:07:09 +00:00
const (
PURGE_TXABORT = 0x0001
2024-05-14 13:07:09 +00:00
PURGE_RXABORT = 0x0002
2024-05-14 13:07:09 +00:00
PURGE_TXCLEAR = 0x0004
2024-05-14 13:07:09 +00:00
PURGE_RXCLEAR = 0x0008
)
// SetCommMask constants. See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setcommmask.
2024-05-14 13:07:09 +00:00
const (
EV_RXCHAR = 0x0001
EV_RXFLAG = 0x0002
2024-05-14 13:07:09 +00:00
EV_TXEMPTY = 0x0004
EV_CTS = 0x0008
EV_DSR = 0x0010
EV_RLSD = 0x0020
EV_BREAK = 0x0040
EV_ERR = 0x0080
EV_RING = 0x0100
2024-05-14 13:07:09 +00:00
)