Skip to content

Instantly share code, notes, and snippets.

@kostix
Created July 6, 2020 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kostix/e3d1d3072f034c46cc84a1991117f692 to your computer and use it in GitHub Desktop.
Save kostix/e3d1d3072f034c46cc84a1991117f692 to your computer and use it in GitHub Desktop.
Get fixed DOS drives in Windows
package main
import (
"errors"
"fmt"
"log"
"syscall"
"unsafe"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
getDriveTypeWProc = kernel32.NewProc("GetDriveTypeW")
)
func getDriveType(rootPathName []uint16) (int, error) {
rc, _, _ := getDriveTypeWProc.Call(
uintptr(unsafe.Pointer(&rootPathName[0])),
)
dt := int(rc)
if dt == driveUnknown || dt == driveNoRootDir {
return -1, driveTypeErrors[dt]
}
return dt, nil
}
var (
errUnknownDriveType = errors.New("unknown drive type")
errNoRootDir = errors.New("invalid root drive path")
driveTypeErrors = [...]error{
0: errUnknownDriveType,
1: errNoRootDir,
}
)
const (
driveUnknown = iota
driveNoRootDir
driveRemovable
driveFixed
driveRemote
driveCDROM
driveRamdisk
)
func getFixedDOSDrives() ([]string, error) {
var drive = [4]uint16{
1: ':',
2: '\\',
}
var drives []string
for c := 'A'; c <= 'Z'; c++ {
drive[0] = uint16(c)
dt, err := getDriveType(drive[:])
if err != nil {
if err == errNoRootDir {
continue
}
return nil, fmt.Errorf("error getting type of: %s: %s",
syscall.UTF16ToString(drive[:]), err)
}
if dt != driveFixed {
continue
}
drives = append(drives, syscall.UTF16ToString(drive[:]))
}
return drives, nil
}
func main() {
drives, err := getFixedDOSDrives()
if err != nil {
log.Fatal(err)
}
for _, drive := range drives {
log.Println(drive)
}
}
@kostix
Copy link
Author

kostix commented Jul 6, 2020

Running on by box (under Wine 4.0) I get:

tmp$ GOOS=windows go build drvs.go 
tmp$ wine64 ./drvs.exe
0009:fixme:process:SetProcessPriorityBoost (0xffffffffffffffff,1): stub
2020/07/06 21:06:02 C:\
2020/07/06 21:06:02 D:\
2020/07/06 21:06:02 X:\
2020/07/06 21:06:02 Z:\

(All drives are mapped using winecfg.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment