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