Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 获取固定驱动器的列表_Windows_Go_Winapi_System Calls_Ffi - Fatal编程技术网

Windows 获取固定驱动器的列表

Windows 获取固定驱动器的列表,windows,go,winapi,system-calls,ffi,Windows,Go,Winapi,System Calls,Ffi,如何获得仅用于物理驱动器的所有装载点的列表?我看到这里有一个类似的答案,但这列出了包括网络共享在内的所有装载点 好的,我决定磨练一下我的Win32 API编程技能,准备一个解决方案 基于lame方法的解决方案如下: 主程序包 进口( “错误” “fmt” “日志” “系统调用” “不安全” ) 变量( kernel32=syscall.NewLazyDLL(“kernel32.dll”) getDriveTypeWProc=kernel32.NewProc(“GetDriveTypeW”) )

如何获得仅用于物理驱动器的所有装载点的列表?我看到这里有一个类似的答案,但这列出了包括网络共享在内的所有装载点


好的,我决定磨练一下我的Win32 API编程技能,准备一个解决方案

基于lame方法的解决方案如下:

主程序包
进口(
“错误”
“fmt”
“日志”
“系统调用”
“不安全”
)
变量(
kernel32=syscall.NewLazyDLL(“kernel32.dll”)
getDriveTypeWProc=kernel32.NewProc(“GetDriveTypeW”)
)
func getDriveType(rootPathName[]uint16)(int,错误){
rc,,:=getDriveTypeWProc.Call(
uintptr(不安全的.Pointer(&rootPathName[0])),
)
dt:=int(rc)
如果dt==driveUnknown | | dt==driveNoRootDir{
返回-1,driveTypeErrors[dt]
}
返回dt,零
}
变量(
errUnknownDriveType=错误。新建(“未知驱动器类型”)
errNoRootDir=错误。新建(“无效的根驱动器路径”)
driveTypeErrors=[…]错误{
0:errUnknownDriveType,
1:errNoRootDir,
}
)
常数(
驱动器未知=iota
驾驶室
可拆卸驱动器
固定驱动器
远程驾驶
驱动器光盘
磁盘驱动器
)
func getFixedDOSDrives()([]字符串,错误){
var驱动器=[4]uint16{
1: ':',
2: '\\',
}
var驱动器[]字符串

对于c:='A';c这是一种基于Eryk Sun在评论中建议的改进方法

代码
主程序包
进口(
“错误”
“日志”
“字符串”
“系统调用”
“不安全”
)
func main(){
装载,错误:=getFixedDriveMounts()
如果错误!=零{
log.Fatal(错误)
}
对于Um,m:=范围安装{
log.Println(“卷:”,m.volume,
“mounts:”,strings.Join(m.mounts,”,“”)
}
}
变量(
kernel32=syscall.NewLazyDLL(“kernel32.dll”)
findFirstVolumeWProc=kernel32.NewProc(“FindFirstVolumeW”)
findNextVolumeWProc=kernel32.NewProc(“FindNextVolumeW”)
findVolumeCloseProc=kernel32.NewProc(“FindVolumeClose”)
getVolumePathNamesForVolumeNameWProc=kernel32.NewProc(“GetVolumePathNamesforVolumeName”)
getDriveTypeWProc=kernel32.NewProc(“GetDriveTypeW”)
)
const guidBufLen=syscall.MAX_PATH+1
func findFirstVolume()(uintpttr,[]uint16,错误){
const invalidHandleValue=^uintpttr(0)
guid:=make([]uint16,guidBufLen)
handle,389;,err:=findFirstVolumeWProc.Call(
uintptr(不安全的.指针(&guid[0]),
uintptr(guidBufLen*2),
)
如果handle==invalidHandleValue{
返回invalidHandleValue、nil、err
}
返回句柄,guid,nil
}
func findNextVolume(句柄uintpttr)([]uint16,bool,error){
常量noMoreFiles=18
guid:=make([]uint16,guidBufLen)
rc,u,err:=findNextVolumeWProc.Call(
手柄
uintptr(不安全的.指针(&guid[0]),
uintptr(guidBufLen*2),
)
如果rc==1{
返回guid,true,nil
}
if err.(syscall.Errno)=noMoreFiles{
返回零,假,零
}
返回nil、false、err
}
func findVolumeClose(句柄UINTPTTR)错误{
好的,错误:=findVolumeCloseProc.Call(句柄)
如果ok==0{
返回错误
}
归零
}
func getVolumePathNamesForVolumeName(volName[]uint16)([]uint16,错误){
常数(
errorMoreData=234
NUL=0x0000
)
变量(
路径名称uint32
路径名[]uint16
)
路径名称len=2
为了{
路径名=make([]uint16,路径名len)
路径名称长度*=2
rc,u2;,err:=getVolumePathNamesForVolumeNameWProc.Call(
uintptr(不安全的.Pointer(&volName[0]),
uintptr(不安全的.指针(&路径名[0]),
uintptr(路径名称),
uintptr(不安全的.指针(&pathNamesLen)),
)
如果rc==0{
如果错误。(syscall.Errno)=errorMoreData{
持续
}
返回零,错误
}
路径名=路径名[:pathNamesLen]
打破
}
var out[]uint16
i:=0
对于j,c:=范围路径名{
如果c==NUL&&i0{
out=追加(out,fixedVolumeMounts{
卷:syscall.UTF16ToString(guid),
安装件:LPSTRsToStrings(安装件),
})
}
归零
})
如果错误!=零{
返回零,错误
}
返回,零
}
func枚举卷(handleVolume func(guid[]uint16)错误)错误{
句柄,guid,错误:=findFirstVolume()
如果错误!=零{
返回错误
}
延迟函数(){
err=findVolumeClose(句柄)
}()
如果错误:=handleVolume(guid);错误!=nil{
返回错误
}
为了{
guid,更多,错误:=findNextVolume(句柄)
如果出错
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:\
tmp$ GOOS=windows go build fvs.go
tmp$ wine64 ./fvs.exe 
0009:fixme:process:SetProcessPriorityBoost (0xffffffffffffffff,1): stub
2020/07/09 22:48:25 volume: \\?\Volume{00000000-0000-0000-0000-000000000043}\ mounts: C:\
2020/07/09 22:48:25 volume: \\?\Volume{00000000-0000-0000-0000-000000000044}\ mounts: D:\
2020/07/09 22:48:25 volume: \\?\Volume{00000000-0000-0000-0000-00000000005a}\ mounts: Z:\
2020/07/09 22:48:25 volume: \\?\Volume{169203c7-20c7-4ca6-aaec-19a806b9b81e}\ mounts: X:\