@@ -3,27 +3,54 @@ package main
3
3
import (
4
4
"flag"
5
5
"fmt"
6
+ "path/filepath"
6
7
"syscall"
7
- "unicode/utf16"
8
8
"unsafe"
9
9
)
10
10
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
+
11
21
func create_sock_flag (name , desc string ) * string {
12
22
return flag .String (name , "tcp" , desc )
13
23
}
14
24
15
25
// Full path of the current executable
16
26
func get_executable_filename () string {
17
- kernel32 , _ := syscall .LoadLibrary ("kernel32.dll" )
18
- defer syscall .FreeLibrary (kernel32 )
19
27
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 )))
27
46
}
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" )
29
56
}
0 commit comments