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