syscall_windows_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package windows_test
  5. import (
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "syscall"
  10. "testing"
  11. "golang.org/x/sys/windows"
  12. )
  13. func TestWin32finddata(t *testing.T) {
  14. dir, err := ioutil.TempDir("", "go-build")
  15. if err != nil {
  16. t.Fatalf("failed to create temp directory: %v", err)
  17. }
  18. defer os.RemoveAll(dir)
  19. path := filepath.Join(dir, "long_name.and_extension")
  20. f, err := os.Create(path)
  21. if err != nil {
  22. t.Fatalf("failed to create %v: %v", path, err)
  23. }
  24. f.Close()
  25. type X struct {
  26. fd windows.Win32finddata
  27. got byte
  28. pad [10]byte // to protect ourselves
  29. }
  30. var want byte = 2 // it is unlikely to have this character in the filename
  31. x := X{got: want}
  32. pathp, _ := windows.UTF16PtrFromString(path)
  33. h, err := windows.FindFirstFile(pathp, &(x.fd))
  34. if err != nil {
  35. t.Fatalf("FindFirstFile failed: %v", err)
  36. }
  37. err = windows.FindClose(h)
  38. if err != nil {
  39. t.Fatalf("FindClose failed: %v", err)
  40. }
  41. if x.got != want {
  42. t.Fatalf("memory corruption: want=%d got=%d", want, x.got)
  43. }
  44. }
  45. func abort(funcname string, err error) {
  46. panic(funcname + " failed: " + err.Error())
  47. }
  48. func ExampleLoadLibrary() {
  49. h, err := windows.LoadLibrary("kernel32.dll")
  50. if err != nil {
  51. abort("LoadLibrary", err)
  52. }
  53. defer windows.FreeLibrary(h)
  54. proc, err := windows.GetProcAddress(h, "GetVersion")
  55. if err != nil {
  56. abort("GetProcAddress", err)
  57. }
  58. r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
  59. major := byte(r)
  60. minor := uint8(r >> 8)
  61. build := uint16(r >> 16)
  62. print("windows version ", major, ".", minor, " (Build ", build, ")\n")
  63. }