Permalink
Browse files

Windows-specific config_dir/config_file implementation.

  • Loading branch information...
nsf committed Dec 26, 2012
1 parent c6983df commit 7c679b0037e63d682e15bac01987ea5f555e6560
Showing with 38 additions and 11 deletions.
  1. +38 −11 os_windows.go
View
@@ -3,27 +3,54 @@ package main
import (
"flag"
"fmt"
+ "path/filepath"
"syscall"
- "unicode/utf16"
"unsafe"
)
+var (
+ shell32 = syscall.NewLazyDLL("shell32.dll")
+ kernel32 = syscall.NewLazyDLL("kernel32.dll")
+)
+
+var (
+ proc_sh_get_folder_path = shell32.NewProc("SHGetFolderPathW")
+ proc_get_module_file_name = kernel32.NewProc("GetModuleFileNameW")
+)
+
func create_sock_flag(name, desc string) *string {
return flag.String(name, "tcp", desc)
}
// Full path of the current executable
func get_executable_filename() string {
- kernel32, _ := syscall.LoadLibrary("kernel32.dll")
- defer syscall.FreeLibrary(kernel32)
b := make([]uint16, syscall.MAX_PATH)
- getModuleFileName, _ := syscall.GetProcAddress(kernel32, "GetModuleFileNameW")
- ret, _, callErr := syscall.Syscall(uintptr(getModuleFileName),
- 3, 0,
- uintptr(unsafe.Pointer(&b[0])),
- uintptr(len(b)))
- if callErr != 0 {
- panic(fmt.Sprintf("GetModuleFileNameW : err %d", int(callErr)))
+ ret, _, err := syscall.Syscall(proc_get_module_file_name.Addr(), 3,
+ 0, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
+ if int(ret) == 0 {
+ panic(fmt.Sprintf("GetModuleFileNameW : err %d", int(err)))
+ }
+ return syscall.UTF16ToString(b)
+}
+
+const (
+ csidl_appdata = 0x1a
+)
+
+func get_appdata_folder_path() string {
+ b := make([]uint16, syscall.MAX_PATH)
+ ret, _, err := syscall.Syscall6(proc_sh_get_folder_path.Addr(), 5,
+ 0, csidl_appdata, 0, 0, uintptr(unsafe.Pointer(&b[0])), 0)
+ if int(ret) != 0 {
+ panic(fmt.Sprintf("SHGetFolderPathW : err %d", int(err)))
}
- return string(utf16.Decode(b[:ret]))
+ return syscall.UTF16ToString(b)
+}
+
+func config_dir() string {
+ return filepath.Join(get_appdata_folder_path(), "gocode")
+}
+
+func config_file() string {
+ return filepath.Join(get_appdata_folder_path(), "gocode", "config.json")
}

0 comments on commit 7c679b0

Please sign in to comment.