forked from arduino/go-win32-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell32_windows.go
60 lines (52 loc) · 1.83 KB
/
shell32_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// Copyright 2018-2023 ARDUINO SA. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package win32
import (
"fmt"
"syscall"
"unsafe"
)
func init() {
if err1, err2 := procSHGetKnownFolderPath.Find(), procCoTaskMemFree.Find(); err1 != nil || err2 != nil {
procSHGetKnownFolderPath = nil
}
if err := procSHGetFolderPathW.Find(); err != nil {
procSHGetFolderPathW = nil
}
}
func getFolder(id *folderIdentifier) (string, error) {
if procSHGetKnownFolderPath != nil {
var pathptr *uint16
if err := getKnownFolderPath(id.FOLDERID, 0, 0, &pathptr); err != nil {
return "", err
}
defer taskMemFree(uintptr(unsafe.Pointer(pathptr)))
return syscall.UTF16ToString((*[65535]uint16)(unsafe.Pointer(pathptr))[:]), nil
}
if procSHGetFolderPathW != nil {
path := make([]uint16, 1024) // MAX_PATH in win32 API is defined as 260, so 1024 should be fine
if err := getFolderPath(0, id.CSIDL, 0, 0, &path[0]); err != nil {
return "", err
}
return syscall.UTF16ToString(path), nil
}
return "", fmt.Errorf("could not call shell32 API to retrieve folder")
}
// GetDocumentsFolder returns the Document folder
func GetDocumentsFolder() (string, error) {
return getFolder(documentsFolder)
}
// GetLocalAppDataFolder returns the LocalAppData folder
func GetLocalAppDataFolder() (string, error) {
return getFolder(localAppDataFolder)
}
// GetRoamingAppDataFolder returns the AppData folder
func GetRoamingAppDataFolder() (string, error) {
return getFolder(roamingAppDataFolder)
}
var documentsFolder = &folderIdentifier{FOLDERID: folderIDDocuments, CSIDL: csidlMyDocuments}
var roamingAppDataFolder = &folderIdentifier{FOLDERID: folderIDRoamingAppData, CSIDL: csidlAppData}
var localAppDataFolder = &folderIdentifier{FOLDERID: folderIDLocalAppData, CSIDL: csidlAppData}