sys_windows.go 452 B

12345678910111213141516171819202122
  1. package tick
  2. import "syscall"
  3. import "os"
  4. import "errors"
  5. func LockFile(file *os.File) error {
  6. h, err := syscall.LoadLibrary("kernel32.dll")
  7. if err != nil {
  8. return err
  9. }
  10. defer syscall.FreeLibrary(h)
  11. addr, err := syscall.GetProcAddress(h, "LockFile")
  12. if err != nil {
  13. return err
  14. }
  15. r0, _, _ := syscall.Syscall6(addr, 5, file.Fd(), 0, 0, 0, 1, 0)
  16. if 0 != int(r0) {
  17. return nil
  18. }
  19. return errors.New("resource temporarily unavailable")
  20. }