Shenghou Ma | 2fc67e7 | 2015-11-01 04:18:58 -0500 | [diff] [blame] | 1 | // Copyright 2016 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 os |
| 6 | |
| 7 | import ( |
| 8 | "internal/syscall/windows" |
| 9 | "syscall" |
| 10 | ) |
| 11 | |
| 12 | func getModuleFileName(handle syscall.Handle) (string, error) { |
| 13 | n := uint32(1024) |
| 14 | var buf []uint16 |
| 15 | for { |
| 16 | buf = make([]uint16, n) |
| 17 | r, err := windows.GetModuleFileName(handle, &buf[0], n) |
| 18 | if err != nil { |
| 19 | return "", err |
| 20 | } |
| 21 | if r < n { |
| 22 | break |
| 23 | } |
| 24 | // r == n means n not big enough |
| 25 | n += 1024 |
| 26 | } |
| 27 | return syscall.UTF16ToString(buf), nil |
| 28 | } |
| 29 | |
| 30 | func executable() (string, error) { |
| 31 | return getModuleFileName(0) |
| 32 | } |