github.com/slantview/etcdctl@v0.1.3-0.20131011185546-5aaeca137f94/third_party/bitbucket.org/kardianos/osext/osext_windows.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package osext 6 7 import ( 8 "syscall" 9 "unicode/utf16" 10 "unsafe" 11 ) 12 13 var ( 14 kernel = syscall.MustLoadDLL("kernel32.dll") 15 getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW") 16 ) 17 18 // GetModuleFileName() with hModule = NULL 19 func executable() (exePath string, err error) { 20 return getModuleFileName() 21 } 22 23 func getModuleFileName() (string, error) { 24 var n uint32 25 b := make([]uint16, syscall.MAX_PATH) 26 size := uint32(len(b)) 27 28 r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size)) 29 n = uint32(r0) 30 if n == 0 { 31 return "", e1 32 } 33 return string(utf16.Decode(b[0:n])), nil 34 }