12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682 |
- // Copyright 2009 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // 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"
- "golang.org/x/sys/internal/unsafeheader"
- )
- type Handle uintptr
- type HWND uintptr
- const (
- InvalidHandle = ^Handle(0)
- InvalidHWND = ^HWND(0)
- // Flags for DefineDosDevice.
- DDD_EXACT_MATCH_ON_REMOVE = 0x00000004
- DDD_NO_BROADCAST_SYSTEM = 0x00000008
- DDD_RAW_TARGET_PATH = 0x00000001
- DDD_REMOVE_DEFINITION = 0x00000002
- // Return values for GetDriveType.
- DRIVE_UNKNOWN = 0
- DRIVE_NO_ROOT_DIR = 1
- DRIVE_REMOVABLE = 2
- DRIVE_FIXED = 3
- DRIVE_REMOTE = 4
- DRIVE_CDROM = 5
- DRIVE_RAMDISK = 6
- // 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
- 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
- // Flags for LockFileEx.
- LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
- LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
- // Return value of SleepEx and other APC functions
- WAIT_IO_COMPLETION = 0x000000C0
- )
- // StringToUTF16 is deprecated. Use UTF16FromString instead.
- // If s contains a NUL byte this function panics instead of
- // returning an error.
- func StringToUTF16(s string) []uint16 {
- a, err := UTF16FromString(s)
- if err != nil {
- panic("windows: string with NUL passed to StringToUTF16")
- }
- return a
- }
- // UTF16FromString returns the UTF-16 encoding of the UTF-8 string
- // s, with a terminating NUL added. If s contains a NUL byte at any
- // location, it returns (nil, syscall.EINVAL).
- func UTF16FromString(s string) ([]uint16, error) {
- for i := 0; i < len(s); i++ {
- if s[i] == 0 {
- return nil, syscall.EINVAL
- }
- }
- return utf16.Encode([]rune(s + "\x00")), nil
- }
- // UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,
- // with a terminating NUL and any bytes after the NUL removed.
- func UTF16ToString(s []uint16) string {
- for i, v := range s {
- if v == 0 {
- s = s[:i]
- break
- }
- }
- return string(utf16.Decode(s))
- }
- // StringToUTF16Ptr is deprecated. Use UTF16PtrFromString instead.
- // If s contains a NUL byte this function panics instead of
- // returning an error.
- func StringToUTF16Ptr(s string) *uint16 { return &StringToUTF16(s)[0] }
- // UTF16PtrFromString returns pointer to the UTF-16 encoding of
- // the UTF-8 string s, with a terminating NUL added. If s
- // contains a NUL byte at any location, it returns (nil, syscall.EINVAL).
- func UTF16PtrFromString(s string) (*uint16, error) {
- a, err := UTF16FromString(s)
- if err != nil {
- return nil, err
- }
- return &a[0], nil
- }
- // UTF16PtrToString takes a pointer to a UTF-16 sequence and returns the corresponding UTF-8 encoded string.
- // If the pointer is nil, it returns the empty string. It assumes that the UTF-16 sequence is terminated
- // at a zero word; if the zero word is not present, the program may crash.
- func UTF16PtrToString(p *uint16) string {
- if p == nil {
- return ""
- }
- if *p == 0 {
- return ""
- }
- // Find NUL terminator.
- n := 0
- for ptr := unsafe.Pointer(p); *(*uint16)(ptr) != 0; n++ {
- ptr = unsafe.Pointer(uintptr(ptr) + unsafe.Sizeof(*p))
- }
- var s []uint16
- h := (*unsafeheader.Slice)(unsafe.Pointer(&s))
- h.Data = unsafe.Pointer(p)
- h.Len = n
- h.Cap = n
- return string(utf16.Decode(s))
- }
- func Getpagesize() int { return 4096 }
- // NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention.
- // This is useful when interoperating with Windows code requiring callbacks.
- // The argument is expected to be a function with with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr.
- func NewCallback(fn interface{}) uintptr {
- return syscall.NewCallback(fn)
- }
- // NewCallbackCDecl converts a Go function to a function pointer conforming to the cdecl calling convention.
- // This is useful when interoperating with Windows code requiring callbacks.
- // The argument is expected to be a function with with one uintptr-sized result. The function must not have arguments with size larger than the size of uintptr.
- func NewCallbackCDecl(fn interface{}) uintptr {
- return syscall.NewCallbackCDecl(fn)
- }
- // windows api calls
- //sys GetLastError() (lasterr error)
- //sys LoadLibrary(libname string) (handle Handle, err error) = LoadLibraryW
- //sys LoadLibraryEx(libname string, zero Handle, flags uintptr) (handle Handle, err error) = LoadLibraryExW
- //sys FreeLibrary(handle Handle) (err error)
- //sys GetProcAddress(module Handle, procname string) (proc uintptr, err error)
- //sys GetModuleFileName(module Handle, filename *uint16, size uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
- //sys GetModuleHandleEx(flags uint32, moduleName *uint16, module *Handle) (err error) = kernel32.GetModuleHandleExW
- //sys SetDefaultDllDirectories(directoryFlags uint32) (err error)
- //sys SetDllDirectory(path string) (err error) = kernel32.SetDllDirectoryW
- //sys GetVersion() (ver uint32, err error)
- //sys FormatMessage(flags uint32, msgsrc uintptr, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) = FormatMessageW
- //sys ExitProcess(exitcode uint32)
- //sys IsWow64Process(handle Handle, isWow64 *bool) (err error) = IsWow64Process
- //sys IsWow64Process2(handle Handle, processMachine *uint16, nativeMachine *uint16) (err error) = IsWow64Process2?
- //sys CreateFile(name *uint16, access uint32, mode uint32, sa *SecurityAttributes, createmode uint32, attrs uint32, templatefile Handle) (handle Handle, err error) [failretval==InvalidHandle] = CreateFileW
- //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
- //sys ConnectNamedPipe(pipe Handle, overlapped *Overlapped) (err error)
- //sys GetNamedPipeInfo(pipe Handle, flags *uint32, outSize *uint32, inSize *uint32, maxInstances *uint32) (err error)
- //sys GetNamedPipeHandleState(pipe Handle, state *uint32, curInstances *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32, userName *uint16, maxUserNameSize uint32) (err error) = GetNamedPipeHandleStateW
- //sys SetNamedPipeHandleState(pipe Handle, state *uint32, maxCollectionCount *uint32, collectDataTimeout *uint32) (err error) = SetNamedPipeHandleState
- //sys ReadFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error)
- //sys WriteFile(handle Handle, buf []byte, done *uint32, overlapped *Overlapped) (err error)
- //sys GetOverlappedResult(handle Handle, overlapped *Overlapped, done *uint32, wait bool) (err error)
- //sys SetFilePointer(handle Handle, lowoffset int32, highoffsetptr *int32, whence uint32) (newlowoffset uint32, err error) [failretval==0xffffffff]
- //sys CloseHandle(handle Handle) (err error)
- //sys GetStdHandle(stdhandle uint32) (handle Handle, err error) [failretval==InvalidHandle]
- //sys SetStdHandle(stdhandle uint32, handle Handle) (err error)
- //sys findFirstFile1(name *uint16, data *win32finddata1) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstFileW
- //sys findNextFile1(handle Handle, data *win32finddata1) (err error) = FindNextFileW
- //sys FindClose(handle Handle) (err error)
- //sys GetFileInformationByHandle(handle Handle, data *ByHandleFileInformation) (err error)
- //sys GetFileInformationByHandleEx(handle Handle, class uint32, outBuffer *byte, outBufferLen uint32) (err error)
- //sys SetFileInformationByHandle(handle Handle, class uint32, inBuffer *byte, inBufferLen uint32) (err error)
- //sys GetCurrentDirectory(buflen uint32, buf *uint16) (n uint32, err error) = GetCurrentDirectoryW
- //sys SetCurrentDirectory(path *uint16) (err error) = SetCurrentDirectoryW
- //sys CreateDirectory(path *uint16, sa *SecurityAttributes) (err error) = CreateDirectoryW
- //sys RemoveDirectory(path *uint16) (err error) = RemoveDirectoryW
- //sys DeleteFile(path *uint16) (err error) = DeleteFileW
- //sys MoveFile(from *uint16, to *uint16) (err error) = MoveFileW
- //sys MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW
- //sys LockFileEx(file Handle, flags uint32, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *Overlapped) (err error)
- //sys UnlockFileEx(file Handle, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *Overlapped) (err error)
- //sys GetComputerName(buf *uint16, n *uint32) (err error) = GetComputerNameW
- //sys GetComputerNameEx(nametype uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW
- //sys SetEndOfFile(handle Handle) (err error)
- //sys GetSystemTimeAsFileTime(time *Filetime)
- //sys GetSystemTimePreciseAsFileTime(time *Filetime)
- //sys GetTimeZoneInformation(tzi *Timezoneinformation) (rc uint32, err error) [failretval==0xffffffff]
- //sys CreateIoCompletionPort(filehandle Handle, cphandle Handle, key uintptr, threadcnt uint32) (handle Handle, err error)
- //sys GetQueuedCompletionStatus(cphandle Handle, qty *uint32, key *uintptr, overlapped **Overlapped, timeout uint32) (err error)
- //sys PostQueuedCompletionStatus(cphandle Handle, qty uint32, key uintptr, overlapped *Overlapped) (err error)
- //sys CancelIo(s Handle) (err error)
- //sys CancelIoEx(s Handle, o *Overlapped) (err error)
- //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
- //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
- //sys initializeProcThreadAttributeList(attrlist *ProcThreadAttributeList, attrcount uint32, flags uint32, size *uintptr) (err error) = InitializeProcThreadAttributeList
- //sys deleteProcThreadAttributeList(attrlist *ProcThreadAttributeList) = DeleteProcThreadAttributeList
- //sys updateProcThreadAttribute(attrlist *ProcThreadAttributeList, flags uint32, attr uintptr, value unsafe.Pointer, size uintptr, prevvalue unsafe.Pointer, returnedsize *uintptr) (err error) = UpdateProcThreadAttribute
- //sys OpenProcess(desiredAccess uint32, inheritHandle bool, processId uint32) (handle Handle, err error)
- //sys ShellExecute(hwnd Handle, verb *uint16, file *uint16, args *uint16, cwd *uint16, showCmd int32) (err error) [failretval<=32] = shell32.ShellExecuteW
- //sys GetWindowThreadProcessId(hwnd HWND, pid *uint32) (tid uint32, err error) = user32.GetWindowThreadProcessId
- //sys GetShellWindow() (shellWindow HWND) = user32.GetShellWindow
- //sys MessageBox(hwnd HWND, text *uint16, caption *uint16, boxtype uint32) (ret int32, err error) [failretval==0] = user32.MessageBoxW
- //sys ExitWindowsEx(flags uint32, reason uint32) (err error) = user32.ExitWindowsEx
- //sys shGetKnownFolderPath(id *KNOWNFOLDERID, flags uint32, token Token, path **uint16) (ret error) = shell32.SHGetKnownFolderPath
- //sys TerminateProcess(handle Handle, exitcode uint32) (err error)
- //sys GetExitCodeProcess(handle Handle, exitcode *uint32) (err error)
- //sys GetStartupInfo(startupInfo *StartupInfo) (err error) = GetStartupInfoW
- //sys GetProcessTimes(handle Handle, creationTime *Filetime, exitTime *Filetime, kernelTime *Filetime, userTime *Filetime) (err error)
- //sys DuplicateHandle(hSourceProcessHandle Handle, hSourceHandle Handle, hTargetProcessHandle Handle, lpTargetHandle *Handle, dwDesiredAccess uint32, bInheritHandle bool, dwOptions uint32) (err error)
- //sys WaitForSingleObject(handle Handle, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff]
- //sys waitForMultipleObjects(count uint32, handles uintptr, waitAll bool, waitMilliseconds uint32) (event uint32, err error) [failretval==0xffffffff] = WaitForMultipleObjects
- //sys GetTempPath(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPathW
- //sys CreatePipe(readhandle *Handle, writehandle *Handle, sa *SecurityAttributes, size uint32) (err error)
- //sys GetFileType(filehandle Handle) (n uint32, err error)
- //sys CryptAcquireContext(provhandle *Handle, container *uint16, provider *uint16, provtype uint32, flags uint32) (err error) = advapi32.CryptAcquireContextW
- //sys CryptReleaseContext(provhandle Handle, flags uint32) (err error) = advapi32.CryptReleaseContext
- //sys CryptGenRandom(provhandle Handle, buflen uint32, buf *byte) (err error) = advapi32.CryptGenRandom
- //sys GetEnvironmentStrings() (envs *uint16, err error) [failretval==nil] = kernel32.GetEnvironmentStringsW
- //sys FreeEnvironmentStrings(envs *uint16) (err error) = kernel32.FreeEnvironmentStringsW
- //sys GetEnvironmentVariable(name *uint16, buffer *uint16, size uint32) (n uint32, err error) = kernel32.GetEnvironmentVariableW
- //sys SetEnvironmentVariable(name *uint16, value *uint16) (err error) = kernel32.SetEnvironmentVariableW
- //sys CreateEnvironmentBlock(block **uint16, token Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
- //sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
- //sys getTickCount64() (ms uint64) = kernel32.GetTickCount64
- //sys SetFileTime(handle Handle, ctime *Filetime, atime *Filetime, wtime *Filetime) (err error)
- //sys GetFileAttributes(name *uint16) (attrs uint32, err error) [failretval==INVALID_FILE_ATTRIBUTES] = kernel32.GetFileAttributesW
- //sys SetFileAttributes(name *uint16, attrs uint32) (err error) = kernel32.SetFileAttributesW
- //sys GetFileAttributesEx(name *uint16, level uint32, info *byte) (err error) = kernel32.GetFileAttributesExW
- //sys GetCommandLine() (cmd *uint16) = kernel32.GetCommandLineW
- //sys CommandLineToArgv(cmd *uint16, argc *int32) (argv *[8192]*[8192]uint16, err error) [failretval==nil] = shell32.CommandLineToArgvW
- //sys LocalFree(hmem Handle) (handle Handle, err error) [failretval!=0]
- //sys LocalAlloc(flags uint32, length uint32) (ptr uintptr, err error)
- //sys SetHandleInformation(handle Handle, mask uint32, flags uint32) (err error)
- //sys FlushFileBuffers(handle Handle) (err error)
- //sys GetFullPathName(path *uint16, buflen uint32, buf *uint16, fname **uint16) (n uint32, err error) = kernel32.GetFullPathNameW
- //sys GetLongPathName(path *uint16, buf *uint16, buflen uint32) (n uint32, err error) = kernel32.GetLongPathNameW
- //sys GetShortPathName(longpath *uint16, shortpath *uint16, buflen uint32) (n uint32, err error) = kernel32.GetShortPathNameW
- //sys GetFinalPathNameByHandle(file Handle, filePath *uint16, filePathSize uint32, flags uint32) (n uint32, err error) = kernel32.GetFinalPathNameByHandleW
- //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
- //sys MapViewOfFile(handle Handle, access uint32, offsetHigh uint32, offsetLow uint32, length uintptr) (addr uintptr, err error)
- //sys UnmapViewOfFile(addr uintptr) (err error)
- //sys FlushViewOfFile(addr uintptr, length uintptr) (err error)
- //sys VirtualLock(addr uintptr, length uintptr) (err error)
- //sys VirtualUnlock(addr uintptr, length uintptr) (err error)
- //sys VirtualAlloc(address uintptr, size uintptr, alloctype uint32, protect uint32) (value uintptr, err error) = kernel32.VirtualAlloc
- //sys VirtualFree(address uintptr, size uintptr, freetype uint32) (err error) = kernel32.VirtualFree
- //sys VirtualProtect(address uintptr, size uintptr, newprotect uint32, oldprotect *uint32) (err error) = kernel32.VirtualProtect
- //sys VirtualProtectEx(process Handle, address uintptr, size uintptr, newProtect uint32, oldProtect *uint32) (err error) = kernel32.VirtualProtectEx
- //sys VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQuery
- //sys VirtualQueryEx(process Handle, address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQueryEx
- //sys ReadProcessMemory(process Handle, baseAddress uintptr, buffer *byte, size uintptr, numberOfBytesRead *uintptr) (err error) = kernel32.ReadProcessMemory
- //sys WriteProcessMemory(process Handle, baseAddress uintptr, buffer *byte, size uintptr, numberOfBytesWritten *uintptr) (err error) = kernel32.WriteProcessMemory
- //sys TransmitFile(s Handle, handle Handle, bytesToWrite uint32, bytsPerSend uint32, overlapped *Overlapped, transmitFileBuf *TransmitFileBuffers, flags uint32) (err error) = mswsock.TransmitFile
- //sys ReadDirectoryChanges(handle Handle, buf *byte, buflen uint32, watchSubTree bool, mask uint32, retlen *uint32, overlapped *Overlapped, completionRoutine uintptr) (err error) = kernel32.ReadDirectoryChangesW
- //sys FindFirstChangeNotification(path string, watchSubtree bool, notifyFilter uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.FindFirstChangeNotificationW
- //sys FindNextChangeNotification(handle Handle) (err error)
- //sys FindCloseChangeNotification(handle Handle) (err error)
- //sys CertOpenSystemStore(hprov Handle, name *uint16) (store Handle, err error) = crypt32.CertOpenSystemStoreW
- //sys CertOpenStore(storeProvider uintptr, msgAndCertEncodingType uint32, cryptProv uintptr, flags uint32, para uintptr) (handle Handle, err error) = crypt32.CertOpenStore
- //sys CertEnumCertificatesInStore(store Handle, prevContext *CertContext) (context *CertContext, err error) [failretval==nil] = crypt32.CertEnumCertificatesInStore
- //sys CertAddCertificateContextToStore(store Handle, certContext *CertContext, addDisposition uint32, storeContext **CertContext) (err error) = crypt32.CertAddCertificateContextToStore
- //sys CertCloseStore(store Handle, flags uint32) (err error) = crypt32.CertCloseStore
- //sys CertDeleteCertificateFromStore(certContext *CertContext) (err error) = crypt32.CertDeleteCertificateFromStore
- //sys CertDuplicateCertificateContext(certContext *CertContext) (dupContext *CertContext) = crypt32.CertDuplicateCertificateContext
- //sys PFXImportCertStore(pfx *CryptDataBlob, password *uint16, flags uint32) (store Handle, err error) = crypt32.PFXImportCertStore
- //sys CertGetCertificateChain(engine Handle, leaf *CertContext, time *Filetime, additionalStore Handle, para *CertChainPara, flags uint32, reserved uintptr, chainCtx **CertChainContext) (err error) = crypt32.CertGetCertificateChain
- //sys CertFreeCertificateChain(ctx *CertChainContext) = crypt32.CertFreeCertificateChain
- //sys CertCreateCertificateContext(certEncodingType uint32, certEncoded *byte, encodedLen uint32) (context *CertContext, err error) [failretval==nil] = crypt32.CertCreateCertificateContext
- //sys CertFreeCertificateContext(ctx *CertContext) (err error) = crypt32.CertFreeCertificateContext
- //sys CertVerifyCertificateChainPolicy(policyOID uintptr, chain *CertChainContext, para *CertChainPolicyPara, status *CertChainPolicyStatus) (err error) = crypt32.CertVerifyCertificateChainPolicy
- //sys CertGetNameString(certContext *CertContext, nameType uint32, flags uint32, typePara unsafe.Pointer, name *uint16, size uint32) (chars uint32) = crypt32.CertGetNameStringW
- //sys CertFindExtension(objId *byte, countExtensions uint32, extensions *CertExtension) (ret *CertExtension) = crypt32.CertFindExtension
- //sys CertFindCertificateInStore(store Handle, certEncodingType uint32, findFlags uint32, findType uint32, findPara unsafe.Pointer, prevCertContext *CertContext) (cert *CertContext, err error) [failretval==nil] = crypt32.CertFindCertificateInStore
- //sys CertFindChainInStore(store Handle, certEncodingType uint32, findFlags uint32, findType uint32, findPara unsafe.Pointer, prevChainContext *CertChainContext) (certchain *CertChainContext, err error) [failretval==nil] = crypt32.CertFindChainInStore
- //sys CryptAcquireCertificatePrivateKey(cert *CertContext, flags uint32, parameters unsafe.Pointer, cryptProvOrNCryptKey *Handle, keySpec *uint32, callerFreeProvOrNCryptKey *bool) (err error) = crypt32.CryptAcquireCertificatePrivateKey
- //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
- //sys CryptDecodeObject(encodingType uint32, structType *byte, encodedBytes *byte, lenEncodedBytes uint32, flags uint32, decoded unsafe.Pointer, decodedLen *uint32) (err error) = crypt32.CryptDecodeObject
- //sys CryptProtectData(dataIn *DataBlob, name *uint16, optionalEntropy *DataBlob, reserved uintptr, promptStruct *CryptProtectPromptStruct, flags uint32, dataOut *DataBlob) (err error) = crypt32.CryptProtectData
- //sys CryptUnprotectData(dataIn *DataBlob, name **uint16, optionalEntropy *DataBlob, reserved uintptr, promptStruct *CryptProtectPromptStruct, flags uint32, dataOut *DataBlob) (err error) = crypt32.CryptUnprotectData
- //sys WinVerifyTrustEx(hwnd HWND, actionId *GUID, data *WinTrustData) (ret error) = wintrust.WinVerifyTrustEx
- //sys RegOpenKeyEx(key Handle, subkey *uint16, options uint32, desiredAccess uint32, result *Handle) (regerrno error) = advapi32.RegOpenKeyExW
- //sys RegCloseKey(key Handle) (regerrno error) = advapi32.RegCloseKey
- //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
- //sys RegEnumKeyEx(key Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, class *uint16, classLen *uint32, lastWriteTime *Filetime) (regerrno error) = advapi32.RegEnumKeyExW
- //sys RegQueryValueEx(key Handle, name *uint16, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) = advapi32.RegQueryValueExW
- //sys RegNotifyChangeKeyValue(key Handle, watchSubtree bool, notifyFilter uint32, event Handle, asynchronous bool) (regerrno error) = advapi32.RegNotifyChangeKeyValue
- //sys GetCurrentProcessId() (pid uint32) = kernel32.GetCurrentProcessId
- //sys ProcessIdToSessionId(pid uint32, sessionid *uint32) (err error) = kernel32.ProcessIdToSessionId
- //sys GetConsoleMode(console Handle, mode *uint32) (err error) = kernel32.GetConsoleMode
- //sys SetConsoleMode(console Handle, mode uint32) (err error) = kernel32.SetConsoleMode
- //sys GetConsoleScreenBufferInfo(console Handle, info *ConsoleScreenBufferInfo) (err error) = kernel32.GetConsoleScreenBufferInfo
- //sys setConsoleCursorPosition(console Handle, position uint32) (err error) = kernel32.SetConsoleCursorPosition
- //sys WriteConsole(console Handle, buf *uint16, towrite uint32, written *uint32, reserved *byte) (err error) = kernel32.WriteConsoleW
- //sys ReadConsole(console Handle, buf *uint16, toread uint32, read *uint32, inputControl *byte) (err error) = kernel32.ReadConsoleW
- //sys CreateToolhelp32Snapshot(flags uint32, processId uint32) (handle Handle, err error) [failretval==InvalidHandle] = kernel32.CreateToolhelp32Snapshot
- //sys Process32First(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32FirstW
- //sys Process32Next(snapshot Handle, procEntry *ProcessEntry32) (err error) = kernel32.Process32NextW
- //sys Thread32First(snapshot Handle, threadEntry *ThreadEntry32) (err error)
- //sys Thread32Next(snapshot Handle, threadEntry *ThreadEntry32) (err error)
- //sys DeviceIoControl(handle Handle, ioControlCode uint32, inBuffer *byte, inBufferSize uint32, outBuffer *byte, outBufferSize uint32, bytesReturned *uint32, overlapped *Overlapped) (err error)
- // This function returns 1 byte BOOLEAN rather than the 4 byte BOOL.
- //sys CreateSymbolicLink(symlinkfilename *uint16, targetfilename *uint16, flags uint32) (err error) [failretval&0xff==0] = CreateSymbolicLinkW
- //sys CreateHardLink(filename *uint16, existingfilename *uint16, reserved uintptr) (err error) [failretval&0xff==0] = CreateHardLinkW
- //sys GetCurrentThreadId() (id uint32)
- //sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateEventW
- //sys CreateEventEx(eventAttrs *SecurityAttributes, name *uint16, flags uint32, desiredAccess uint32) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateEventExW
- //sys OpenEvent(desiredAccess uint32, inheritHandle bool, name *uint16) (handle Handle, err error) = kernel32.OpenEventW
- //sys SetEvent(event Handle) (err error) = kernel32.SetEvent
- //sys ResetEvent(event Handle) (err error) = kernel32.ResetEvent
- //sys PulseEvent(event Handle) (err error) = kernel32.PulseEvent
- //sys CreateMutex(mutexAttrs *SecurityAttributes, initialOwner bool, name *uint16) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateMutexW
- //sys CreateMutexEx(mutexAttrs *SecurityAttributes, name *uint16, flags uint32, desiredAccess uint32) (handle Handle, err error) [failretval == 0 || e1 == ERROR_ALREADY_EXISTS] = kernel32.CreateMutexExW
- //sys OpenMutex(desiredAccess uint32, inheritHandle bool, name *uint16) (handle Handle, err error) = kernel32.OpenMutexW
- //sys ReleaseMutex(mutex Handle) (err error) = kernel32.ReleaseMutex
- //sys SleepEx(milliseconds uint32, alertable bool) (ret uint32) = kernel32.SleepEx
- //sys CreateJobObject(jobAttr *SecurityAttributes, name *uint16) (handle Handle, err error) = kernel32.CreateJobObjectW
- //sys AssignProcessToJobObject(job Handle, process Handle) (err error) = kernel32.AssignProcessToJobObject
- //sys TerminateJobObject(job Handle, exitCode uint32) (err error) = kernel32.TerminateJobObject
- //sys SetErrorMode(mode uint32) (ret uint32) = kernel32.SetErrorMode
- //sys ResumeThread(thread Handle) (ret uint32, err error) [failretval==0xffffffff] = kernel32.ResumeThread
- //sys SetPriorityClass(process Handle, priorityClass uint32) (err error) = kernel32.SetPriorityClass
- //sys GetPriorityClass(process Handle) (ret uint32, err error) = kernel32.GetPriorityClass
- //sys QueryInformationJobObject(job Handle, JobObjectInformationClass int32, JobObjectInformation uintptr, JobObjectInformationLength uint32, retlen *uint32) (err error) = kernel32.QueryInformationJobObject
- //sys SetInformationJobObject(job Handle, JobObjectInformationClass uint32, JobObjectInformation uintptr, JobObjectInformationLength uint32) (ret int, err error)
- //sys GenerateConsoleCtrlEvent(ctrlEvent uint32, processGroupID uint32) (err error)
- //sys GetProcessId(process Handle) (id uint32, err error)
- //sys QueryFullProcessImageName(proc Handle, flags uint32, exeName *uint16, size *uint32) (err error) = kernel32.QueryFullProcessImageNameW
- //sys OpenThread(desiredAccess uint32, inheritHandle bool, threadId uint32) (handle Handle, err error)
- //sys SetProcessPriorityBoost(process Handle, disable bool) (err error) = kernel32.SetProcessPriorityBoost
- //sys GetProcessWorkingSetSizeEx(hProcess Handle, lpMinimumWorkingSetSize *uintptr, lpMaximumWorkingSetSize *uintptr, flags *uint32)
- //sys SetProcessWorkingSetSizeEx(hProcess Handle, dwMinimumWorkingSetSize uintptr, dwMaximumWorkingSetSize uintptr, flags uint32) (err error)
- //sys GetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
- //sys SetCommTimeouts(handle Handle, timeouts *CommTimeouts) (err error)
- // Volume Management Functions
- //sys DefineDosDevice(flags uint32, deviceName *uint16, targetPath *uint16) (err error) = DefineDosDeviceW
- //sys DeleteVolumeMountPoint(volumeMountPoint *uint16) (err error) = DeleteVolumeMountPointW
- //sys FindFirstVolume(volumeName *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeW
- //sys FindFirstVolumeMountPoint(rootPathName *uint16, volumeMountPoint *uint16, bufferLength uint32) (handle Handle, err error) [failretval==InvalidHandle] = FindFirstVolumeMountPointW
- //sys FindNextVolume(findVolume Handle, volumeName *uint16, bufferLength uint32) (err error) = FindNextVolumeW
- //sys FindNextVolumeMountPoint(findVolumeMountPoint Handle, volumeMountPoint *uint16, bufferLength uint32) (err error) = FindNextVolumeMountPointW
- //sys FindVolumeClose(findVolume Handle) (err error)
- //sys FindVolumeMountPointClose(findVolumeMountPoint Handle) (err error)
- //sys GetDiskFreeSpaceEx(directoryName *uint16, freeBytesAvailableToCaller *uint64, totalNumberOfBytes *uint64, totalNumberOfFreeBytes *uint64) (err error) = GetDiskFreeSpaceExW
- //sys GetDriveType(rootPathName *uint16) (driveType uint32) = GetDriveTypeW
- //sys GetLogicalDrives() (drivesBitMask uint32, err error) [failretval==0]
- //sys GetLogicalDriveStrings(bufferLength uint32, buffer *uint16) (n uint32, err error) [failretval==0] = GetLogicalDriveStringsW
- //sys GetVolumeInformation(rootPathName *uint16, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationW
- //sys GetVolumeInformationByHandle(file Handle, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationByHandleW
- //sys GetVolumeNameForVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16, bufferlength uint32) (err error) = GetVolumeNameForVolumeMountPointW
- //sys GetVolumePathName(fileName *uint16, volumePathName *uint16, bufferLength uint32) (err error) = GetVolumePathNameW
- //sys GetVolumePathNamesForVolumeName(volumeName *uint16, volumePathNames *uint16, bufferLength uint32, returnLength *uint32) (err error) = GetVolumePathNamesForVolumeNameW
- //sys QueryDosDevice(deviceName *uint16, targetPath *uint16, max uint32) (n uint32, err error) [failretval==0] = QueryDosDeviceW
- //sys SetVolumeLabel(rootPathName *uint16, volumeName *uint16) (err error) = SetVolumeLabelW
- //sys SetVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16) (err error) = SetVolumeMountPointW
- //sys InitiateSystemShutdownEx(machineName *uint16, message *uint16, timeout uint32, forceAppsClosed bool, rebootAfterShutdown bool, reason uint32) (err error) = advapi32.InitiateSystemShutdownExW
- //sys SetProcessShutdownParameters(level uint32, flags uint32) (err error) = kernel32.SetProcessShutdownParameters
- //sys GetProcessShutdownParameters(level *uint32, flags *uint32) (err error) = kernel32.GetProcessShutdownParameters
- //sys clsidFromString(lpsz *uint16, pclsid *GUID) (ret error) = ole32.CLSIDFromString
- //sys stringFromGUID2(rguid *GUID, lpsz *uint16, cchMax int32) (chars int32) = ole32.StringFromGUID2
- //sys coCreateGuid(pguid *GUID) (ret error) = ole32.CoCreateGuid
- //sys CoTaskMemFree(address unsafe.Pointer) = ole32.CoTaskMemFree
- //sys CoInitializeEx(reserved uintptr, coInit uint32) (ret error) = ole32.CoInitializeEx
- //sys CoUninitialize() = ole32.CoUninitialize
- //sys CoGetObject(name *uint16, bindOpts *BIND_OPTS3, guid *GUID, functionTable **uintptr) (ret error) = ole32.CoGetObject
- //sys getProcessPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetProcessPreferredUILanguages
- //sys getThreadPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetThreadPreferredUILanguages
- //sys getUserPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetUserPreferredUILanguages
- //sys getSystemPreferredUILanguages(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) (err error) = kernel32.GetSystemPreferredUILanguages
- //sys findResource(module Handle, name uintptr, resType uintptr) (resInfo Handle, err error) = kernel32.FindResourceW
- //sys SizeofResource(module Handle, resInfo Handle) (size uint32, err error) = kernel32.SizeofResource
- //sys LoadResource(module Handle, resInfo Handle) (resData Handle, err error) = kernel32.LoadResource
- //sys LockResource(resData Handle) (addr uintptr, err error) = kernel32.LockResource
- // Process Status API (PSAPI)
- //sys EnumProcesses(processIds []uint32, bytesReturned *uint32) (err error) = psapi.EnumProcesses
- //sys EnumProcessModules(process Handle, module *Handle, cb uint32, cbNeeded *uint32) (err error) = psapi.EnumProcessModules
- //sys EnumProcessModulesEx(process Handle, module *Handle, cb uint32, cbNeeded *uint32, filterFlag uint32) (err error) = psapi.EnumProcessModulesEx
- //sys GetModuleInformation(process Handle, module Handle, modinfo *ModuleInfo, cb uint32) (err error) = psapi.GetModuleInformation
- //sys GetModuleFileNameEx(process Handle, module Handle, filename *uint16, size uint32) (err error) = psapi.GetModuleFileNameExW
- //sys GetModuleBaseName(process Handle, module Handle, baseName *uint16, size uint32) (err error) = psapi.GetModuleBaseNameW
- // NT Native APIs
- //sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb
- //sys rtlGetVersion(info *OsVersionInfoEx) (ntstatus error) = ntdll.RtlGetVersion
- //sys rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) = ntdll.RtlGetNtVersionNumbers
- //sys RtlGetCurrentPeb() (peb *PEB) = ntdll.RtlGetCurrentPeb
- //sys RtlInitUnicodeString(destinationString *NTUnicodeString, sourceString *uint16) = ntdll.RtlInitUnicodeString
- //sys RtlInitString(destinationString *NTString, sourceString *byte) = ntdll.RtlInitString
- //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
- //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
- //sys RtlDosPathNameToNtPathName(dosName *uint16, ntName *NTUnicodeString, ntFileNamePart *uint16, relativeName *RTL_RELATIVE_NAME) (ntstatus error) = ntdll.RtlDosPathNameToNtPathName_U_WithStatus
- //sys RtlDosPathNameToRelativeNtPathName(dosName *uint16, ntName *NTUnicodeString, ntFileNamePart *uint16, relativeName *RTL_RELATIVE_NAME) (ntstatus error) = ntdll.RtlDosPathNameToRelativeNtPathName_U_WithStatus
- //sys RtlDefaultNpAcl(acl **ACL) (ntstatus error) = ntdll.RtlDefaultNpAcl
- //sys NtQueryInformationProcess(proc Handle, procInfoClass int32, procInfo unsafe.Pointer, procInfoLen uint32, retLen *uint32) (ntstatus error) = ntdll.NtQueryInformationProcess
- //sys NtSetInformationProcess(proc Handle, procInfoClass int32, procInfo unsafe.Pointer, procInfoLen uint32) (ntstatus error) = ntdll.NtSetInformationProcess
- // syscall interface implementation for other packages
- // GetCurrentProcess returns the handle for the current process.
- // It is a pseudo handle that does not need to be closed.
- // The returned error is always nil.
- //
- // Deprecated: use CurrentProcess for the same Handle without the nil
- // error.
- func GetCurrentProcess() (Handle, error) {
- return CurrentProcess(), nil
- }
- // CurrentProcess returns the handle for the current process.
- // It is a pseudo handle that does not need to be closed.
- func CurrentProcess() Handle { return Handle(^uintptr(1 - 1)) }
- // GetCurrentThread returns the handle for the current thread.
- // It is a pseudo handle that does not need to be closed.
- // The returned error is always nil.
- //
- // Deprecated: use CurrentThread for the same Handle without the nil
- // error.
- func GetCurrentThread() (Handle, error) {
- return CurrentThread(), nil
- }
- // CurrentThread returns the handle for the current thread.
- // It is a pseudo handle that does not need to be closed.
- func CurrentThread() Handle { return Handle(^uintptr(2 - 1)) }
- // GetProcAddressByOrdinal retrieves the address of the exported
- // function from module by ordinal.
- func GetProcAddressByOrdinal(module Handle, ordinal uintptr) (proc uintptr, err error) {
- r0, _, e1 := syscall.Syscall(procGetProcAddress.Addr(), 2, uintptr(module), ordinal, 0)
- proc = uintptr(r0)
- if proc == 0 {
- err = errnoErr(e1)
- }
- return
- }
- func Exit(code int) { ExitProcess(uint32(code)) }
- func makeInheritSa() *SecurityAttributes {
- var sa SecurityAttributes
- sa.Length = uint32(unsafe.Sizeof(sa))
- sa.InheritHandle = 1
- return &sa
- }
- func Open(path string, mode int, perm uint32) (fd Handle, err error) {
- if len(path) == 0 {
- return InvalidHandle, ERROR_FILE_NOT_FOUND
- }
- pathp, err := UTF16PtrFromString(path)
- if err != nil {
- return InvalidHandle, err
- }
- var access uint32
- switch mode & (O_RDONLY | O_WRONLY | O_RDWR) {
- case O_RDONLY:
- access = GENERIC_READ
- case O_WRONLY:
- access = GENERIC_WRITE
- case O_RDWR:
- access = GENERIC_READ | GENERIC_WRITE
- }
- if mode&O_CREAT != 0 {
- access |= GENERIC_WRITE
- }
- if mode&O_APPEND != 0 {
- access &^= GENERIC_WRITE
- access |= FILE_APPEND_DATA
- }
- sharemode := uint32(FILE_SHARE_READ | FILE_SHARE_WRITE)
- var sa *SecurityAttributes
- if mode&O_CLOEXEC == 0 {
- sa = makeInheritSa()
- }
- var createmode uint32
- switch {
- case mode&(O_CREAT|O_EXCL) == (O_CREAT | O_EXCL):
- createmode = CREATE_NEW
- case mode&(O_CREAT|O_TRUNC) == (O_CREAT | O_TRUNC):
- createmode = CREATE_ALWAYS
- case mode&O_CREAT == O_CREAT:
- createmode = OPEN_ALWAYS
- case mode&O_TRUNC == O_TRUNC:
- createmode = TRUNCATE_EXISTING
- default:
- createmode = OPEN_EXISTING
- }
- var attrs uint32 = FILE_ATTRIBUTE_NORMAL
- if perm&S_IWRITE == 0 {
- attrs = FILE_ATTRIBUTE_READONLY
- }
- h, e := CreateFile(pathp, access, sharemode, sa, createmode, attrs, 0)
- return h, e
- }
- func Read(fd Handle, p []byte) (n int, err error) {
- var done uint32
- e := ReadFile(fd, p, &done, nil)
- if e != nil {
- if e == ERROR_BROKEN_PIPE {
- // NOTE(brainman): work around ERROR_BROKEN_PIPE is returned on reading EOF from stdin
- return 0, nil
- }
- return 0, e
- }
- if raceenabled {
- if done > 0 {
- raceWriteRange(unsafe.Pointer(&p[0]), int(done))
- }
- raceAcquire(unsafe.Pointer(&ioSync))
- }
- return int(done), nil
- }
- func Write(fd Handle, p []byte) (n int, err error) {
- if raceenabled {
- raceReleaseMerge(unsafe.Pointer(&ioSync))
- }
- var done uint32
- e := WriteFile(fd, p, &done, nil)
- if e != nil {
- return 0, e
- }
- if raceenabled && done > 0 {
- raceReadRange(unsafe.Pointer(&p[0]), int(done))
- }
- return int(done), nil
- }
- var ioSync int64
- func Seek(fd Handle, offset int64, whence int) (newoffset int64, err error) {
- var w uint32
- switch whence {
- case 0:
- w = FILE_BEGIN
- case 1:
- w = FILE_CURRENT
- case 2:
- w = FILE_END
- }
- hi := int32(offset >> 32)
- lo := int32(offset)
- // use GetFileType to check pipe, pipe can't do seek
- ft, _ := GetFileType(fd)
- if ft == FILE_TYPE_PIPE {
- return 0, syscall.EPIPE
- }
- rlo, e := SetFilePointer(fd, lo, &hi, w)
- if e != nil {
- return 0, e
- }
- return int64(hi)<<32 + int64(rlo), nil
- }
- func Close(fd Handle) (err error) {
- return CloseHandle(fd)
- }
- var (
- Stdin = getStdHandle(STD_INPUT_HANDLE)
- Stdout = getStdHandle(STD_OUTPUT_HANDLE)
- Stderr = getStdHandle(STD_ERROR_HANDLE)
- )
- func getStdHandle(stdhandle uint32) (fd Handle) {
- r, _ := GetStdHandle(stdhandle)
- CloseOnExec(r)
- return r
- }
- const ImplementsGetwd = true
- func Getwd() (wd string, err error) {
- b := make([]uint16, 300)
- n, e := GetCurrentDirectory(uint32(len(b)), &b[0])
- if e != nil {
- return "", e
- }
- return string(utf16.Decode(b[0:n])), nil
- }
- func Chdir(path string) (err error) {
- pathp, err := UTF16PtrFromString(path)
- if err != nil {
- return err
- }
- return SetCurrentDirectory(pathp)
- }
- func Mkdir(path string, mode uint32) (err error) {
- pathp, err := UTF16PtrFromString(path)
- if err != nil {
- return err
- }
- return CreateDirectory(pathp, nil)
- }
- func Rmdir(path string) (err error) {
- pathp, err := UTF16PtrFromString(path)
- if err != nil {
- return err
- }
- return RemoveDirectory(pathp)
- }
- func Unlink(path string) (err error) {
- pathp, err := UTF16PtrFromString(path)
- if err != nil {
- return err
- }
- return DeleteFile(pathp)
- }
- func Rename(oldpath, newpath string) (err error) {
- from, err := UTF16PtrFromString(oldpath)
- if err != nil {
- return err
- }
- to, err := UTF16PtrFromString(newpath)
- if err != nil {
- return err
- }
- return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING)
- }
- func ComputerName() (name string, err error) {
- var n uint32 = MAX_COMPUTERNAME_LENGTH + 1
- b := make([]uint16, n)
- e := GetComputerName(&b[0], &n)
- if e != nil {
- return "", e
- }
- return string(utf16.Decode(b[0:n])), nil
- }
- func DurationSinceBoot() time.Duration {
- return time.Duration(getTickCount64()) * time.Millisecond
- }
- func Ftruncate(fd Handle, length int64) (err error) {
- curoffset, e := Seek(fd, 0, 1)
- if e != nil {
- return e
- }
- defer Seek(fd, curoffset, 0)
- _, e = Seek(fd, length, 0)
- if e != nil {
- return e
- }
- e = SetEndOfFile(fd)
- if e != nil {
- return e
- }
- return nil
- }
- func Gettimeofday(tv *Timeval) (err error) {
- var ft Filetime
- GetSystemTimeAsFileTime(&ft)
- *tv = NsecToTimeval(ft.Nanoseconds())
- return nil
- }
- func Pipe(p []Handle) (err error) {
- if len(p) != 2 {
- return syscall.EINVAL
- }
- var r, w Handle
- e := CreatePipe(&r, &w, makeInheritSa(), 0)
- if e != nil {
- return e
- }
- p[0] = r
- p[1] = w
- return nil
- }
- func Utimes(path string, tv []Timeval) (err error) {
- if len(tv) != 2 {
- return syscall.EINVAL
- }
- pathp, e := UTF16PtrFromString(path)
- if e != nil {
- return e
- }
- h, e := CreateFile(pathp,
- FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
- OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
- if e != nil {
- return e
- }
- defer Close(h)
- a := NsecToFiletime(tv[0].Nanoseconds())
- w := NsecToFiletime(tv[1].Nanoseconds())
- return SetFileTime(h, nil, &a, &w)
- }
- func UtimesNano(path string, ts []Timespec) (err error) {
- if len(ts) != 2 {
- return syscall.EINVAL
- }
- pathp, e := UTF16PtrFromString(path)
- if e != nil {
- return e
- }
- h, e := CreateFile(pathp,
- FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, nil,
- OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0)
- if e != nil {
- return e
- }
- defer Close(h)
- a := NsecToFiletime(TimespecToNsec(ts[0]))
- w := NsecToFiletime(TimespecToNsec(ts[1]))
- return SetFileTime(h, nil, &a, &w)
- }
- func Fsync(fd Handle) (err error) {
- return FlushFileBuffers(fd)
- }
- func Chmod(path string, mode uint32) (err error) {
- p, e := UTF16PtrFromString(path)
- if e != nil {
- return e
- }
- attrs, e := GetFileAttributes(p)
- if e != nil {
- return e
- }
- if mode&S_IWRITE != 0 {
- attrs &^= FILE_ATTRIBUTE_READONLY
- } else {
- attrs |= FILE_ATTRIBUTE_READONLY
- }
- return SetFileAttributes(p, attrs)
- }
- func LoadGetSystemTimePreciseAsFileTime() error {
- return procGetSystemTimePreciseAsFileTime.Find()
- }
- func LoadCancelIoEx() error {
- return procCancelIoEx.Find()
- }
- func LoadSetFileCompletionNotificationModes() error {
- return procSetFileCompletionNotificationModes.Find()
- }
- func WaitForMultipleObjects(handles []Handle, waitAll bool, waitMilliseconds uint32) (event uint32, err error) {
- // Every other win32 array API takes arguments as "pointer, count", except for this function. So we
- // can't declare it as a usual [] type, because mksyscall will use the opposite order. We therefore
- // trivially stub this ourselves.
- var handlePtr *Handle
- if len(handles) > 0 {
- handlePtr = &handles[0]
- }
- return waitForMultipleObjects(uint32(len(handles)), uintptr(unsafe.Pointer(handlePtr)), waitAll, waitMilliseconds)
- }
- // net api calls
- const socket_error = uintptr(^uint32(0))
- //sys WSAStartup(verreq uint32, data *WSAData) (sockerr error) = ws2_32.WSAStartup
- //sys WSACleanup() (err error) [failretval==socket_error] = ws2_32.WSACleanup
- //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
- //sys socket(af int32, typ int32, protocol int32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.socket
- //sys sendto(s Handle, buf []byte, flags int32, to unsafe.Pointer, tolen int32) (err error) [failretval==socket_error] = ws2_32.sendto
- //sys recvfrom(s Handle, buf []byte, flags int32, from *RawSockaddrAny, fromlen *int32) (n int32, err error) [failretval==-1] = ws2_32.recvfrom
- //sys Setsockopt(s Handle, level int32, optname int32, optval *byte, optlen int32) (err error) [failretval==socket_error] = ws2_32.setsockopt
- //sys Getsockopt(s Handle, level int32, optname int32, optval *byte, optlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockopt
- //sys bind(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.bind
- //sys connect(s Handle, name unsafe.Pointer, namelen int32) (err error) [failretval==socket_error] = ws2_32.connect
- //sys getsockname(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getsockname
- //sys getpeername(s Handle, rsa *RawSockaddrAny, addrlen *int32) (err error) [failretval==socket_error] = ws2_32.getpeername
- //sys listen(s Handle, backlog int32) (err error) [failretval==socket_error] = ws2_32.listen
- //sys shutdown(s Handle, how int32) (err error) [failretval==socket_error] = ws2_32.shutdown
- //sys Closesocket(s Handle) (err error) [failretval==socket_error] = ws2_32.closesocket
- //sys AcceptEx(ls Handle, as Handle, buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, recvd *uint32, overlapped *Overlapped) (err error) = mswsock.AcceptEx
- //sys GetAcceptExSockaddrs(buf *byte, rxdatalen uint32, laddrlen uint32, raddrlen uint32, lrsa **RawSockaddrAny, lrsalen *int32, rrsa **RawSockaddrAny, rrsalen *int32) = mswsock.GetAcceptExSockaddrs
- //sys WSARecv(s Handle, bufs *WSABuf, bufcnt uint32, recvd *uint32, flags *uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSARecv
- //sys WSASend(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, overlapped *Overlapped, croutine *byte) (err error) [failretval==socket_error] = ws2_32.WSASend
- //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
- //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
- //sys WSASocket(af int32, typ int32, protocol int32, protoInfo *WSAProtocolInfo, group uint32, flags uint32) (handle Handle, err error) [failretval==InvalidHandle] = ws2_32.WSASocketW
- //sys GetHostByName(name string) (h *Hostent, err error) [failretval==nil] = ws2_32.gethostbyname
- //sys GetServByName(name string, proto string) (s *Servent, err error) [failretval==nil] = ws2_32.getservbyname
- //sys Ntohs(netshort uint16) (u uint16) = ws2_32.ntohs
- //sys GetProtoByName(name string) (p *Protoent, err error) [failretval==nil] = ws2_32.getprotobyname
- //sys DnsQuery(name string, qtype uint16, options uint32, extra *byte, qrs **DNSRecord, pr *byte) (status error) = dnsapi.DnsQuery_W
- //sys DnsRecordListFree(rl *DNSRecord, freetype uint32) = dnsapi.DnsRecordListFree
- //sys DnsNameCompare(name1 *uint16, name2 *uint16) (same bool) = dnsapi.DnsNameCompare_W
- //sys GetAddrInfoW(nodename *uint16, servicename *uint16, hints *AddrinfoW, result **AddrinfoW) (sockerr error) = ws2_32.GetAddrInfoW
- //sys FreeAddrInfoW(addrinfo *AddrinfoW) = ws2_32.FreeAddrInfoW
- //sys GetIfEntry(pIfRow *MibIfRow) (errcode error) = iphlpapi.GetIfEntry
- //sys GetAdaptersInfo(ai *IpAdapterInfo, ol *uint32) (errcode error) = iphlpapi.GetAdaptersInfo
- //sys SetFileCompletionNotificationModes(handle Handle, flags uint8) (err error) = kernel32.SetFileCompletionNotificationModes
- //sys WSAEnumProtocols(protocols *int32, protocolBuffer *WSAProtocolInfo, bufferLength *uint32) (n int32, err error) [failretval==-1] = ws2_32.WSAEnumProtocolsW
- //sys WSAGetOverlappedResult(h Handle, o *Overlapped, bytes *uint32, wait bool, flags *uint32) (err error) = ws2_32.WSAGetOverlappedResult
- //sys GetAdaptersAddresses(family uint32, flags uint32, reserved uintptr, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
- //sys GetACP() (acp uint32) = kernel32.GetACP
- //sys MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
- // For testing: clients can set this flag to force
- // creation of IPv6 sockets to return EAFNOSUPPORT.
- var SocketDisableIPv6 bool
- type RawSockaddrInet4 struct {
- Family uint16
- Port uint16
- Addr [4]byte /* in_addr */
- Zero [8]uint8
- }
- type RawSockaddrInet6 struct {
- Family uint16
- Port uint16
- Flowinfo uint32
- Addr [16]byte /* in6_addr */
- Scope_id uint32
- }
- type RawSockaddr struct {
- Family uint16
- Data [14]int8
- }
- type RawSockaddrAny struct {
- Addr RawSockaddr
- Pad [100]int8
- }
- type Sockaddr interface {
- sockaddr() (ptr unsafe.Pointer, len int32, err error) // lowercase; only we can define Sockaddrs
- }
- type SockaddrInet4 struct {
- Port int
- Addr [4]byte
- raw RawSockaddrInet4
- }
- func (sa *SockaddrInet4) sockaddr() (unsafe.Pointer, int32, error) {
- if sa.Port < 0 || sa.Port > 0xFFFF {
- return nil, 0, syscall.EINVAL
- }
- sa.raw.Family = AF_INET
- p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
- p[0] = byte(sa.Port >> 8)
- p[1] = byte(sa.Port)
- for i := 0; i < len(sa.Addr); i++ {
- sa.raw.Addr[i] = sa.Addr[i]
- }
- return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil
- }
- type SockaddrInet6 struct {
- Port int
- ZoneId uint32
- Addr [16]byte
- raw RawSockaddrInet6
- }
- func (sa *SockaddrInet6) sockaddr() (unsafe.Pointer, int32, error) {
- if sa.Port < 0 || sa.Port > 0xFFFF {
- return nil, 0, syscall.EINVAL
- }
- sa.raw.Family = AF_INET6
- p := (*[2]byte)(unsafe.Pointer(&sa.raw.Port))
- p[0] = byte(sa.Port >> 8)
- p[1] = byte(sa.Port)
- sa.raw.Scope_id = sa.ZoneId
- for i := 0; i < len(sa.Addr); i++ {
- sa.raw.Addr[i] = sa.Addr[i]
- }
- return unsafe.Pointer(&sa.raw), int32(unsafe.Sizeof(sa.raw)), nil
- }
- type RawSockaddrUnix struct {
- Family uint16
- Path [UNIX_PATH_MAX]int8
- }
- type SockaddrUnix struct {
- Name string
- raw RawSockaddrUnix
- }
- func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) {
- name := sa.Name
- n := len(name)
- if n > len(sa.raw.Path) {
- return nil, 0, syscall.EINVAL
- }
- if n == len(sa.raw.Path) && name[0] != '@' {
- return nil, 0, syscall.EINVAL
- }
- sa.raw.Family = AF_UNIX
- for i := 0; i < n; i++ {
- sa.raw.Path[i] = int8(name[i])
- }
- // length is family (uint16), name, NUL.
- sl := int32(2)
- if n > 0 {
- sl += int32(n) + 1
- }
- if sa.raw.Path[0] == '@' {
- sa.raw.Path[0] = 0
- // Don't count trailing NUL for abstract address.
- sl--
- }
- return unsafe.Pointer(&sa.raw), sl, nil
- }
- func (rsa *RawSockaddrAny) Sockaddr() (Sockaddr, error) {
- switch rsa.Addr.Family {
- case AF_UNIX:
- pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa))
- sa := new(SockaddrUnix)
- if pp.Path[0] == 0 {
- // "Abstract" Unix domain socket.
- // Rewrite leading NUL as @ for textual display.
- // (This is the standard convention.)
- // Not friendly to overwrite in place,
- // but the callers below don't care.
- pp.Path[0] = '@'
- }
- // Assume path ends at NUL.
- // This is not technically the Linux semantics for
- // abstract Unix domain sockets--they are supposed
- // to be uninterpreted fixed-size binary blobs--but
- // everyone uses this convention.
- n := 0
- for n < len(pp.Path) && pp.Path[n] != 0 {
- n++
- }
- bytes := (*[len(pp.Path)]byte)(unsafe.Pointer(&pp.Path[0]))[0:n]
- sa.Name = string(bytes)
- return sa, nil
- case AF_INET:
- pp := (*RawSockaddrInet4)(unsafe.Pointer(rsa))
- sa := new(SockaddrInet4)
- p := (*[2]byte)(unsafe.Pointer(&pp.Port))
- sa.Port = int(p[0])<<8 + int(p[1])
- for i := 0; i < len(sa.Addr); i++ {
- sa.Addr[i] = pp.Addr[i]
- }
- return sa, nil
- case AF_INET6:
- pp := (*RawSockaddrInet6)(unsafe.Pointer(rsa))
- sa := new(SockaddrInet6)
- p := (*[2]byte)(unsafe.Pointer(&pp.Port))
- sa.Port = int(p[0])<<8 + int(p[1])
- sa.ZoneId = pp.Scope_id
- for i := 0; i < len(sa.Addr); i++ {
- sa.Addr[i] = pp.Addr[i]
- }
- return sa, nil
- }
- return nil, syscall.EAFNOSUPPORT
- }
- func Socket(domain, typ, proto int) (fd Handle, err error) {
- if domain == AF_INET6 && SocketDisableIPv6 {
- return InvalidHandle, syscall.EAFNOSUPPORT
- }
- return socket(int32(domain), int32(typ), int32(proto))
- }
- func SetsockoptInt(fd Handle, level, opt int, value int) (err error) {
- v := int32(value)
- return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&v)), int32(unsafe.Sizeof(v)))
- }
- func Bind(fd Handle, sa Sockaddr) (err error) {
- ptr, n, err := sa.sockaddr()
- if err != nil {
- return err
- }
- return bind(fd, ptr, n)
- }
- func Connect(fd Handle, sa Sockaddr) (err error) {
- ptr, n, err := sa.sockaddr()
- if err != nil {
- return err
- }
- return connect(fd, ptr, n)
- }
- func Getsockname(fd Handle) (sa Sockaddr, err error) {
- var rsa RawSockaddrAny
- l := int32(unsafe.Sizeof(rsa))
- if err = getsockname(fd, &rsa, &l); err != nil {
- return
- }
- return rsa.Sockaddr()
- }
- func Getpeername(fd Handle) (sa Sockaddr, err error) {
- var rsa RawSockaddrAny
- l := int32(unsafe.Sizeof(rsa))
- if err = getpeername(fd, &rsa, &l); err != nil {
- return
- }
- return rsa.Sockaddr()
- }
- func Listen(s Handle, n int) (err error) {
- return listen(s, int32(n))
- }
- func Shutdown(fd Handle, how int) (err error) {
- return shutdown(fd, int32(how))
- }
- func WSASendto(s Handle, bufs *WSABuf, bufcnt uint32, sent *uint32, flags uint32, to Sockaddr, overlapped *Overlapped, croutine *byte) (err error) {
- rsa, l, err := to.sockaddr()
- if err != nil {
- return err
- }
- return WSASendTo(s, bufs, bufcnt, sent, flags, (*RawSockaddrAny)(unsafe.Pointer(rsa)), l, overlapped, croutine)
- }
- func LoadGetAddrInfo() error {
- return procGetAddrInfoW.Find()
- }
- var connectExFunc struct {
- once sync.Once
- addr uintptr
- err error
- }
- func LoadConnectEx() error {
- connectExFunc.once.Do(func() {
- var s Handle
- s, connectExFunc.err = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)
- if connectExFunc.err != nil {
- return
- }
- defer CloseHandle(s)
- var n uint32
- connectExFunc.err = WSAIoctl(s,
- SIO_GET_EXTENSION_FUNCTION_POINTER,
- (*byte)(unsafe.Pointer(&WSAID_CONNECTEX)),
- uint32(unsafe.Sizeof(WSAID_CONNECTEX)),
- (*byte)(unsafe.Pointer(&connectExFunc.addr)),
- uint32(unsafe.Sizeof(connectExFunc.addr)),
- &n, nil, 0)
- })
- return connectExFunc.err
- }
- func connectEx(s Handle, name unsafe.Pointer, namelen int32, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) (err error) {
- 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)
- if r1 == 0 {
- if e1 != 0 {
- err = error(e1)
- } else {
- err = syscall.EINVAL
- }
- }
- return
- }
- func ConnectEx(fd Handle, sa Sockaddr, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) error {
- err := LoadConnectEx()
- if err != nil {
- return errorspkg.New("failed to find ConnectEx: " + err.Error())
- }
- ptr, n, err := sa.sockaddr()
- if err != nil {
- return err
- }
- return connectEx(fd, ptr, n, sendBuf, sendDataLen, bytesSent, overlapped)
- }
- var sendRecvMsgFunc struct {
- once sync.Once
- sendAddr uintptr
- recvAddr uintptr
- err error
- }
- func loadWSASendRecvMsg() error {
- sendRecvMsgFunc.once.Do(func() {
- var s Handle
- s, sendRecvMsgFunc.err = Socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
- if sendRecvMsgFunc.err != nil {
- return
- }
- defer CloseHandle(s)
- var n uint32
- sendRecvMsgFunc.err = WSAIoctl(s,
- SIO_GET_EXTENSION_FUNCTION_POINTER,
- (*byte)(unsafe.Pointer(&WSAID_WSARECVMSG)),
- uint32(unsafe.Sizeof(WSAID_WSARECVMSG)),
- (*byte)(unsafe.Pointer(&sendRecvMsgFunc.recvAddr)),
- uint32(unsafe.Sizeof(sendRecvMsgFunc.recvAddr)),
- &n, nil, 0)
- if sendRecvMsgFunc.err != nil {
- return
- }
- sendRecvMsgFunc.err = WSAIoctl(s,
- SIO_GET_EXTENSION_FUNCTION_POINTER,
- (*byte)(unsafe.Pointer(&WSAID_WSASENDMSG)),
- uint32(unsafe.Sizeof(WSAID_WSASENDMSG)),
- (*byte)(unsafe.Pointer(&sendRecvMsgFunc.sendAddr)),
- uint32(unsafe.Sizeof(sendRecvMsgFunc.sendAddr)),
- &n, nil, 0)
- })
- return sendRecvMsgFunc.err
- }
- func WSASendMsg(fd Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlapped *Overlapped, croutine *byte) error {
- err := loadWSASendRecvMsg()
- if err != nil {
- return err
- }
- 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)))
- if r1 == socket_error {
- err = errnoErr(e1)
- }
- return err
- }
- func WSARecvMsg(fd Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *Overlapped, croutine *byte) error {
- err := loadWSASendRecvMsg()
- if err != nil {
- return err
- }
- 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)
- if r1 == socket_error {
- err = errnoErr(e1)
- }
- return err
- }
- // Invented structures to support what package os expects.
- type Rusage struct {
- CreationTime Filetime
- ExitTime Filetime
- KernelTime Filetime
- UserTime Filetime
- }
- 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
- // consistency with the corresponding package for other operating systems.
- type Timespec struct {
- Sec int64
- Nsec int64
- }
- func TimespecToNsec(ts Timespec) int64 { return int64(ts.Sec)*1e9 + int64(ts.Nsec) }
- func NsecToTimespec(nsec int64) (ts Timespec) {
- ts.Sec = nsec / 1e9
- ts.Nsec = nsec % 1e9
- return
- }
- // 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) {
- var rsa RawSockaddrAny
- l := int32(unsafe.Sizeof(rsa))
- n32, err := recvfrom(fd, p, int32(flags), &rsa, &l)
- n = int(n32)
- if err != nil {
- return
- }
- from, err = rsa.Sockaddr()
- return
- }
- func Sendto(fd Handle, p []byte, flags int, to Sockaddr) (err error) {
- ptr, l, err := to.sockaddr()
- if err != nil {
- return err
- }
- return sendto(fd, p, int32(flags), ptr, l)
- }
- 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.
- // sysLinger is the real system call structure.
- // BUG(brainman): The definition of Linger is not appropriate for direct use
- // with Setsockopt and Getsockopt.
- // Use SetsockoptLinger instead.
- type Linger struct {
- Onoff int32
- Linger int32
- }
- type sysLinger struct {
- Onoff uint16
- Linger uint16
- }
- type IPMreq struct {
- Multiaddr [4]byte /* in_addr */
- Interface [4]byte /* in_addr */
- }
- type IPv6Mreq struct {
- Multiaddr [16]byte /* in6_addr */
- Interface uint32
- }
- func GetsockoptInt(fd Handle, level, opt int) (int, error) {
- v := int32(0)
- l := int32(unsafe.Sizeof(v))
- err := Getsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&v)), &l)
- return int(v), err
- }
- func SetsockoptLinger(fd Handle, level, opt int, l *Linger) (err error) {
- sys := sysLinger{Onoff: uint16(l.Onoff), Linger: uint16(l.Linger)}
- return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&sys)), int32(unsafe.Sizeof(sys)))
- }
- func SetsockoptInet4Addr(fd Handle, level, opt int, value [4]byte) (err error) {
- return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(&value[0])), 4)
- }
- func SetsockoptIPMreq(fd Handle, level, opt int, mreq *IPMreq) (err error) {
- return Setsockopt(fd, int32(level), int32(opt), (*byte)(unsafe.Pointer(mreq)), int32(unsafe.Sizeof(*mreq)))
- }
- func SetsockoptIPv6Mreq(fd Handle, level, opt int, mreq *IPv6Mreq) (err error) {
- return syscall.EWINDOWS
- }
- func Getpid() (pid int) { return int(GetCurrentProcessId()) }
- func FindFirstFile(name *uint16, data *Win32finddata) (handle Handle, err error) {
- // NOTE(rsc): The Win32finddata struct is wrong for the system call:
- // the two paths are each one uint16 short. Use the correct struct,
- // a win32finddata1, and then copy the results out.
- // There is no loss of expressivity here, because the final
- // uint16, if it is used, is supposed to be a NUL, and Go doesn't need that.
- // For Go 1.1, we might avoid the allocation of win32finddata1 here
- // by adding a final Bug [2]uint16 field to the struct and then
- // adjusting the fields in the result directly.
- var data1 win32finddata1
- handle, err = findFirstFile1(name, &data1)
- if err == nil {
- copyFindData(data, &data1)
- }
- return
- }
- func FindNextFile(handle Handle, data *Win32finddata) (err error) {
- var data1 win32finddata1
- err = findNextFile1(handle, &data1)
- if err == nil {
- copyFindData(data, &data1)
- }
- return
- }
- func getProcessEntry(pid int) (*ProcessEntry32, error) {
- snapshot, err := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
- if err != nil {
- return nil, err
- }
- defer CloseHandle(snapshot)
- var procEntry ProcessEntry32
- procEntry.Size = uint32(unsafe.Sizeof(procEntry))
- if err = Process32First(snapshot, &procEntry); err != nil {
- return nil, err
- }
- for {
- if procEntry.ProcessID == uint32(pid) {
- return &procEntry, nil
- }
- err = Process32Next(snapshot, &procEntry)
- if err != nil {
- return nil, err
- }
- }
- }
- func Getppid() (ppid int) {
- pe, err := getProcessEntry(Getpid())
- if err != nil {
- return -1
- }
- return int(pe.ParentProcessID)
- }
- // TODO(brainman): fix all needed for os
- func Fchdir(fd Handle) (err error) { return syscall.EWINDOWS }
- 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 }
- 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 }
- func Getgroups() (gids []int, err error) { return nil, syscall.EWINDOWS }
- type Signal int
- func (s Signal) Signal() {}
- func (s Signal) String() string {
- if 0 <= s && int(s) < len(signals) {
- str := signals[s]
- if str != "" {
- return str
- }
- }
- return "signal " + itoa(int(s))
- }
- func LoadCreateSymbolicLink() error {
- return procCreateSymbolicLinkW.Find()
- }
- // Readlink returns the destination of the named symbolic link.
- func Readlink(path string, buf []byte) (n int, err error) {
- fd, err := CreateFile(StringToUTF16Ptr(path), GENERIC_READ, 0, nil, OPEN_EXISTING,
- FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, 0)
- if err != nil {
- return -1, err
- }
- defer CloseHandle(fd)
- rdbbuf := make([]byte, MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
- var bytesReturned uint32
- err = DeviceIoControl(fd, FSCTL_GET_REPARSE_POINT, nil, 0, &rdbbuf[0], uint32(len(rdbbuf)), &bytesReturned, nil)
- if err != nil {
- return -1, err
- }
- rdb := (*reparseDataBuffer)(unsafe.Pointer(&rdbbuf[0]))
- var s string
- switch rdb.ReparseTag {
- case IO_REPARSE_TAG_SYMLINK:
- data := (*symbolicLinkReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer))
- p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0]))
- s = UTF16ToString(p[data.PrintNameOffset/2 : (data.PrintNameLength-data.PrintNameOffset)/2])
- case IO_REPARSE_TAG_MOUNT_POINT:
- data := (*mountPointReparseBuffer)(unsafe.Pointer(&rdb.reparseBuffer))
- p := (*[0xffff]uint16)(unsafe.Pointer(&data.PathBuffer[0]))
- s = UTF16ToString(p[data.PrintNameOffset/2 : (data.PrintNameLength-data.PrintNameOffset)/2])
- default:
- // the path is not a symlink or junction but another type of reparse
- // point
- return -1, syscall.ENOENT
- }
- n = copy(buf, []byte(s))
- return n, nil
- }
- // GUIDFromString parses a string in the form of
- // "{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" into a GUID.
- func GUIDFromString(str string) (GUID, error) {
- guid := GUID{}
- str16, err := syscall.UTF16PtrFromString(str)
- if err != nil {
- return guid, err
- }
- err = clsidFromString(str16, &guid)
- if err != nil {
- return guid, err
- }
- return guid, nil
- }
- // GenerateGUID creates a new random GUID.
- func GenerateGUID() (GUID, error) {
- guid := GUID{}
- err := coCreateGuid(&guid)
- if err != nil {
- return guid, err
- }
- return guid, nil
- }
- // String returns the canonical string form of the GUID,
- // in the form of "{XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}".
- func (guid GUID) String() string {
- var str [100]uint16
- chars := stringFromGUID2(&guid, &str[0], int32(len(str)))
- if chars <= 1 {
- return ""
- }
- return string(utf16.Decode(str[:chars-1]))
- }
- // KnownFolderPath returns a well-known folder path for the current user, specified by one of
- // the FOLDERID_ constants, and chosen and optionally created based on a KF_ flag.
- func KnownFolderPath(folderID *KNOWNFOLDERID, flags uint32) (string, error) {
- return Token(0).KnownFolderPath(folderID, flags)
- }
- // KnownFolderPath returns a well-known folder path for the user token, specified by one of
- // the FOLDERID_ constants, and chosen and optionally created based on a KF_ flag.
- func (t Token) KnownFolderPath(folderID *KNOWNFOLDERID, flags uint32) (string, error) {
- var p *uint16
- err := shGetKnownFolderPath(folderID, flags, t, &p)
- if err != nil {
- return "", err
- }
- defer CoTaskMemFree(unsafe.Pointer(p))
- return UTF16PtrToString(p), nil
- }
- // RtlGetVersion returns the version of the underlying operating system, ignoring
- // manifest semantics but is affected by the application compatibility layer.
- func RtlGetVersion() *OsVersionInfoEx {
- info := &OsVersionInfoEx{}
- info.osVersionInfoSize = uint32(unsafe.Sizeof(*info))
- // According to documentation, this function always succeeds.
- // The function doesn't even check the validity of the
- // osVersionInfoSize member. Disassembling ntdll.dll indicates
- // that the documentation is indeed correct about that.
- _ = rtlGetVersion(info)
- return info
- }
- // RtlGetNtVersionNumbers returns the version of the underlying operating system,
- // ignoring manifest semantics and the application compatibility layer.
- func RtlGetNtVersionNumbers() (majorVersion, minorVersion, buildNumber uint32) {
- rtlGetNtVersionNumbers(&majorVersion, &minorVersion, &buildNumber)
- buildNumber &= 0xffff
- return
- }
- // GetProcessPreferredUILanguages retrieves the process preferred UI languages.
- func GetProcessPreferredUILanguages(flags uint32) ([]string, error) {
- return getUILanguages(flags, getProcessPreferredUILanguages)
- }
- // GetThreadPreferredUILanguages retrieves the thread preferred UI languages for the current thread.
- func GetThreadPreferredUILanguages(flags uint32) ([]string, error) {
- return getUILanguages(flags, getThreadPreferredUILanguages)
- }
- // GetUserPreferredUILanguages retrieves information about the user preferred UI languages.
- func GetUserPreferredUILanguages(flags uint32) ([]string, error) {
- return getUILanguages(flags, getUserPreferredUILanguages)
- }
- // GetSystemPreferredUILanguages retrieves the system preferred UI languages.
- func GetSystemPreferredUILanguages(flags uint32) ([]string, error) {
- return getUILanguages(flags, getSystemPreferredUILanguages)
- }
- func getUILanguages(flags uint32, f func(flags uint32, numLanguages *uint32, buf *uint16, bufSize *uint32) error) ([]string, error) {
- size := uint32(128)
- for {
- var numLanguages uint32
- buf := make([]uint16, size)
- err := f(flags, &numLanguages, &buf[0], &size)
- if err == ERROR_INSUFFICIENT_BUFFER {
- continue
- }
- if err != nil {
- return nil, err
- }
- buf = buf[:size]
- if numLanguages == 0 || len(buf) == 0 { // GetProcessPreferredUILanguages may return numLanguages==0 with "\0\0"
- return []string{}, nil
- }
- if buf[len(buf)-1] == 0 {
- buf = buf[:len(buf)-1] // remove terminating null
- }
- languages := make([]string, 0, numLanguages)
- from := 0
- for i, c := range buf {
- if c == 0 {
- languages = append(languages, string(utf16.Decode(buf[from:i])))
- from = i + 1
- }
- }
- return languages, nil
- }
- }
- func SetConsoleCursorPosition(console Handle, position Coord) error {
- return setConsoleCursorPosition(console, *((*uint32)(unsafe.Pointer(&position))))
- }
- func (s NTStatus) Errno() syscall.Errno {
- return rtlNtStatusToDosErrorNoTeb(s)
- }
- func langID(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) }
- func (s NTStatus) Error() string {
- b := make([]uint16, 300)
- 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)
- if err != nil {
- return fmt.Sprintf("NTSTATUS 0x%08x", uint32(s))
- }
- // trim terminating \r and \n
- for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
- }
- return string(utf16.Decode(b[:n]))
- }
- // NewNTUnicodeString returns a new NTUnicodeString structure for use with native
- // NT APIs that work over the NTUnicodeString type. Note that most Windows APIs
- // do not use NTUnicodeString, and instead UTF16PtrFromString should be used for
- // the more common *uint16 string type.
- func NewNTUnicodeString(s string) (*NTUnicodeString, error) {
- var u NTUnicodeString
- s16, err := UTF16PtrFromString(s)
- if err != nil {
- return nil, err
- }
- RtlInitUnicodeString(&u, s16)
- return &u, nil
- }
- // Slice returns a uint16 slice that aliases the data in the NTUnicodeString.
- func (s *NTUnicodeString) Slice() []uint16 {
- var slice []uint16
- hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice))
- hdr.Data = unsafe.Pointer(s.Buffer)
- hdr.Len = int(s.Length)
- hdr.Cap = int(s.MaximumLength)
- return slice
- }
- func (s *NTUnicodeString) String() string {
- return UTF16ToString(s.Slice())
- }
- // NewNTString returns a new NTString structure for use with native
- // NT APIs that work over the NTString type. Note that most Windows APIs
- // do not use NTString, and instead UTF16PtrFromString should be used for
- // the more common *uint16 string type.
- func NewNTString(s string) (*NTString, error) {
- var nts NTString
- s8, err := BytePtrFromString(s)
- if err != nil {
- return nil, err
- }
- RtlInitString(&nts, s8)
- return &nts, nil
- }
- // Slice returns a byte slice that aliases the data in the NTString.
- func (s *NTString) Slice() []byte {
- var slice []byte
- hdr := (*unsafeheader.Slice)(unsafe.Pointer(&slice))
- hdr.Data = unsafe.Pointer(s.Buffer)
- hdr.Len = int(s.Length)
- hdr.Cap = int(s.MaximumLength)
- return slice
- }
- func (s *NTString) String() string {
- return ByteSliceToString(s.Slice())
- }
- // FindResource resolves a resource of the given name and resource type.
- func FindResource(module Handle, name, resType ResourceIDOrString) (Handle, error) {
- var namePtr, resTypePtr uintptr
- var name16, resType16 *uint16
- var err error
- resolvePtr := func(i interface{}, keep **uint16) (uintptr, error) {
- switch v := i.(type) {
- case string:
- *keep, err = UTF16PtrFromString(v)
- if err != nil {
- return 0, err
- }
- return uintptr(unsafe.Pointer(*keep)), nil
- case ResourceID:
- return uintptr(v), nil
- }
- return 0, errorspkg.New("parameter must be a ResourceID or a string")
- }
- namePtr, err = resolvePtr(name, &name16)
- if err != nil {
- return 0, err
- }
- resTypePtr, err = resolvePtr(resType, &resType16)
- if err != nil {
- return 0, err
- }
- resInfo, err := findResource(module, namePtr, resTypePtr)
- runtime.KeepAlive(name16)
- runtime.KeepAlive(resType16)
- return resInfo, err
- }
- func LoadResourceData(module, resInfo Handle) (data []byte, err error) {
- size, err := SizeofResource(module, resInfo)
- if err != nil {
- return
- }
- resData, err := LoadResource(module, resInfo)
- if err != nil {
- return
- }
- ptr, err := LockResource(resData)
- if err != nil {
- return
- }
- h := (*unsafeheader.Slice)(unsafe.Pointer(&data))
- h.Data = unsafe.Pointer(ptr)
- h.Len = int(size)
- h.Cap = int(size)
- return
- }
|