-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain2.go
60 lines (56 loc) · 1.75 KB
/
main2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"strconv"
"syscall"
"unsafe"
)
func GetLogicalDrives() []string {
kernel32 := syscall.MustLoadDLL("kernel32.dll")
GetLogicalDrives := kernel32.MustFindProc("GetLogicalDrives")
n, _, _ := GetLogicalDrives.Call()
s := strconv.FormatInt(int64(n), 2)
fmt.Println(s)
var res []string
for i := len(s) - 1; i >= 0; i-- {
if s[i] == '1' {
driver := string(rune('A'+len(s)-i-1)) + ":"
res = append(res, driver)
}
}
return res
}
func GetVolumeInformation() {
// https://bbs.csdn.net/topics/350167157
// https://stackoverflow.com/questions/35238933/golang-call-getvolumeinformation-winapi-function
var RootPathName = `C:\`
var VolumeNameBuffer = make([]uint16, syscall.MAX_PATH+1)
var nVolumeNameSize = uint32(len(VolumeNameBuffer))
var VolumeSerialNumber uint32
var MaximumComponentLength uint32
var FileSystemFlags uint32
var FileSystemNameBuffer = make([]uint16, 255)
var nFileSystemNameSize uint32 = syscall.MAX_PATH + 1
kernel32, _ := syscall.LoadLibrary("kernel32.dll")
getVolume, _ := syscall.GetProcAddress(kernel32, "GetVolumeInformationW")
var nargs uintptr = 8
p, _ := syscall.UTF16PtrFromString(RootPathName)
ret, _, callErr := syscall.Syscall9(getVolume,
nargs,
uintptr(unsafe.Pointer(p)),
uintptr(unsafe.Pointer(&VolumeNameBuffer[0])),
uintptr(nVolumeNameSize),
uintptr(unsafe.Pointer(&VolumeSerialNumber)),
uintptr(unsafe.Pointer(&MaximumComponentLength)),
uintptr(unsafe.Pointer(&FileSystemFlags)),
uintptr(unsafe.Pointer(&FileSystemNameBuffer[0])),
uintptr(nFileSystemNameSize),
0)
fmt.Println(ret, callErr)
fmt.Println(syscall.UTF16ToString(VolumeNameBuffer))
fmt.Println(syscall.UTF16ToString(FileSystemNameBuffer))
}
func main() {
//fmt.Println(GetLogicalDrives())
GetVolumeInformation()
}