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
5package windows_test
6
7import (
8	"io/ioutil"
9	"os"
10	"path/filepath"
11	"syscall"
12	"testing"
13	"unsafe"
14
15	"golang.org/x/sys/windows"
16)
17
18func TestWin32finddata(t *testing.T) {
19	dir, err := ioutil.TempDir("", "go-build")
20	if err != nil {
21		t.Fatalf("failed to create temp directory: %v", err)
22	}
23	defer os.RemoveAll(dir)
24
25	path := filepath.Join(dir, "long_name.and_extension")
26	f, err := os.Create(path)
27	if err != nil {
28		t.Fatalf("failed to create %v: %v", path, err)
29	}
30	f.Close()
31
32	type X struct {
33		fd  windows.Win32finddata
34		got byte
35		pad [10]byte // to protect ourselves
36
37	}
38	var want byte = 2 // it is unlikely to have this character in the filename
39	x := X{got: want}
40
41	pathp, _ := windows.UTF16PtrFromString(path)
42	h, err := windows.FindFirstFile(pathp, &(x.fd))
43	if err != nil {
44		t.Fatalf("FindFirstFile failed: %v", err)
45	}
46	err = windows.FindClose(h)
47	if err != nil {
48		t.Fatalf("FindClose failed: %v", err)
49	}
50
51	if x.got != want {
52		t.Fatalf("memory corruption: want=%d got=%d", want, x.got)
53	}
54}
55
56func TestFormatMessage(t *testing.T) {
57	dll := windows.MustLoadDLL("pdh.dll")
58
59	pdhOpenQuery := func(datasrc *uint16, userdata uint32, query *windows.Handle) (errno uintptr) {
60		r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhOpenQueryW").Addr(), 3, uintptr(unsafe.Pointer(datasrc)), uintptr(userdata), uintptr(unsafe.Pointer(query)))
61		return r0
62	}
63
64	pdhCloseQuery := func(query windows.Handle) (errno uintptr) {
65		r0, _, _ := syscall.Syscall(dll.MustFindProc("PdhCloseQuery").Addr(), 1, uintptr(query), 0, 0)
66		return r0
67	}
68
69	var q windows.Handle
70	name, err := windows.UTF16PtrFromString("no_such_source")
71	if err != nil {
72		t.Fatal(err)
73	}
74	errno := pdhOpenQuery(name, 0, &q)
75	if errno == 0 {
76		pdhCloseQuery(q)
77		t.Fatal("PdhOpenQuery succeeded, but expected to fail.")
78	}
79
80	const flags uint32 = syscall.FORMAT_MESSAGE_FROM_HMODULE | syscall.FORMAT_MESSAGE_ARGUMENT_ARRAY | syscall.FORMAT_MESSAGE_IGNORE_INSERTS
81	buf := make([]uint16, 300)
82	_, err = windows.FormatMessage(flags, uintptr(dll.Handle), uint32(errno), 0, buf, nil)
83	if err != nil {
84		t.Fatalf("FormatMessage for handle=%x and errno=%x failed: %v", dll.Handle, errno, err)
85	}
86}
87
88func abort(funcname string, err error) {
89	panic(funcname + " failed: " + err.Error())
90}
91
92func ExampleLoadLibrary() {
93	h, err := windows.LoadLibrary("kernel32.dll")
94	if err != nil {
95		abort("LoadLibrary", err)
96	}
97	defer windows.FreeLibrary(h)
98	proc, err := windows.GetProcAddress(h, "GetVersion")
99	if err != nil {
100		abort("GetProcAddress", err)
101	}
102	r, _, _ := syscall.Syscall(uintptr(proc), 0, 0, 0, 0)
103	major := byte(r)
104	minor := uint8(r >> 8)
105	build := uint16(r >> 16)
106	print("windows version ", major, ".", minor, " (Build ", build, ")\n")
107}
108