golang1.7中Windows修改盤符小程式

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
package main// #include <stdlib.h>import "C"import (    "bytes"    "errors"    "os"    "syscall"    "unsafe")var (    success                = "The operation completed successfully."    kernel                 = syscall.NewLazyDLL("Kernel32.dll")    setVolumeMountPoint    = kernel.NewProc("SetVolumeMountPointA")    getValumeNameForMount  = kernel.NewProc("GetVolumeNameForVolumeMountPointA")    deleteVolumeMountPoint = kernel.NewProc("DeleteVolumeMountPointA"))func main() {    defer syscall.CloseHandle(syscall.Handle(kernel.Handle()))    if len(os.Args) != 3 {        usage()    }    if os.Args[1] == "-d" {        let := os.Args[2]        if len(let) != 1 {            println("盤符不正確,確認盤符格式:-d D格式")            return        }        let += ":\\"        err := delVolumeMount(let)        if err != nil {            println(err.Error())        }        return    }    err := ChangeLetter(os.Args[1], os.Args[2])    if err != nil {        println(err.Error())    }}//ChangeLetter("E","G")func ChangeLetter(oldLetter, newLetter string) error {    if len(oldLetter) > 1 || len(newLetter) != 1 {        return errors.New("盤符不正確,確認盤符格式:C,D格式")    }    oldLetter += ":\\"    newLetter += ":\\"    volumeID, err := getValumeName(oldLetter)    if err != nil {        return err    }    println("擷取到 volume id:", volumeID)    err = delVolumeMount(oldLetter)    if err != nil {        return err    }    return setVolumeMount(newLetter, volumeID)}//getValumeName("D:\\")func getValumeName(letter string) (string, error) {    char := C.CString(letter)    defer C.free(unsafe.Pointer(char))    n := make([]byte, 254)    _, _, err := getValumeNameForMount.Call(uintptr(unsafe.Pointer(char)), uintptr(unsafe.Pointer(&n[0])), 254)    if err.Error() != "More data is available." {        return "", err    }    return string(n[:bytes.Index(n, []byte{0})]), nil}//setVolumeMount("G:\\", `\\?\Volume{3a7aa2c9-5dfc-11e6-99d9-806e6f6e6963}\`)func setVolumeMount(letter, volumeName string) error {    path := C.CString(letter)    defer C.free(unsafe.Pointer(path))    v := C.CString(volumeName)    defer C.free(unsafe.Pointer(v))    _, _, err := setVolumeMountPoint.Call(uintptr(unsafe.Pointer(path)), uintptr(unsafe.Pointer(v)))    if err.Error() != success {        return err    }    return nil}//delVolumeMount("D:\\")func delVolumeMount(letter string) error {    p := C.CString(letter)    defer C.free(unsafe.Pointer(p))    _, _, err := deleteVolumeMountPoint.Call(uintptr(unsafe.Pointer(p)))    if err.Error() != success {        return err    }    return nil}func usage() {    println(os.Args[0] + ` usage:    刪除一個盤符:` + os.Args[0] + " -d E" + `    修改一個盤符:` + os.Args[0] + " D E")    os.Exit(1)}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.