Skip to content

Commit 7c679b0

Browse files
committed
Windows-specific config_dir/config_file implementation.
1 parent c6983df commit 7c679b0

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

os_windows.go

+38-11
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,54 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"path/filepath"
67
"syscall"
7-
"unicode/utf16"
88
"unsafe"
99
)
1010

11+
var (
12+
shell32 = syscall.NewLazyDLL("shell32.dll")
13+
kernel32 = syscall.NewLazyDLL("kernel32.dll")
14+
)
15+
16+
var (
17+
proc_sh_get_folder_path = shell32.NewProc("SHGetFolderPathW")
18+
proc_get_module_file_name = kernel32.NewProc("GetModuleFileNameW")
19+
)
20+
1121
func create_sock_flag(name, desc string) *string {
1222
return flag.String(name, "tcp", desc)
1323
}
1424

1525
// Full path of the current executable
1626
func get_executable_filename() string {
17-
kernel32, _ := syscall.LoadLibrary("kernel32.dll")
18-
defer syscall.FreeLibrary(kernel32)
1927
b := make([]uint16, syscall.MAX_PATH)
20-
getModuleFileName, _ := syscall.GetProcAddress(kernel32, "GetModuleFileNameW")
21-
ret, _, callErr := syscall.Syscall(uintptr(getModuleFileName),
22-
3, 0,
23-
uintptr(unsafe.Pointer(&b[0])),
24-
uintptr(len(b)))
25-
if callErr != 0 {
26-
panic(fmt.Sprintf("GetModuleFileNameW : err %d", int(callErr)))
28+
ret, _, err := syscall.Syscall(proc_get_module_file_name.Addr(), 3,
29+
0, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
30+
if int(ret) == 0 {
31+
panic(fmt.Sprintf("GetModuleFileNameW : err %d", int(err)))
32+
}
33+
return syscall.UTF16ToString(b)
34+
}
35+
36+
const (
37+
csidl_appdata = 0x1a
38+
)
39+
40+
func get_appdata_folder_path() string {
41+
b := make([]uint16, syscall.MAX_PATH)
42+
ret, _, err := syscall.Syscall6(proc_sh_get_folder_path.Addr(), 5,
43+
0, csidl_appdata, 0, 0, uintptr(unsafe.Pointer(&b[0])), 0)
44+
if int(ret) != 0 {
45+
panic(fmt.Sprintf("SHGetFolderPathW : err %d", int(err)))
2746
}
28-
return string(utf16.Decode(b[:ret]))
47+
return syscall.UTF16ToString(b)
48+
}
49+
50+
func config_dir() string {
51+
return filepath.Join(get_appdata_folder_path(), "gocode")
52+
}
53+
54+
func config_file() string {
55+
return filepath.Join(get_appdata_folder_path(), "gocode", "config.json")
2956
}

0 commit comments

Comments
 (0)