github.com/gonutz/osext@v0.0.0-20171206091709-9e22409d6bb6/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 //+build !go1.8 6 7 package osext 8 9 import ( 10 "syscall" 11 "unicode/utf16" 12 "unsafe" 13 ) 14 15 var ( 16 kernel = syscall.MustLoadDLL("kernel32.dll") 17 getModuleFileNameProc = kernel.MustFindProc("GetModuleFileNameW") 18 ) 19 20 // GetModuleFileName() with hModule = NULL 21 func executable() (exePath string, err error) { 22 return getModuleFileName() 23 } 24 25 func getModuleFileName() (string, error) { 26 var n uint32 27 b := make([]uint16, syscall.MAX_PATH) 28 size := uint32(len(b)) 29 30 r0, _, e1 := getModuleFileNameProc.Call(0, uintptr(unsafe.Pointer(&b[0])), uintptr(size)) 31 n = uint32(r0) 32 if n == 0 { 33 return "", e1 34 } 35 return string(utf16.Decode(b[0:n])), nil 36 }