github.com/enetx/g@v1.0.80/internal/filelock/syscall/windows/zsyscall_windows.go (about) 1 package windows 2 3 import ( 4 "syscall" 5 "unsafe" 6 7 "github.com/enetx/g/internal/filelock/syscall/windows/sysdll" 8 ) 9 10 var _ unsafe.Pointer 11 12 // Do the interface allocations only once for common 13 // Errno values. 14 const ( 15 errnoERROR_IO_PENDING = 997 16 ) 17 18 var ( 19 errERROR_IO_PENDING error = syscall.Errno(errnoERROR_IO_PENDING) 20 errERROR_EINVAL error = syscall.EINVAL 21 ) 22 23 // errnoErr returns common boxed Errno values, to prevent 24 // allocations at runtime. 25 func errnoErr(e syscall.Errno) error { 26 switch e { 27 case 0: 28 return errERROR_EINVAL 29 case errnoERROR_IO_PENDING: 30 return errERROR_IO_PENDING 31 } 32 // TODO: add more here, after collecting data on the common 33 // error values see on Windows. (perhaps when running 34 // all.bat?) 35 return e 36 } 37 38 var ( 39 modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll")) 40 modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll")) 41 modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll")) 42 modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll")) 43 modpsapi = syscall.NewLazyDLL(sysdll.Add("psapi.dll")) 44 moduserenv = syscall.NewLazyDLL(sysdll.Add("userenv.dll")) 45 modws2_32 = syscall.NewLazyDLL(sysdll.Add("ws2_32.dll")) 46 47 procAdjustTokenPrivileges = modadvapi32.NewProc("AdjustTokenPrivileges") 48 procDuplicateTokenEx = modadvapi32.NewProc("DuplicateTokenEx") 49 procImpersonateSelf = modadvapi32.NewProc("ImpersonateSelf") 50 procLookupPrivilegeValueW = modadvapi32.NewProc("LookupPrivilegeValueW") 51 procOpenThreadToken = modadvapi32.NewProc("OpenThreadToken") 52 procRevertToSelf = modadvapi32.NewProc("RevertToSelf") 53 procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation") 54 procSystemFunction036 = modadvapi32.NewProc("SystemFunction036") 55 procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses") 56 procGetACP = modkernel32.NewProc("GetACP") 57 procGetComputerNameExW = modkernel32.NewProc("GetComputerNameExW") 58 procGetConsoleCP = modkernel32.NewProc("GetConsoleCP") 59 procGetCurrentThread = modkernel32.NewProc("GetCurrentThread") 60 procGetFileInformationByHandleEx = modkernel32.NewProc("GetFileInformationByHandleEx") 61 procGetFinalPathNameByHandleW = modkernel32.NewProc("GetFinalPathNameByHandleW") 62 procGetModuleFileNameW = modkernel32.NewProc("GetModuleFileNameW") 63 procLockFileEx = modkernel32.NewProc("LockFileEx") 64 procModule32FirstW = modkernel32.NewProc("Module32FirstW") 65 procModule32NextW = modkernel32.NewProc("Module32NextW") 66 procMoveFileExW = modkernel32.NewProc("MoveFileExW") 67 procMultiByteToWideChar = modkernel32.NewProc("MultiByteToWideChar") 68 procSetFileInformationByHandle = modkernel32.NewProc("SetFileInformationByHandle") 69 procUnlockFileEx = modkernel32.NewProc("UnlockFileEx") 70 procVirtualQuery = modkernel32.NewProc("VirtualQuery") 71 procNetShareAdd = modnetapi32.NewProc("NetShareAdd") 72 procNetShareDel = modnetapi32.NewProc("NetShareDel") 73 procNetUserGetLocalGroups = modnetapi32.NewProc("NetUserGetLocalGroups") 74 procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo") 75 procCreateEnvironmentBlock = moduserenv.NewProc("CreateEnvironmentBlock") 76 procDestroyEnvironmentBlock = moduserenv.NewProc("DestroyEnvironmentBlock") 77 procGetProfilesDirectoryW = moduserenv.NewProc("GetProfilesDirectoryW") 78 procWSASocketW = modws2_32.NewProc("WSASocketW") 79 ) 80 81 func adjustTokenPrivileges( 82 token syscall.Token, 83 disableAllPrivileges bool, 84 newstate *TOKEN_PRIVILEGES, 85 buflen uint32, 86 prevstate *TOKEN_PRIVILEGES, 87 returnlen *uint32, 88 ) (ret uint32, err error) { 89 var _p0 uint32 90 if disableAllPrivileges { 91 _p0 = 1 92 } 93 r0, _, e1 := syscall.Syscall6( 94 procAdjustTokenPrivileges.Addr(), 95 6, 96 uintptr(token), 97 uintptr(_p0), 98 uintptr(unsafe.Pointer(newstate)), 99 uintptr(buflen), 100 uintptr(unsafe.Pointer(prevstate)), 101 uintptr(unsafe.Pointer(returnlen)), 102 ) 103 ret = uint32(r0) 104 if true { 105 err = errnoErr(e1) 106 } 107 return 108 } 109 110 func DuplicateTokenEx( 111 hExistingToken syscall.Token, 112 dwDesiredAccess uint32, 113 lpTokenAttributes *syscall.SecurityAttributes, 114 impersonationLevel uint32, 115 tokenType TokenType, 116 phNewToken *syscall.Token, 117 ) (err error) { 118 r1, _, e1 := syscall.Syscall6( 119 procDuplicateTokenEx.Addr(), 120 6, 121 uintptr(hExistingToken), 122 uintptr(dwDesiredAccess), 123 uintptr(unsafe.Pointer(lpTokenAttributes)), 124 uintptr(impersonationLevel), 125 uintptr(tokenType), 126 uintptr(unsafe.Pointer(phNewToken)), 127 ) 128 if r1 == 0 { 129 err = errnoErr(e1) 130 } 131 return 132 } 133 134 func ImpersonateSelf(impersonationlevel uint32) (err error) { 135 r1, _, e1 := syscall.Syscall(procImpersonateSelf.Addr(), 1, uintptr(impersonationlevel), 0, 0) 136 if r1 == 0 { 137 err = errnoErr(e1) 138 } 139 return 140 } 141 142 func LookupPrivilegeValue(systemname, name *uint16, luid *LUID) (err error) { 143 r1, _, e1 := syscall.Syscall( 144 procLookupPrivilegeValueW.Addr(), 145 3, 146 uintptr(unsafe.Pointer(systemname)), 147 uintptr(unsafe.Pointer(name)), 148 uintptr(unsafe.Pointer(luid)), 149 ) 150 if r1 == 0 { 151 err = errnoErr(e1) 152 } 153 return 154 } 155 156 func OpenThreadToken(h syscall.Handle, access uint32, openasself bool, token *syscall.Token) (err error) { 157 var _p0 uint32 158 if openasself { 159 _p0 = 1 160 } 161 r1, _, e1 := syscall.Syscall6( 162 procOpenThreadToken.Addr(), 163 4, 164 uintptr(h), 165 uintptr(access), 166 uintptr(_p0), 167 uintptr(unsafe.Pointer(token)), 168 0, 169 0, 170 ) 171 if r1 == 0 { 172 err = errnoErr(e1) 173 } 174 return 175 } 176 177 func RevertToSelf() (err error) { 178 r1, _, e1 := syscall.Syscall(procRevertToSelf.Addr(), 0, 0, 0, 0) 179 if r1 == 0 { 180 err = errnoErr(e1) 181 } 182 return 183 } 184 185 func SetTokenInformation( 186 tokenHandle syscall.Token, 187 tokenInformationClass uint32, 188 tokenInformation uintptr, 189 tokenInformationLength uint32, 190 ) (err error) { 191 r1, _, e1 := syscall.Syscall6( 192 procSetTokenInformation.Addr(), 193 4, 194 uintptr(tokenHandle), 195 uintptr(tokenInformationClass), 196 uintptr(tokenInformation), 197 uintptr(tokenInformationLength), 198 0, 199 0, 200 ) 201 if r1 == 0 { 202 err = errnoErr(e1) 203 } 204 return 205 } 206 207 func RtlGenRandom(buf []byte) (err error) { 208 var _p0 *byte 209 if len(buf) > 0 { 210 _p0 = &buf[0] 211 } 212 r1, _, e1 := syscall.Syscall(procSystemFunction036.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0) 213 if r1 == 0 { 214 err = errnoErr(e1) 215 } 216 return 217 } 218 219 func GetAdaptersAddresses( 220 family uint32, 221 flags uint32, 222 reserved uintptr, 223 adapterAddresses *IpAdapterAddresses, 224 sizePointer *uint32, 225 ) (errcode error) { 226 r0, _, _ := syscall.Syscall6( 227 procGetAdaptersAddresses.Addr(), 228 5, 229 uintptr(family), 230 uintptr(flags), 231 uintptr(reserved), 232 uintptr(unsafe.Pointer(adapterAddresses)), 233 uintptr(unsafe.Pointer(sizePointer)), 234 0, 235 ) 236 if r0 != 0 { 237 errcode = syscall.Errno(r0) 238 } 239 return 240 } 241 242 func GetACP() (acp uint32) { 243 r0, _, _ := syscall.Syscall(procGetACP.Addr(), 0, 0, 0, 0) 244 acp = uint32(r0) 245 return 246 } 247 248 func GetComputerNameEx(nameformat uint32, buf *uint16, n *uint32) (err error) { 249 r1, _, e1 := syscall.Syscall( 250 procGetComputerNameExW.Addr(), 251 3, 252 uintptr(nameformat), 253 uintptr(unsafe.Pointer(buf)), 254 uintptr(unsafe.Pointer(n)), 255 ) 256 if r1 == 0 { 257 err = errnoErr(e1) 258 } 259 return 260 } 261 262 func GetConsoleCP() (ccp uint32) { 263 r0, _, _ := syscall.Syscall(procGetConsoleCP.Addr(), 0, 0, 0, 0) 264 ccp = uint32(r0) 265 return 266 } 267 268 func GetCurrentThread() (pseudoHandle syscall.Handle, err error) { 269 r0, _, e1 := syscall.Syscall(procGetCurrentThread.Addr(), 0, 0, 0, 0) 270 pseudoHandle = syscall.Handle(r0) 271 if pseudoHandle == 0 { 272 err = errnoErr(e1) 273 } 274 return 275 } 276 277 func GetFileInformationByHandleEx(handle syscall.Handle, class uint32, info *byte, bufsize uint32) (err error) { 278 r1, _, e1 := syscall.Syscall6( 279 procGetFileInformationByHandleEx.Addr(), 280 4, 281 uintptr(handle), 282 uintptr(class), 283 uintptr(unsafe.Pointer(info)), 284 uintptr(bufsize), 285 0, 286 0, 287 ) 288 if r1 == 0 { 289 err = errnoErr(e1) 290 } 291 return 292 } 293 294 func GetFinalPathNameByHandle( 295 file syscall.Handle, 296 filePath *uint16, 297 filePathSize uint32, 298 flags uint32, 299 ) (n uint32, err error) { 300 r0, _, e1 := syscall.Syscall6( 301 procGetFinalPathNameByHandleW.Addr(), 302 4, 303 uintptr(file), 304 uintptr(unsafe.Pointer(filePath)), 305 uintptr(filePathSize), 306 uintptr(flags), 307 0, 308 0, 309 ) 310 n = uint32(r0) 311 if n == 0 { 312 err = errnoErr(e1) 313 } 314 return 315 } 316 317 func GetModuleFileName(module syscall.Handle, fn *uint16, len uint32) (n uint32, err error) { 318 r0, _, e1 := syscall.Syscall( 319 procGetModuleFileNameW.Addr(), 320 3, 321 uintptr(module), 322 uintptr(unsafe.Pointer(fn)), 323 uintptr(len), 324 ) 325 n = uint32(r0) 326 if n == 0 { 327 err = errnoErr(e1) 328 } 329 return 330 } 331 332 func LockFileEx( 333 file syscall.Handle, 334 flags uint32, 335 reserved uint32, 336 bytesLow uint32, 337 bytesHigh uint32, 338 overlapped *syscall.Overlapped, 339 ) (err error) { 340 r1, _, e1 := syscall.Syscall6( 341 procLockFileEx.Addr(), 342 6, 343 uintptr(file), 344 uintptr(flags), 345 uintptr(reserved), 346 uintptr(bytesLow), 347 uintptr(bytesHigh), 348 uintptr(unsafe.Pointer(overlapped)), 349 ) 350 if r1 == 0 { 351 err = errnoErr(e1) 352 } 353 return 354 } 355 356 func Module32First(snapshot syscall.Handle, moduleEntry *ModuleEntry32) (err error) { 357 r1, _, e1 := syscall.Syscall( 358 procModule32FirstW.Addr(), 359 2, 360 uintptr(snapshot), 361 uintptr(unsafe.Pointer(moduleEntry)), 362 0, 363 ) 364 if r1 == 0 { 365 err = errnoErr(e1) 366 } 367 return 368 } 369 370 func Module32Next(snapshot syscall.Handle, moduleEntry *ModuleEntry32) (err error) { 371 r1, _, e1 := syscall.Syscall( 372 procModule32NextW.Addr(), 373 2, 374 uintptr(snapshot), 375 uintptr(unsafe.Pointer(moduleEntry)), 376 0, 377 ) 378 if r1 == 0 { 379 err = errnoErr(e1) 380 } 381 return 382 } 383 384 func MoveFileEx(from, to *uint16, flags uint32) (err error) { 385 r1, _, e1 := syscall.Syscall( 386 procMoveFileExW.Addr(), 387 3, 388 uintptr(unsafe.Pointer(from)), 389 uintptr(unsafe.Pointer(to)), 390 uintptr(flags), 391 ) 392 if r1 == 0 { 393 err = errnoErr(e1) 394 } 395 return 396 } 397 398 func MultiByteToWideChar( 399 codePage uint32, 400 dwFlags uint32, 401 str *byte, 402 nstr int32, 403 wchar *uint16, 404 nwchar int32, 405 ) (nwrite int32, err error) { 406 r0, _, e1 := syscall.Syscall6( 407 procMultiByteToWideChar.Addr(), 408 6, 409 uintptr(codePage), 410 uintptr(dwFlags), 411 uintptr(unsafe.Pointer(str)), 412 uintptr(nstr), 413 uintptr(unsafe.Pointer(wchar)), 414 uintptr(nwchar), 415 ) 416 nwrite = int32(r0) 417 if nwrite == 0 { 418 err = errnoErr(e1) 419 } 420 return 421 } 422 423 func SetFileInformationByHandle( 424 handle syscall.Handle, 425 fileInformationClass uint32, 426 buf uintptr, 427 bufsize uint32, 428 ) (err error) { 429 r1, _, e1 := syscall.Syscall6( 430 procSetFileInformationByHandle.Addr(), 431 4, 432 uintptr(handle), 433 uintptr(fileInformationClass), 434 uintptr(buf), 435 uintptr(bufsize), 436 0, 437 0, 438 ) 439 if r1 == 0 { 440 err = errnoErr(e1) 441 } 442 return 443 } 444 445 func UnlockFileEx( 446 file syscall.Handle, 447 reserved uint32, 448 bytesLow uint32, 449 bytesHigh uint32, 450 overlapped *syscall.Overlapped, 451 ) (err error) { 452 r1, _, e1 := syscall.Syscall6( 453 procUnlockFileEx.Addr(), 454 5, 455 uintptr(file), 456 uintptr(reserved), 457 uintptr(bytesLow), 458 uintptr(bytesHigh), 459 uintptr(unsafe.Pointer(overlapped)), 460 0, 461 ) 462 if r1 == 0 { 463 err = errnoErr(e1) 464 } 465 return 466 } 467 468 func VirtualQuery(address uintptr, buffer *MemoryBasicInformation, length uintptr) (err error) { 469 r1, _, e1 := syscall.Syscall( 470 procVirtualQuery.Addr(), 471 3, 472 uintptr(address), 473 uintptr(unsafe.Pointer(buffer)), 474 uintptr(length), 475 ) 476 if r1 == 0 { 477 err = errnoErr(e1) 478 } 479 return 480 } 481 482 func NetShareAdd(serverName *uint16, level uint32, buf *byte, parmErr *uint16) (neterr error) { 483 r0, _, _ := syscall.Syscall6( 484 procNetShareAdd.Addr(), 485 4, 486 uintptr(unsafe.Pointer(serverName)), 487 uintptr(level), 488 uintptr(unsafe.Pointer(buf)), 489 uintptr(unsafe.Pointer(parmErr)), 490 0, 491 0, 492 ) 493 if r0 != 0 { 494 neterr = syscall.Errno(r0) 495 } 496 return 497 } 498 499 func NetShareDel(serverName, netName *uint16, reserved uint32) (neterr error) { 500 r0, _, _ := syscall.Syscall( 501 procNetShareDel.Addr(), 502 3, 503 uintptr(unsafe.Pointer(serverName)), 504 uintptr(unsafe.Pointer(netName)), 505 uintptr(reserved), 506 ) 507 if r0 != 0 { 508 neterr = syscall.Errno(r0) 509 } 510 return 511 } 512 513 func NetUserGetLocalGroups( 514 serverName *uint16, 515 userName *uint16, 516 level uint32, 517 flags uint32, 518 buf **byte, 519 prefMaxLen uint32, 520 entriesRead *uint32, 521 totalEntries *uint32, 522 ) (neterr error) { 523 r0, _, _ := syscall.Syscall9( 524 procNetUserGetLocalGroups.Addr(), 525 8, 526 uintptr(unsafe.Pointer(serverName)), 527 uintptr(unsafe.Pointer(userName)), 528 uintptr(level), 529 uintptr(flags), 530 uintptr(unsafe.Pointer(buf)), 531 uintptr(prefMaxLen), 532 uintptr(unsafe.Pointer(entriesRead)), 533 uintptr(unsafe.Pointer(totalEntries)), 534 0, 535 ) 536 if r0 != 0 { 537 neterr = syscall.Errno(r0) 538 } 539 return 540 } 541 542 func GetProcessMemoryInfo(handle syscall.Handle, memCounters *PROCESS_MEMORY_COUNTERS, cb uint32) (err error) { 543 r1, _, e1 := syscall.Syscall( 544 procGetProcessMemoryInfo.Addr(), 545 3, 546 uintptr(handle), 547 uintptr(unsafe.Pointer(memCounters)), 548 uintptr(cb), 549 ) 550 if r1 == 0 { 551 err = errnoErr(e1) 552 } 553 return 554 } 555 556 func CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) { 557 var _p0 uint32 558 if inheritExisting { 559 _p0 = 1 560 } 561 r1, _, e1 := syscall.Syscall( 562 procCreateEnvironmentBlock.Addr(), 563 3, 564 uintptr(unsafe.Pointer(block)), 565 uintptr(token), 566 uintptr(_p0), 567 ) 568 if r1 == 0 { 569 err = errnoErr(e1) 570 } 571 return 572 } 573 574 func DestroyEnvironmentBlock(block *uint16) (err error) { 575 r1, _, e1 := syscall.Syscall(procDestroyEnvironmentBlock.Addr(), 1, uintptr(unsafe.Pointer(block)), 0, 0) 576 if r1 == 0 { 577 err = errnoErr(e1) 578 } 579 return 580 } 581 582 func GetProfilesDirectory(dir *uint16, dirLen *uint32) (err error) { 583 r1, _, e1 := syscall.Syscall( 584 procGetProfilesDirectoryW.Addr(), 585 2, 586 uintptr(unsafe.Pointer(dir)), 587 uintptr(unsafe.Pointer(dirLen)), 588 0, 589 ) 590 if r1 == 0 { 591 err = errnoErr(e1) 592 } 593 return 594 } 595 596 func WSASocket( 597 af int32, 598 typ int32, 599 protocol int32, 600 protinfo *syscall.WSAProtocolInfo, 601 group uint32, 602 flags uint32, 603 ) (handle syscall.Handle, err error) { 604 r0, _, e1 := syscall.Syscall6( 605 procWSASocketW.Addr(), 606 6, 607 uintptr(af), 608 uintptr(typ), 609 uintptr(protocol), 610 uintptr(unsafe.Pointer(protinfo)), 611 uintptr(group), 612 uintptr(flags), 613 ) 614 handle = syscall.Handle(r0) 615 if handle == syscall.InvalidHandle { 616 err = errnoErr(e1) 617 } 618 return 619 }