Source file src/internal/syscall/windows/syscall_windows.go

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package windows
     6  
     7  import (
     8  	"sync"
     9  	"syscall"
    10  	"unsafe"
    11  )
    12  
    13  // CanUseLongPaths is true when the OS supports opting into
    14  // proper long path handling without the need for fixups.
    15  //
    16  //go:linkname CanUseLongPaths
    17  var CanUseLongPaths bool
    18  
    19  // UTF16PtrToString is like UTF16ToString, but takes *uint16
    20  // as a parameter instead of []uint16.
    21  func UTF16PtrToString(p *uint16) string {
    22  	if p == nil {
    23  		return ""
    24  	}
    25  	end := unsafe.Pointer(p)
    26  	n := 0
    27  	for *(*uint16)(end) != 0 {
    28  		end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
    29  		n++
    30  	}
    31  	return syscall.UTF16ToString(unsafe.Slice(p, n))
    32  }
    33  
    34  const (
    35  	ERROR_INVALID_HANDLE         syscall.Errno = 6
    36  	ERROR_BAD_LENGTH             syscall.Errno = 24
    37  	ERROR_SHARING_VIOLATION      syscall.Errno = 32
    38  	ERROR_LOCK_VIOLATION         syscall.Errno = 33
    39  	ERROR_NOT_SUPPORTED          syscall.Errno = 50
    40  	ERROR_CALL_NOT_IMPLEMENTED   syscall.Errno = 120
    41  	ERROR_INVALID_NAME           syscall.Errno = 123
    42  	ERROR_NEGATIVE_SEEK          syscall.Errno = 131
    43  	ERROR_LOCK_FAILED            syscall.Errno = 167
    44  	ERROR_IO_INCOMPLETE          syscall.Errno = 996
    45  	ERROR_NO_TOKEN               syscall.Errno = 1008
    46  	ERROR_NO_UNICODE_TRANSLATION syscall.Errno = 1113
    47  	ERROR_CANT_ACCESS_FILE       syscall.Errno = 1920
    48  )
    49  
    50  const (
    51  	GAA_FLAG_INCLUDE_PREFIX   = 0x00000010
    52  	GAA_FLAG_INCLUDE_GATEWAYS = 0x0080
    53  )
    54  
    55  const (
    56  	IF_TYPE_OTHER              = 1
    57  	IF_TYPE_ETHERNET_CSMACD    = 6
    58  	IF_TYPE_ISO88025_TOKENRING = 9
    59  	IF_TYPE_PPP                = 23
    60  	IF_TYPE_SOFTWARE_LOOPBACK  = 24
    61  	IF_TYPE_ATM                = 37
    62  	IF_TYPE_IEEE80211          = 71
    63  	IF_TYPE_TUNNEL             = 131
    64  	IF_TYPE_IEEE1394           = 144
    65  )
    66  
    67  type SocketAddress struct {
    68  	Sockaddr       *syscall.RawSockaddrAny
    69  	SockaddrLength int32
    70  }
    71  
    72  type IpAdapterUnicastAddress struct {
    73  	Length             uint32
    74  	Flags              uint32
    75  	Next               *IpAdapterUnicastAddress
    76  	Address            SocketAddress
    77  	PrefixOrigin       int32
    78  	SuffixOrigin       int32
    79  	DadState           int32
    80  	ValidLifetime      uint32
    81  	PreferredLifetime  uint32
    82  	LeaseLifetime      uint32
    83  	OnLinkPrefixLength uint8
    84  }
    85  
    86  type IpAdapterAnycastAddress struct {
    87  	Length  uint32
    88  	Flags   uint32
    89  	Next    *IpAdapterAnycastAddress
    90  	Address SocketAddress
    91  }
    92  
    93  type IpAdapterMulticastAddress struct {
    94  	Length  uint32
    95  	Flags   uint32
    96  	Next    *IpAdapterMulticastAddress
    97  	Address SocketAddress
    98  }
    99  
   100  type IpAdapterDnsServerAdapter struct {
   101  	Length   uint32
   102  	Reserved uint32
   103  	Next     *IpAdapterDnsServerAdapter
   104  	Address  SocketAddress
   105  }
   106  
   107  type IpAdapterPrefix struct {
   108  	Length       uint32
   109  	Flags        uint32
   110  	Next         *IpAdapterPrefix
   111  	Address      SocketAddress
   112  	PrefixLength uint32
   113  }
   114  
   115  type IpAdapterWinsServerAddress struct {
   116  	Length   uint32
   117  	Reserved uint32
   118  	Next     *IpAdapterWinsServerAddress
   119  	Address  SocketAddress
   120  }
   121  
   122  type IpAdapterGatewayAddress struct {
   123  	Length   uint32
   124  	Reserved uint32
   125  	Next     *IpAdapterGatewayAddress
   126  	Address  SocketAddress
   127  }
   128  
   129  type IpAdapterAddresses struct {
   130  	Length                 uint32
   131  	IfIndex                uint32
   132  	Next                   *IpAdapterAddresses
   133  	AdapterName            *byte
   134  	FirstUnicastAddress    *IpAdapterUnicastAddress
   135  	FirstAnycastAddress    *IpAdapterAnycastAddress
   136  	FirstMulticastAddress  *IpAdapterMulticastAddress
   137  	FirstDnsServerAddress  *IpAdapterDnsServerAdapter
   138  	DnsSuffix              *uint16
   139  	Description            *uint16
   140  	FriendlyName           *uint16
   141  	PhysicalAddress        [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
   142  	PhysicalAddressLength  uint32
   143  	Flags                  uint32
   144  	Mtu                    uint32
   145  	IfType                 uint32
   146  	OperStatus             uint32
   147  	Ipv6IfIndex            uint32
   148  	ZoneIndices            [16]uint32
   149  	FirstPrefix            *IpAdapterPrefix
   150  	TransmitLinkSpeed      uint64
   151  	ReceiveLinkSpeed       uint64
   152  	FirstWinsServerAddress *IpAdapterWinsServerAddress
   153  	FirstGatewayAddress    *IpAdapterGatewayAddress
   154  	/* more fields might be present here. */
   155  }
   156  
   157  type SecurityAttributes struct {
   158  	Length             uint16
   159  	SecurityDescriptor uintptr
   160  	InheritHandle      bool
   161  }
   162  
   163  type FILE_BASIC_INFO struct {
   164  	CreationTime   int64
   165  	LastAccessTime int64
   166  	LastWriteTime  int64
   167  	ChangedTime    int64
   168  	FileAttributes uint32
   169  
   170  	// Pad out to 8-byte alignment.
   171  	//
   172  	// Without this padding, TestChmod fails due to an argument validation error
   173  	// in SetFileInformationByHandle on windows/386.
   174  	//
   175  	// https://learn.microsoft.com/en-us/cpp/build/reference/zp-struct-member-alignment?view=msvc-170
   176  	// says that “The C/C++ headers in the Windows SDK assume the platform's
   177  	// default alignment is used.” What we see here is padding rather than
   178  	// alignment, but maybe it is related.
   179  	_ uint32
   180  }
   181  
   182  const (
   183  	IfOperStatusUp             = 1
   184  	IfOperStatusDown           = 2
   185  	IfOperStatusTesting        = 3
   186  	IfOperStatusUnknown        = 4
   187  	IfOperStatusDormant        = 5
   188  	IfOperStatusNotPresent     = 6
   189  	IfOperStatusLowerLayerDown = 7
   190  )
   191  
   192  //sys	GetAdaptersAddresses(family uint32, flags uint32, reserved unsafe.Pointer, adapterAddresses *IpAdapterAddresses, sizePointer *uint32) (errcode error) = iphlpapi.GetAdaptersAddresses
   193  //sys	GetComputerNameEx(nameformat uint32, buf *uint16, n *uint32) (err error) = GetComputerNameExW
   194  //sys	MoveFileEx(from *uint16, to *uint16, flags uint32) (err error) = MoveFileExW
   195  //sys	GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32, err error) = kernel32.GetModuleFileNameW
   196  //sys	SetFileInformationByHandle(handle syscall.Handle, fileInformationClass uint32, buf unsafe.Pointer, bufsize uint32) (err error) = kernel32.SetFileInformationByHandle
   197  //sys	VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) = kernel32.VirtualQuery
   198  //sys	GetTempPath2(buflen uint32, buf *uint16) (n uint32, err error) = GetTempPath2W
   199  //sys	GetFileSizeEx(handle syscall.Handle, size *int64) (err error) = kernel32.GetFileSizeEx
   200  
   201  const (
   202  	// flags for CreateToolhelp32Snapshot
   203  	TH32CS_SNAPMODULE   = 0x08
   204  	TH32CS_SNAPMODULE32 = 0x10
   205  )
   206  
   207  const MAX_MODULE_NAME32 = 255
   208  
   209  type ModuleEntry32 struct {
   210  	Size         uint32
   211  	ModuleID     uint32
   212  	ProcessID    uint32
   213  	GlblcntUsage uint32
   214  	ProccntUsage uint32
   215  	ModBaseAddr  uintptr
   216  	ModBaseSize  uint32
   217  	ModuleHandle syscall.Handle
   218  	Module       [MAX_MODULE_NAME32 + 1]uint16
   219  	ExePath      [syscall.MAX_PATH]uint16
   220  }
   221  
   222  const SizeofModuleEntry32 = unsafe.Sizeof(ModuleEntry32{})
   223  
   224  //sys	Module32First(snapshot syscall.Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32FirstW
   225  //sys	Module32Next(snapshot syscall.Handle, moduleEntry *ModuleEntry32) (err error) = kernel32.Module32NextW
   226  
   227  const (
   228  	WSA_FLAG_OVERLAPPED        = 0x01
   229  	WSA_FLAG_NO_HANDLE_INHERIT = 0x80
   230  
   231  	WSAEINVAL       syscall.Errno = 10022
   232  	WSAEMSGSIZE     syscall.Errno = 10040
   233  	WSAEAFNOSUPPORT syscall.Errno = 10047
   234  
   235  	MSG_PEEK   = 0x2
   236  	MSG_TRUNC  = 0x0100
   237  	MSG_CTRUNC = 0x0200
   238  
   239  	socket_error = uintptr(^uint32(0))
   240  )
   241  
   242  var WSAID_WSASENDMSG = syscall.GUID{
   243  	Data1: 0xa441e712,
   244  	Data2: 0x754f,
   245  	Data3: 0x43ca,
   246  	Data4: [8]byte{0x84, 0xa7, 0x0d, 0xee, 0x44, 0xcf, 0x60, 0x6d},
   247  }
   248  
   249  var WSAID_WSARECVMSG = syscall.GUID{
   250  	Data1: 0xf689d7c8,
   251  	Data2: 0x6f1f,
   252  	Data3: 0x436b,
   253  	Data4: [8]byte{0x8a, 0x53, 0xe5, 0x4f, 0xe3, 0x51, 0xc3, 0x22},
   254  }
   255  
   256  var sendRecvMsgFunc struct {
   257  	once     sync.Once
   258  	sendAddr uintptr
   259  	recvAddr uintptr
   260  	err      error
   261  }
   262  
   263  type WSAMsg struct {
   264  	Name        *syscall.RawSockaddrAny
   265  	Namelen     int32
   266  	Buffers     *syscall.WSABuf
   267  	BufferCount uint32
   268  	Control     syscall.WSABuf
   269  	Flags       uint32
   270  }
   271  
   272  //sys	WSASocket(af int32, typ int32, protocol int32, protinfo *syscall.WSAProtocolInfo, group uint32, flags uint32) (handle syscall.Handle, err error) [failretval==syscall.InvalidHandle] = ws2_32.WSASocketW
   273  //sys	WSADuplicateSocket(s syscall.Handle, processID uint32, info *syscall.WSAProtocolInfo) (err error) [failretval!=0] = ws2_32.WSADuplicateSocketW
   274  //sys	WSAGetOverlappedResult(h syscall.Handle, o *syscall.Overlapped, bytes *uint32, wait bool, flags *uint32) (err error) = ws2_32.WSAGetOverlappedResult
   275  
   276  func loadWSASendRecvMsg() error {
   277  	sendRecvMsgFunc.once.Do(func() {
   278  		var s syscall.Handle
   279  		s, sendRecvMsgFunc.err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
   280  		if sendRecvMsgFunc.err != nil {
   281  			return
   282  		}
   283  		defer syscall.CloseHandle(s)
   284  		var n uint32
   285  		sendRecvMsgFunc.err = syscall.WSAIoctl(s,
   286  			syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
   287  			(*byte)(unsafe.Pointer(&WSAID_WSARECVMSG)),
   288  			uint32(unsafe.Sizeof(WSAID_WSARECVMSG)),
   289  			(*byte)(unsafe.Pointer(&sendRecvMsgFunc.recvAddr)),
   290  			uint32(unsafe.Sizeof(sendRecvMsgFunc.recvAddr)),
   291  			&n, nil, 0)
   292  		if sendRecvMsgFunc.err != nil {
   293  			return
   294  		}
   295  		sendRecvMsgFunc.err = syscall.WSAIoctl(s,
   296  			syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
   297  			(*byte)(unsafe.Pointer(&WSAID_WSASENDMSG)),
   298  			uint32(unsafe.Sizeof(WSAID_WSASENDMSG)),
   299  			(*byte)(unsafe.Pointer(&sendRecvMsgFunc.sendAddr)),
   300  			uint32(unsafe.Sizeof(sendRecvMsgFunc.sendAddr)),
   301  			&n, nil, 0)
   302  	})
   303  	return sendRecvMsgFunc.err
   304  }
   305  
   306  func WSASendMsg(fd syscall.Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
   307  	err := loadWSASendRecvMsg()
   308  	if err != nil {
   309  		return err
   310  	}
   311  	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)))
   312  	if r1 == socket_error {
   313  		if e1 != 0 {
   314  			err = errnoErr(e1)
   315  		} else {
   316  			err = syscall.EINVAL
   317  		}
   318  	}
   319  	return err
   320  }
   321  
   322  func WSARecvMsg(fd syscall.Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
   323  	err := loadWSASendRecvMsg()
   324  	if err != nil {
   325  		return err
   326  	}
   327  	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)
   328  	if r1 == socket_error {
   329  		if e1 != 0 {
   330  			err = errnoErr(e1)
   331  		} else {
   332  			err = syscall.EINVAL
   333  		}
   334  	}
   335  	return err
   336  }
   337  
   338  const (
   339  	ComputerNameNetBIOS                   = 0
   340  	ComputerNameDnsHostname               = 1
   341  	ComputerNameDnsDomain                 = 2
   342  	ComputerNameDnsFullyQualified         = 3
   343  	ComputerNamePhysicalNetBIOS           = 4
   344  	ComputerNamePhysicalDnsHostname       = 5
   345  	ComputerNamePhysicalDnsDomain         = 6
   346  	ComputerNamePhysicalDnsFullyQualified = 7
   347  	ComputerNameMax                       = 8
   348  
   349  	MOVEFILE_REPLACE_EXISTING      = 0x1
   350  	MOVEFILE_COPY_ALLOWED          = 0x2
   351  	MOVEFILE_DELAY_UNTIL_REBOOT    = 0x4
   352  	MOVEFILE_WRITE_THROUGH         = 0x8
   353  	MOVEFILE_CREATE_HARDLINK       = 0x10
   354  	MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x20
   355  )
   356  
   357  func Rename(oldpath, newpath string) error {
   358  	from, err := syscall.UTF16PtrFromString(oldpath)
   359  	if err != nil {
   360  		return err
   361  	}
   362  	to, err := syscall.UTF16PtrFromString(newpath)
   363  	if err != nil {
   364  		return err
   365  	}
   366  	return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING)
   367  }
   368  
   369  //sys LockFileEx(file syscall.Handle, flags uint32, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *syscall.Overlapped) (err error) = kernel32.LockFileEx
   370  //sys UnlockFileEx(file syscall.Handle, reserved uint32, bytesLow uint32, bytesHigh uint32, overlapped *syscall.Overlapped) (err error) = kernel32.UnlockFileEx
   371  
   372  const (
   373  	LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
   374  	LOCKFILE_EXCLUSIVE_LOCK   = 0x00000002
   375  )
   376  
   377  const MB_ERR_INVALID_CHARS = 8
   378  
   379  //sys	GetACP() (acp uint32) = kernel32.GetACP
   380  //sys	GetConsoleCP() (ccp uint32) = kernel32.GetConsoleCP
   381  //sys	MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32, wchar *uint16, nwchar int32) (nwrite int32, err error) = kernel32.MultiByteToWideChar
   382  //sys	GetCurrentThread() (pseudoHandle syscall.Handle, err error) = kernel32.GetCurrentThread
   383  
   384  // Constants from lmshare.h
   385  const (
   386  	STYPE_DISKTREE  = 0x00
   387  	STYPE_TEMPORARY = 0x40000000
   388  )
   389  
   390  type SHARE_INFO_2 struct {
   391  	Netname     *uint16
   392  	Type        uint32
   393  	Remark      *uint16
   394  	Permissions uint32
   395  	MaxUses     uint32
   396  	CurrentUses uint32
   397  	Path        *uint16
   398  	Passwd      *uint16
   399  }
   400  
   401  //sys  NetShareAdd(serverName *uint16, level uint32, buf *byte, parmErr *uint16) (neterr error) = netapi32.NetShareAdd
   402  //sys  NetShareDel(serverName *uint16, netName *uint16, reserved uint32) (neterr error) = netapi32.NetShareDel
   403  
   404  const (
   405  	FILE_NAME_NORMALIZED = 0x0
   406  	FILE_NAME_OPENED     = 0x8
   407  
   408  	VOLUME_NAME_DOS  = 0x0
   409  	VOLUME_NAME_GUID = 0x1
   410  	VOLUME_NAME_NONE = 0x4
   411  	VOLUME_NAME_NT   = 0x2
   412  )
   413  
   414  //sys	GetFinalPathNameByHandle(file syscall.Handle, filePath *uint16, filePathSize uint32, flags uint32) (n uint32, err error) = kernel32.GetFinalPathNameByHandleW
   415  
   416  func ErrorLoadingGetTempPath2() error {
   417  	return procGetTempPath2W.Find()
   418  }
   419  
   420  //sys	CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
   421  //sys	DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
   422  //sys	CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
   423  
   424  //sys	ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
   425  
   426  type FILE_ID_BOTH_DIR_INFO struct {
   427  	NextEntryOffset uint32
   428  	FileIndex       uint32
   429  	CreationTime    syscall.Filetime
   430  	LastAccessTime  syscall.Filetime
   431  	LastWriteTime   syscall.Filetime
   432  	ChangeTime      syscall.Filetime
   433  	EndOfFile       uint64
   434  	AllocationSize  uint64
   435  	FileAttributes  uint32
   436  	FileNameLength  uint32
   437  	EaSize          uint32
   438  	ShortNameLength uint32
   439  	ShortName       [12]uint16
   440  	FileID          uint64
   441  	FileName        [1]uint16
   442  }
   443  
   444  type FILE_FULL_DIR_INFO struct {
   445  	NextEntryOffset uint32
   446  	FileIndex       uint32
   447  	CreationTime    syscall.Filetime
   448  	LastAccessTime  syscall.Filetime
   449  	LastWriteTime   syscall.Filetime
   450  	ChangeTime      syscall.Filetime
   451  	EndOfFile       uint64
   452  	AllocationSize  uint64
   453  	FileAttributes  uint32
   454  	FileNameLength  uint32
   455  	EaSize          uint32
   456  	FileName        [1]uint16
   457  }
   458  
   459  //sys	GetVolumeInformationByHandle(file syscall.Handle, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationByHandleW
   460  //sys	GetVolumeNameForVolumeMountPoint(volumeMountPoint *uint16, volumeName *uint16, bufferlength uint32) (err error) = GetVolumeNameForVolumeMountPointW
   461  
   462  type RUNTIME_FUNCTION struct {
   463  	BeginAddress uint32
   464  	EndAddress   uint32
   465  	UnwindData   uint32
   466  }
   467  
   468  //sys	RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table unsafe.Pointer) (ret *RUNTIME_FUNCTION) = kernel32.RtlLookupFunctionEntry
   469  //sys	RtlVirtualUnwind(handlerType uint32, baseAddress uintptr, pc uintptr, entry *RUNTIME_FUNCTION, ctxt unsafe.Pointer, data unsafe.Pointer, frame *uintptr, ctxptrs unsafe.Pointer) (ret uintptr) = kernel32.RtlVirtualUnwind
   470  
   471  type SERVICE_STATUS struct {
   472  	ServiceType             uint32
   473  	CurrentState            uint32
   474  	ControlsAccepted        uint32
   475  	Win32ExitCode           uint32
   476  	ServiceSpecificExitCode uint32
   477  	CheckPoint              uint32
   478  	WaitHint                uint32
   479  }
   480  
   481  const (
   482  	SERVICE_RUNNING      = 4
   483  	SERVICE_QUERY_STATUS = 4
   484  )
   485  
   486  //sys    OpenService(mgr syscall.Handle, serviceName *uint16, access uint32) (handle syscall.Handle, err error) = advapi32.OpenServiceW
   487  //sys	QueryServiceStatus(hService syscall.Handle, lpServiceStatus *SERVICE_STATUS) (err error)  = advapi32.QueryServiceStatus
   488  //sys    OpenSCManager(machineName *uint16, databaseName *uint16, access uint32) (handle syscall.Handle, err error)  [failretval==0] = advapi32.OpenSCManagerW
   489  
   490  func FinalPath(h syscall.Handle, flags uint32) (string, error) {
   491  	buf := make([]uint16, 100)
   492  	for {
   493  		n, err := GetFinalPathNameByHandle(h, &buf[0], uint32(len(buf)), flags)
   494  		if err != nil {
   495  			return "", err
   496  		}
   497  		if n < uint32(len(buf)) {
   498  			break
   499  		}
   500  		buf = make([]uint16, n)
   501  	}
   502  	return syscall.UTF16ToString(buf), nil
   503  }
   504  
   505  // QueryPerformanceCounter retrieves the current value of performance counter.
   506  //
   507  //go:linkname QueryPerformanceCounter
   508  func QueryPerformanceCounter() int64 // Implemented in runtime package.
   509  
   510  // QueryPerformanceFrequency retrieves the frequency of the performance counter.
   511  // The returned value is represented as counts per second.
   512  //
   513  //go:linkname QueryPerformanceFrequency
   514  func QueryPerformanceFrequency() int64 // Implemented in runtime package.
   515  
   516  //sys   GetModuleHandle(modulename *uint16) (handle syscall.Handle, err error) = kernel32.GetModuleHandleW
   517  
   518  const (
   519  	PIPE_ACCESS_INBOUND  = 0x00000001
   520  	PIPE_ACCESS_OUTBOUND = 0x00000002
   521  	PIPE_ACCESS_DUPLEX   = 0x00000003
   522  
   523  	PIPE_TYPE_BYTE    = 0x00000000
   524  	PIPE_TYPE_MESSAGE = 0x00000004
   525  
   526  	PIPE_READMODE_BYTE    = 0x00000000
   527  	PIPE_READMODE_MESSAGE = 0x00000002
   528  )
   529  
   530  //sys	CreateIoCompletionPort(filehandle syscall.Handle, cphandle syscall.Handle, key uintptr, threadcnt uint32) (handle syscall.Handle, err error)
   531  //sys	GetOverlappedResult(handle syscall.Handle, overlapped *syscall.Overlapped, done *uint32, wait bool) (err error)
   532  //sys	CreateNamedPipe(name *uint16, flags uint32, pipeMode uint32, maxInstances uint32, outSize uint32, inSize uint32, defaultTimeout uint32, sa *syscall.SecurityAttributes) (handle syscall.Handle, err error)  [failretval==syscall.InvalidHandle] = CreateNamedPipeW
   533  
   534  //sys	ReOpenFile(filehandle syscall.Handle, desiredAccess uint32, shareMode uint32, flagAndAttributes uint32) (handle syscall.Handle, err error)
   535  
   536  // NTStatus corresponds with NTSTATUS, error values returned by ntdll.dll and
   537  // other native functions.
   538  type NTStatus uint32
   539  
   540  func (s NTStatus) Errno() syscall.Errno {
   541  	return rtlNtStatusToDosErrorNoTeb(s)
   542  }
   543  
   544  func langID(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) }
   545  
   546  func (s NTStatus) Error() string {
   547  	return s.Errno().Error()
   548  }
   549  
   550  // x/sys/windows/mkerrors.bash can generate a complete list of NTStatus codes.
   551  //
   552  // At the moment, we only need a couple, so just put them here manually.
   553  // If this list starts getting long, we should consider generating the full set.
   554  const (
   555  	STATUS_OBJECT_NAME_COLLISION     NTStatus = 0xC0000035
   556  	STATUS_FILE_IS_A_DIRECTORY       NTStatus = 0xC00000BA
   557  	STATUS_DIRECTORY_NOT_EMPTY       NTStatus = 0xC0000101
   558  	STATUS_NOT_A_DIRECTORY           NTStatus = 0xC0000103
   559  	STATUS_CANNOT_DELETE             NTStatus = 0xC0000121
   560  	STATUS_REPARSE_POINT_ENCOUNTERED NTStatus = 0xC000050B
   561  	STATUS_NOT_SUPPORTED             NTStatus = 0xC00000BB
   562  	STATUS_INVALID_PARAMETER         NTStatus = 0xC000000D
   563  	STATUS_INVALID_INFO_CLASS        NTStatus = 0xC0000003
   564  )
   565  
   566  const (
   567  	FileModeInformation = 16
   568  )
   569  
   570  // https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_file_mode_information
   571  type FILE_MODE_INFORMATION struct {
   572  	Mode uint32
   573  }
   574  
   575  // NT Native APIs
   576  //sys   NtCreateFile(handle *syscall.Handle, access uint32, oa *OBJECT_ATTRIBUTES, iosb *IO_STATUS_BLOCK, allocationSize *int64, attributes uint32, share uint32, disposition uint32, options uint32, eabuffer unsafe.Pointer, ealength uint32) (ntstatus error) = ntdll.NtCreateFile
   577  //sys   NtOpenFile(handle *syscall.Handle, access uint32, oa *OBJECT_ATTRIBUTES, iosb *IO_STATUS_BLOCK, share uint32, options uint32) (ntstatus error) = ntdll.NtOpenFile
   578  //sys   rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb
   579  //sys   NtSetInformationFile(handle syscall.Handle, iosb *IO_STATUS_BLOCK, inBuffer unsafe.Pointer, inBufferLen uint32, class uint32) (ntstatus error) = ntdll.NtSetInformationFile
   580  //sys	RtlIsDosDeviceName_U(name *uint16) (ret uint32) = ntdll.RtlIsDosDeviceName_U
   581  //sys   NtQueryInformationFile(handle syscall.Handle, iosb *IO_STATUS_BLOCK, inBuffer unsafe.Pointer, inBufferLen uint32, class uint32) (ntstatus error) = ntdll.NtQueryInformationFile
   582  

View as plain text