1
2
3
4
5 package windows
6
7 import (
8 "sync"
9 "syscall"
10 "unsafe"
11 )
12
13
14
15
16
17 var CanUseLongPaths bool
18
19
20
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
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
171
172
173
174
175
176
177
178
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
193
194
195
196
197
198
199
200
201 const (
202
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
225
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
273
274
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
370
371
372 const (
373 LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
374 LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
375 )
376
377 const MB_ERR_INVALID_CHARS = 8
378
379
380
381
382
383
384
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
402
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
415
416 func ErrorLoadingGetTempPath2() error {
417 return procGetTempPath2W.Find()
418 }
419
420
421
422
423
424
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
460
461
462 type RUNTIME_FUNCTION struct {
463 BeginAddress uint32
464 EndAddress uint32
465 UnwindData uint32
466 }
467
468
469
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
487
488
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
506
507
508 func QueryPerformanceCounter() int64
509
510
511
512
513
514 func QueryPerformanceFrequency() int64
515
516
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
531
532
533
534
535
536
537
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
551
552
553
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
571 type FILE_MODE_INFORMATION struct {
572 Mode uint32
573 }
574
575
576
577
578
579
580
581
582
View as plain text