forked from AllenDang/w32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwp.go
130 lines (115 loc) · 3.21 KB
/
uwp.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package wutil
import (
"strconv"
"strings"
"unsafe"
"github.com/yinyajiang/go-w32"
)
//UwpAppDesc ...
type UwpAppDesc struct {
WorkDir string
ContainerName string
DisplayName string
PackageName string
Desc string
ExePath string
}
//LoadUWPDesc ...
func LoadUWPDesc(name string) (bret bool, desc UwpAppDesc) {
defer func(n string) {
if !bret {
bret, desc = loadUWPDescFromReg(n)
}
}(name)
if !w32.IsHightWin10() {
return
}
var dwNumPublicAppCs w32.DWORD
var pPublicAppCs w32.PINET_FIREWALL_APP_CONTAINER
w32.NetworkIsolationEnumAppContainers(0, &dwNumPublicAppCs, &pPublicAppCs)
if pPublicAppCs != nil {
defer w32.NetworkIsolationFreeAppContainers(pPublicAppCs)
}
appCsIndex := func(pPublicAppCs w32.PINET_FIREWALL_APP_CONTAINER, index int) w32.PINET_FIREWALL_APP_CONTAINER {
return w32.PINET_FIREWALL_APP_CONTAINER(w32.PrtIndex(pPublicAppCs, index))
}
for i := 0; i < int(dwNumPublicAppCs); i++ {
publicAppCs := appCsIndex(pPublicAppCs, i)
if nil == unsafe.Pointer((publicAppCs.AppContainerName)) {
continue
}
tmpName := w32.UTF16PtrToString(publicAppCs.AppContainerName)
if -1 == strings.Index(tmpName, name) {
continue
}
desc.WorkDir = w32.UTF16PtrToString(publicAppCs.WorkingDirectory)
desc.ContainerName = w32.UTF16PtrToString(publicAppCs.AppContainerName)
desc.DisplayName = w32.UTF16PtrToString(publicAppCs.DisplayName)
desc.PackageName = w32.UTF16PtrToString(publicAppCs.PackageFullName)
desc.Desc = w32.UTF16PtrToString(publicAppCs.Description)
if strings.HasSuffix(desc.WorkDir, "/") || strings.HasSuffix(desc.WorkDir, `\`) {
desc.WorkDir = desc.WorkDir[0 : len(desc.WorkDir)-1]
}
desc.ExePath = desc.WorkDir + `\` + desc.DisplayName + ".exe"
bret = true
break
}
return
}
func loadUWPDescFromReg(name string) (bRet bool, desc UwpAppDesc) {
if len(name) == 0 {
return
}
hRootKey := w32.HKEY_CURRENT_USER
strRegPath := "SOFTWARE\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\AppModel\\Repository\\Packages"
hMainKey := w32.RegOpenKeyEx(hRootKey, strRegPath, w32.KEY_READ)
if 0 == hMainKey {
return
}
defer w32.RegCloseKey(hMainKey)
sys := "x" + strconv.Itoa(w32.GetSysBit())
subpath := ""
for dwIndex := 0; ; dwIndex++ {
val := w32.RegEnumKeyEx(hMainKey, uint32(dwIndex))
if len(val) == 0 {
break
}
if -1 == strings.Index(val, name) {
continue
}
if -1 == strings.Index(val, sys) {
continue
}
subpath = val
break
}
if len(subpath) == 0 {
return
}
fullRegPath := strRegPath + "\\" + subpath
_, wzBuff := w32.RegGetRawAll(hRootKey, fullRegPath, "PackageID")
if len(wzBuff) > 0 {
desc.PackageName = w32.UTF16ByteToString(wzBuff)
} else {
desc.PackageName = subpath
}
_, wzBuff = w32.RegGetRawAll(hRootKey, fullRegPath, "DisplayName")
if len(wzBuff) > 0 {
desc.DisplayName = w32.UTF16ByteToString(wzBuff)
desc.Desc = desc.DisplayName
}
_, wzBuff = w32.RegGetRawAll(hRootKey, fullRegPath, "PackageRootFolder")
if len(wzBuff) > 0 {
desc.WorkDir = w32.UTF16ByteToString(wzBuff)
} else {
return
}
if len(desc.DisplayName) > 0 {
desc.ExePath = desc.WorkDir + "\\" + desc.DisplayName + ".exe"
if !w32.IsExist(desc.ExePath) {
desc.ExePath = ""
}
}
bRet = true
return
}