syscall_windows_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 TestFormatMessage(t *testing.T) {
  46. dll := windows.MustLoadDLL("netevent.dll")
  47. const TITLE_SC_MESSAGE_BOX uint32 = 0xC0001B75
  48. const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
  49. buf := make([]uint16, 300)
  50. _, err := windows.FormatMessage(flags, uintptr(dll.Handle), TITLE_SC_MESSAGE_BOX, 0, buf, nil)
  51. if err != nil {
  52. t.Fatalf("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, TITLE_SC_MESSAGE_BOX, err)
  53. }
  54. }
  55. func abort(funcname string, err error) {
  56. panic(funcname + " failed: " + err.Error())
  57. }
  58. func ExampleLoadLibrary() {
  59. h, err := windows.LoadLibrary("kernel32.dll")
  60. if err != nil {
  61. abort("LoadLibrary", err)
  62. }
  63. defer windows.FreeLibrary(h)
  64. proc, err := windows.GetProcAddress(h, "GetVersion")
  65. if err != nil {
  66. abort("GetProcAddress", err)
  67. }
  68. r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
  69. major := byte(r)
  70. minor := uint8(r >> 8)
  71. build := uint16(r >> 16)
  72. print("windows version ", major, ".", minor, " (Build ", build, ")\n")
  73. }
  74. func TestTOKEN_ALL_ACCESS(t *testing.T) {
  75. if windows.TOKEN_ALL_ACCESS != 0xF01FF {
  76. t.Errorf("TOKEN_ALL_ACCESS = %x, want 0xF01FF", windows.TOKEN_ALL_ACCESS)
  77. }
  78. }