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_BAD_LENGTH syscall.Errno = 24
36 ERROR_SHARING_VIOLATION syscall.Errno = 32
37 ERROR_LOCK_VIOLATION syscall.Errno = 33
38 ERROR_NOT_SUPPORTED syscall.Errno = 50
39 ERROR_CALL_NOT_IMPLEMENTED syscall.Errno = 120
40 ERROR_INVALID_NAME syscall.Errno = 123
41 ERROR_LOCK_FAILED syscall.Errno = 167
42 ERROR_NO_TOKEN syscall.Errno = 1008
43 ERROR_NO_UNICODE_TRANSLATION syscall.Errno = 1113
44 )
45
46 const (
47 GAA_FLAG_INCLUDE_PREFIX = 0x00000010
48 GAA_FLAG_INCLUDE_GATEWAYS = 0x0080
49 )
50
51 const (
52 IF_TYPE_OTHER = 1
53 IF_TYPE_ETHERNET_CSMACD = 6
54 IF_TYPE_ISO88025_TOKENRING = 9
55 IF_TYPE_PPP = 23
56 IF_TYPE_SOFTWARE_LOOPBACK = 24
57 IF_TYPE_ATM = 37
58 IF_TYPE_IEEE80211 = 71
59 IF_TYPE_TUNNEL = 131
60 IF_TYPE_IEEE1394 = 144
61 )
62
63 type SocketAddress struct {
64 Sockaddr *syscall.RawSockaddrAny
65 SockaddrLength int32
66 }
67
68 type IpAdapterUnicastAddress struct {
69 Length uint32
70 Flags uint32
71 Next *IpAdapterUnicastAddress
72 Address SocketAddress
73 PrefixOrigin int32
74 SuffixOrigin int32
75 DadState int32
76 ValidLifetime uint32
77 PreferredLifetime uint32
78 LeaseLifetime uint32
79 OnLinkPrefixLength uint8
80 }
81
82 type IpAdapterAnycastAddress struct {
83 Length uint32
84 Flags uint32
85 Next *IpAdapterAnycastAddress
86 Address SocketAddress
87 }
88
89 type IpAdapterMulticastAddress struct {
90 Length uint32
91 Flags uint32
92 Next *IpAdapterMulticastAddress
93 Address SocketAddress
94 }
95
96 type IpAdapterDnsServerAdapter struct {
97 Length uint32
98 Reserved uint32
99 Next *IpAdapterDnsServerAdapter
100 Address SocketAddress
101 }
102
103 type IpAdapterPrefix struct {
104 Length uint32
105 Flags uint32
106 Next *IpAdapterPrefix
107 Address SocketAddress
108 PrefixLength uint32
109 }
110
111 type IpAdapterWinsServerAddress struct {
112 Length uint32
113 Reserved uint32
114 Next *IpAdapterWinsServerAddress
115 Address SocketAddress
116 }
117
118 type IpAdapterGatewayAddress struct {
119 Length uint32
120 Reserved uint32
121 Next *IpAdapterGatewayAddress
122 Address SocketAddress
123 }
124
125 type IpAdapterAddresses struct {
126 Length uint32
127 IfIndex uint32
128 Next *IpAdapterAddresses
129 AdapterName *byte
130 FirstUnicastAddress *IpAdapterUnicastAddress
131 FirstAnycastAddress *IpAdapterAnycastAddress
132 FirstMulticastAddress *IpAdapterMulticastAddress
133 FirstDnsServerAddress *IpAdapterDnsServerAdapter
134 DnsSuffix *uint16
135 Description *uint16
136 FriendlyName *uint16
137 PhysicalAddress [syscall.MAX_ADAPTER_ADDRESS_LENGTH]byte
138 PhysicalAddressLength uint32
139 Flags uint32
140 Mtu uint32
141 IfType uint32
142 OperStatus uint32
143 Ipv6IfIndex uint32
144 ZoneIndices [16]uint32
145 FirstPrefix *IpAdapterPrefix
146 TransmitLinkSpeed uint64
147 ReceiveLinkSpeed uint64
148 FirstWinsServerAddress *IpAdapterWinsServerAddress
149 FirstGatewayAddress *IpAdapterGatewayAddress
150
151 }
152
153 type SecurityAttributes struct {
154 Length uint16
155 SecurityDescriptor uintptr
156 InheritHandle bool
157 }
158
159 type FILE_BASIC_INFO struct {
160 CreationTime int64
161 LastAccessTime int64
162 LastWriteTime int64
163 ChangedTime int64
164 FileAttributes uint32
165
166
167
168
169
170
171
172
173
174
175 _ uint32
176 }
177
178 const (
179 IfOperStatusUp = 1
180 IfOperStatusDown = 2
181 IfOperStatusTesting = 3
182 IfOperStatusUnknown = 4
183 IfOperStatusDormant = 5
184 IfOperStatusNotPresent = 6
185 IfOperStatusLowerLayerDown = 7
186 )
187
188
189
190
191
192
193
194
195
196 const (
197
198 TH32CS_SNAPMODULE = 0x08
199 TH32CS_SNAPMODULE32 = 0x10
200 )
201
202 const MAX_MODULE_NAME32 = 255
203
204 type ModuleEntry32 struct {
205 Size uint32
206 ModuleID uint32
207 ProcessID uint32
208 GlblcntUsage uint32
209 ProccntUsage uint32
210 ModBaseAddr uintptr
211 ModBaseSize uint32
212 ModuleHandle syscall.Handle
213 Module [MAX_MODULE_NAME32 + 1]uint16
214 ExePath [syscall.MAX_PATH]uint16
215 }
216
217 const SizeofModuleEntry32 = unsafe.Sizeof(ModuleEntry32{})
218
219
220
221
222 const (
223 WSA_FLAG_OVERLAPPED = 0x01
224 WSA_FLAG_NO_HANDLE_INHERIT = 0x80
225
226 WSAEINVAL syscall.Errno = 10022
227 WSAEMSGSIZE syscall.Errno = 10040
228 WSAEAFNOSUPPORT syscall.Errno = 10047
229
230 MSG_PEEK = 0x2
231 MSG_TRUNC = 0x0100
232 MSG_CTRUNC = 0x0200
233
234 socket_error = uintptr(^uint32(0))
235 )
236
237 var WSAID_WSASENDMSG = syscall.GUID{
238 Data1: 0xa441e712,
239 Data2: 0x754f,
240 Data3: 0x43ca,
241 Data4: [8]byte{0x84, 0xa7, 0x0d, 0xee, 0x44, 0xcf, 0x60, 0x6d},
242 }
243
244 var WSAID_WSARECVMSG = syscall.GUID{
245 Data1: 0xf689d7c8,
246 Data2: 0x6f1f,
247 Data3: 0x436b,
248 Data4: [8]byte{0x8a, 0x53, 0xe5, 0x4f, 0xe3, 0x51, 0xc3, 0x22},
249 }
250
251 var sendRecvMsgFunc struct {
252 once sync.Once
253 sendAddr uintptr
254 recvAddr uintptr
255 err error
256 }
257
258 type WSAMsg struct {
259 Name syscall.Pointer
260 Namelen int32
261 Buffers *syscall.WSABuf
262 BufferCount uint32
263 Control syscall.WSABuf
264 Flags uint32
265 }
266
267
268
269
270 func loadWSASendRecvMsg() error {
271 sendRecvMsgFunc.once.Do(func() {
272 var s syscall.Handle
273 s, sendRecvMsgFunc.err = syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
274 if sendRecvMsgFunc.err != nil {
275 return
276 }
277 defer syscall.CloseHandle(s)
278 var n uint32
279 sendRecvMsgFunc.err = syscall.WSAIoctl(s,
280 syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
281 (*byte)(unsafe.Pointer(&WSAID_WSARECVMSG)),
282 uint32(unsafe.Sizeof(WSAID_WSARECVMSG)),
283 (*byte)(unsafe.Pointer(&sendRecvMsgFunc.recvAddr)),
284 uint32(unsafe.Sizeof(sendRecvMsgFunc.recvAddr)),
285 &n, nil, 0)
286 if sendRecvMsgFunc.err != nil {
287 return
288 }
289 sendRecvMsgFunc.err = syscall.WSAIoctl(s,
290 syscall.SIO_GET_EXTENSION_FUNCTION_POINTER,
291 (*byte)(unsafe.Pointer(&WSAID_WSASENDMSG)),
292 uint32(unsafe.Sizeof(WSAID_WSASENDMSG)),
293 (*byte)(unsafe.Pointer(&sendRecvMsgFunc.sendAddr)),
294 uint32(unsafe.Sizeof(sendRecvMsgFunc.sendAddr)),
295 &n, nil, 0)
296 })
297 return sendRecvMsgFunc.err
298 }
299
300 func WSASendMsg(fd syscall.Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
301 err := loadWSASendRecvMsg()
302 if err != nil {
303 return err
304 }
305 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)))
306 if r1 == socket_error {
307 if e1 != 0 {
308 err = errnoErr(e1)
309 } else {
310 err = syscall.EINVAL
311 }
312 }
313 return err
314 }
315
316 func WSARecvMsg(fd syscall.Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *syscall.Overlapped, croutine *byte) error {
317 err := loadWSASendRecvMsg()
318 if err != nil {
319 return err
320 }
321 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)
322 if r1 == socket_error {
323 if e1 != 0 {
324 err = errnoErr(e1)
325 } else {
326 err = syscall.EINVAL
327 }
328 }
329 return err
330 }
331
332 const (
333 ComputerNameNetBIOS = 0
334 ComputerNameDnsHostname = 1
335 ComputerNameDnsDomain = 2
336 ComputerNameDnsFullyQualified = 3
337 ComputerNamePhysicalNetBIOS = 4
338 ComputerNamePhysicalDnsHostname = 5
339 ComputerNamePhysicalDnsDomain = 6
340 ComputerNamePhysicalDnsFullyQualified = 7
341 ComputerNameMax = 8
342
343 MOVEFILE_REPLACE_EXISTING = 0x1
344 MOVEFILE_COPY_ALLOWED = 0x2
345 MOVEFILE_DELAY_UNTIL_REBOOT = 0x4
346 MOVEFILE_WRITE_THROUGH = 0x8
347 MOVEFILE_CREATE_HARDLINK = 0x10
348 MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x20
349 )
350
351 func Rename(oldpath, newpath string) error {
352 from, err := syscall.UTF16PtrFromString(oldpath)
353 if err != nil {
354 return err
355 }
356 to, err := syscall.UTF16PtrFromString(newpath)
357 if err != nil {
358 return err
359 }
360 return MoveFileEx(from, to, MOVEFILE_REPLACE_EXISTING)
361 }
362
363
364
365
366 const (
367 LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
368 LOCKFILE_EXCLUSIVE_LOCK = 0x00000002
369 )
370
371 const MB_ERR_INVALID_CHARS = 8
372
373
374
375
376
377
378
379 const (
380 STYPE_DISKTREE = 0x00
381 STYPE_TEMPORARY = 0x40000000
382 )
383
384 type SHARE_INFO_2 struct {
385 Netname *uint16
386 Type uint32
387 Remark *uint16
388 Permissions uint32
389 MaxUses uint32
390 CurrentUses uint32
391 Path *uint16
392 Passwd *uint16
393 }
394
395
396
397
398 const (
399 FILE_NAME_NORMALIZED = 0x0
400 FILE_NAME_OPENED = 0x8
401
402 VOLUME_NAME_DOS = 0x0
403 VOLUME_NAME_GUID = 0x1
404 VOLUME_NAME_NONE = 0x4
405 VOLUME_NAME_NT = 0x2
406 )
407
408
409
410 func ErrorLoadingGetTempPath2() error {
411 return procGetTempPath2W.Find()
412 }
413
414
415
416
417
418
419
420 type FILE_ID_BOTH_DIR_INFO struct {
421 NextEntryOffset uint32
422 FileIndex uint32
423 CreationTime syscall.Filetime
424 LastAccessTime syscall.Filetime
425 LastWriteTime syscall.Filetime
426 ChangeTime syscall.Filetime
427 EndOfFile uint64
428 AllocationSize uint64
429 FileAttributes uint32
430 FileNameLength uint32
431 EaSize uint32
432 ShortNameLength uint32
433 ShortName [12]uint16
434 FileID uint64
435 FileName [1]uint16
436 }
437
438 type FILE_FULL_DIR_INFO struct {
439 NextEntryOffset uint32
440 FileIndex uint32
441 CreationTime syscall.Filetime
442 LastAccessTime syscall.Filetime
443 LastWriteTime syscall.Filetime
444 ChangeTime syscall.Filetime
445 EndOfFile uint64
446 AllocationSize uint64
447 FileAttributes uint32
448 FileNameLength uint32
449 EaSize uint32
450 FileName [1]uint16
451 }
452
453
454
455
456
457
458
459 type SERVICE_STATUS struct {
460 ServiceType uint32
461 CurrentState uint32
462 ControlsAccepted uint32
463 Win32ExitCode uint32
464 ServiceSpecificExitCode uint32
465 CheckPoint uint32
466 WaitHint uint32
467 }
468
469 const (
470 SERVICE_RUNNING = 4
471 SERVICE_QUERY_STATUS = 4
472 )
473
474
475
476
477
478 func FinalPath(h syscall.Handle, flags uint32) (string, error) {
479 buf := make([]uint16, 100)
480 for {
481 n, err := GetFinalPathNameByHandle(h, &buf[0], uint32(len(buf)), flags)
482 if err != nil {
483 return "", err
484 }
485 if n < uint32(len(buf)) {
486 break
487 }
488 buf = make([]uint16, n)
489 }
490 return syscall.UTF16ToString(buf), nil
491 }
492
493
494
495
496 func QueryPerformanceCounter() int64
497
498
499
500
501
502 func QueryPerformanceFrequency() int64
503
504
505
506
507
508 type NTStatus uint32
509
510 func (s NTStatus) Errno() syscall.Errno {
511 return rtlNtStatusToDosErrorNoTeb(s)
512 }
513
514 func langID(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) }
515
516 func (s NTStatus) Error() string {
517 return s.Errno().Error()
518 }
519
520
521
522
523
524 const (
525 STATUS_FILE_IS_A_DIRECTORY NTStatus = 0xC00000BA
526 STATUS_NOT_A_DIRECTORY NTStatus = 0xC0000103
527 STATUS_REPARSE_POINT_ENCOUNTERED NTStatus = 0xC000050B
528 )
529
530
531
532
533
View as plain text