Skip to content

x/sys/windows: GetDriveType should call "GetDriveTypeW" #23121

@zosmac

Description

@zosmac

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
uname -a
Darwin [elided.com] 17.3.0 Darwin Kernel Version 17.3.0: Thu Nov 9 18:09:22 PST 2017; root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64

What did you do?

// getdrivetype project main.go
package main

import (
	"fmt"
	"path/filepath"
	"unsafe"

	"golang.org/x/sys/windows"
)

var (
	modkernel32      = windows.NewLazySystemDLL("kernel32.dll")
	procGetDriveType = modkernel32.NewProc("GetDriveTypeW")
)

func main() {
	path, _ := filepath.Abs(".")
	wpath, _ := windows.UTF16PtrFromString(path)
	wvolume := make([]uint16, len(path)+1)
	if err := windows.GetVolumePathName(wpath, &wvolume[0], uint32(len(wvolume))); err != nil {
		fmt.Println(err)
		return
	}
	volume := windows.UTF16ToString(wvolume)

	dtp, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(&wvolume[0])))
	fmt.Printf("path: %s volume: %s type: %s\n", path, volume, driveType(uint32(dtp)))

	dt32 := windows.GetDriveType(&wvolume[0])
	fmt.Printf("drive type for %s is %s\n", path, driveType(dt32))
}

func driveType(dt uint32) string {
	switch dt {
	case windows.DRIVE_CDROM:
		return "cd-rom"
	case windows.DRIVE_FIXED:
		return "fixed"
	case windows.DRIVE_NO_ROOT_DIR:
		return "no-root-dir"
	case windows.DRIVE_RAMDISK:
		return "ram-disk"
	case windows.DRIVE_REMOTE:
		return "remote"
	case windows.DRIVE_REMOVABLE:
		return "removable"
	case windows.DRIVE_UNKNOWN:
		return "unknown"
	}
	return "unrecognized"
}

What did you expect to see?

path: c:\Users\zosmac volume: c:\ type: fixed
path: c:\Users\zosmac volume: c:\ type: fixed

What did you see instead?

c:\Users\zosmac>u:\bin\getdrivetype.exe
path: c:\Users\zosmac volume: c:\ type: fixed
panic: Failed to find GetDriveType procedure in kernel32.dll: The specified procedure could not be found.

goroutine 1 [running]:
golang.org/x/sys/windows.(*LazyProc).mustFind(0xc04206bc80)
        /Users/zosmac/go/src/golang.org/x/sys/windows/dll_windows.go:286 +0x5f
golang.org/x/sys/windows.(*LazyProc).Addr(0xc04206bc80, 0x4d977c)
        /Users/zosmac/go/src/golang.org/x/sys/windows/dll_windows.go:294 +0x32
golang.org/x/sys/windows.GetDriveType(0xc04200a460, 0x1d)
        /Users/zosmac/go/src/golang.org/x/sys/windows/zsyscall_windows.go:1965 +0x34
main.main()
        /Users/zosmac/go/src/tests/getdrivetype/main.go:30 +0x341

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions