github.com/hy3/cuto@v0.9.8-0.20160830082821-aa6652f877b7/util/path_windows.go (about)

     1  // Copyright 2015 unirita Inc.
     2  // Created 2015/04/10 shanxia
     3  
     4  package util
     5  
     6  import (
     7  	"fmt"
     8  	"path/filepath"
     9  	"unsafe"
    10  )
    11  
    12  var (
    13  	modulePath = getModulePath()
    14  )
    15  
    16  const max_path = 520
    17  
    18  // Rootフォルダを取得する(実行ファイルはRootパス下のbinフォルダにあると想定)
    19  func GetRootPath() string {
    20  	return filepath.Dir(filepath.Dir(modulePath))
    21  }
    22  
    23  // 現在のフォルダパスを返す。
    24  func GetCurrentPath() string {
    25  	return filepath.Dir(modulePath)
    26  }
    27  
    28  func getModulePath() string {
    29  	// MAX_PATHがUTF-8になる場合は、これくらいあれば十分か?
    30  	var buf [max_path]byte
    31  	procGetModuleFileNameW.Call(0, uintptr(unsafe.Pointer(&buf)), (uintptr)(max_path))
    32  
    33  	// Unicodeで取得しているので、2byte目が0の部分を除外する。
    34  	var path [max_path / 2]byte
    35  	var j int
    36  	for i := 0; i < len(buf); i++ {
    37  		if buf[i] != 0 {
    38  			path[j] = buf[i]
    39  			j++
    40  		}
    41  	}
    42  	return fmt.Sprintf("%s", path)
    43  }