syscall_windows_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "testing"
  10. "code.google.com/p/go.sys/windows"
  11. )
  12. func TestWin32finddata(t *testing.T) {
  13. dir, err := ioutil.TempDir("", "go-build")
  14. if err != nil {
  15. t.Fatalf("failed to create temp directory: %v", err)
  16. }
  17. defer os.RemoveAll(dir)
  18. path := filepath.Join(dir, "long_name.and_extension")
  19. f, err := os.Create(path)
  20. if err != nil {
  21. t.Fatalf("failed to create %v: %v", path, err)
  22. }
  23. f.Close()
  24. type X struct {
  25. fd windows.Win32finddata
  26. got byte
  27. pad [10]byte // to protect ourselves
  28. }
  29. var want byte = 2 // it is unlikely to have this character in the filename
  30. x := X{got: want}
  31. pathp, _ := windows.UTF16PtrFromString(path)
  32. h, err := windows.FindFirstFile(pathp, &(x.fd))
  33. if err != nil {
  34. t.Fatalf("FindFirstFile failed: %v", err)
  35. }
  36. err = windows.FindClose(h)
  37. if err != nil {
  38. t.Fatalf("FindClose failed: %v", err)
  39. }
  40. if x.got != want {
  41. t.Fatalf("memory corruption: want=%d got=%d", want, x.got)
  42. }
  43. }
  44. func abort(funcname string, err error) {
  45. panic(funcname + " failed: " + err.Error())
  46. }
  47. func ExampleLoadLibrary() {
  48. h, err := windows.LoadLibrary("kernel32.dll")
  49. if err != nil {
  50. abort("LoadLibrary", err)
  51. }
  52. defer windows.FreeLibrary(h)
  53. proc, err := windows.GetProcAddress(h, "GetVersion")
  54. if err != nil {
  55. abort("GetProcAddress", err)
  56. }
  57. r, _, _ := windows.Syscall(uintptr(proc), 0, 0, 0, 0)
  58. major := byte(r)
  59. minor := uint8(r >> 8)
  60. build := uint16(r >> 16)
  61. print("windows version ", major, ".", minor, " (Build ", build, ")\n")
  62. }