go 获取自身路径

180it 2020-10-26 PM 64℃ 0条
package main

import (

    "fmt"
    "syscall"
    "unicode/utf16"
    "unsafe"
)

var(
    kernel=syscall.MustLoadDLL("kernel32.dll")
    getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW")
)

func getExePath() (exePath string, err error) {
    return getModuleFileName()
}

func getModuleFileName() (string, error) {
    var n uint32
    b := make([]uint16, syscall.MAX_PATH)
    size := uint32(len(b))

    r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size))
    n = uint32(r0)
    if n == 0 {
        return "", e1
    }
    return string(utf16.Decode(b[0:n])), nil
}

func main() {
    path,_:=getExePath()
    fmt.Println(path)

}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

go 获取自身路径