forked from AllenDang/w32
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvapi32.go
190 lines (160 loc) · 5.27 KB
/
advapi32.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2010 The W32 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package w32
import (
"fmt"
"syscall"
"unsafe"
)
var (
modadvapi32 = syscall.NewLazyDLL("advapi32.dll")
procRegOpenKeyEx = modadvapi32.NewProc("RegOpenKeyExW")
procRegCloseKey = modadvapi32.NewProc("RegCloseKey")
procRegGetValue = modadvapi32.NewProc("RegGetValueW")
procRegEnumKeyEx = modadvapi32.NewProc("RegEnumKeyExW")
procRegSetKeyValue = modadvapi32.NewProc("RegSetKeyValueW")
procOpenSCManager = modadvapi32.NewProc("OpenSCManagerW")
procCloseServiceHandle = modadvapi32.NewProc("CloseServiceHandle")
procOpenService = modadvapi32.NewProc("OpenServiceW")
procStartService = modadvapi32.NewProc("StartServiceW")
procControlService = modadvapi32.NewProc("ControlService")
)
func RegOpenKeyEx(hKey HKEY, subKey string, samDesired uint32) HKEY {
var result HKEY
ret, _, _ := procRegOpenKeyEx.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))),
uintptr(0),
uintptr(samDesired),
uintptr(unsafe.Pointer(&result)))
if ret != ERROR_SUCCESS {
panic(fmt.Sprintf("RegOpenKeyEx(%d, %s, %d) failed", hKey, subKey, samDesired))
}
return result
}
func RegCloseKey(hKey HKEY) {
ret, _, _ := procRegCloseKey.Call(
uintptr(hKey))
if ret != ERROR_SUCCESS {
panic(fmt.Sprintf("RegCloseKey(%d) failed", hKey))
}
}
func RegGetString(hKey HKEY, subKey string, value string) string {
var bufLen uint32
procRegGetValue.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))),
uintptr(RRF_RT_REG_SZ),
0,
0,
uintptr(unsafe.Pointer(&bufLen)))
if bufLen == 0 {
return ""
}
buf := make([]uint16, bufLen)
ret, _, _ := procRegGetValue.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))),
uintptr(RRF_RT_REG_SZ),
0,
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&bufLen)))
if ret != ERROR_SUCCESS {
return ""
}
return syscall.UTF16ToString(buf)
}
func RegSetKeyValue(hKey HKEY, subKey string, valueName string, dwType DWORD, data uintptr, cbData uint16) (errno int) {
ret, _, _ := procRegSetKeyValue.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(valueName))),
uintptr(dwType),
data,
uintptr(cbData))
return int(ret)
}
func RegEnumKeyEx(hKey HKEY, index DWORD) string {
var bufLen uint32 = 255
buf := make([]uint16, bufLen)
procRegEnumKeyEx.Call(
uintptr(hKey),
uintptr(index),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&bufLen)),
0,
0,
0,
0)
return syscall.UTF16ToString(buf)
}
func OpenSCManager(lpMachineName, lpDatabaseName string, dwDesiredAccess DWORD) (HANDLE, error) {
var p1, p2 uintptr
if len(lpMachineName) > 0 {
p1 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpMachineName)))
}
if len(lpDatabaseName) > 0 {
p2 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpDatabaseName)))
}
ret, _, _ := procOpenSCManager.Call(
p1,
p2,
uintptr(dwDesiredAccess))
if ret == 0 {
return 0, syscall.GetLastError()
}
return HANDLE(ret), nil
}
func CloseServiceHandle(hSCObject HANDLE) error {
ret, _, _ := procCloseServiceHandle.Call(uintptr(hSCObject))
if ret == 0 {
return syscall.GetLastError()
}
return nil
}
func OpenService(hSCManager HANDLE, lpServiceName string, dwDesiredAccess DWORD) (HANDLE, error) {
ret, _, _ := procOpenService.Call(
uintptr(hSCManager),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceName))),
uintptr(dwDesiredAccess))
if ret == 0 {
return 0, syscall.GetLastError()
}
return HANDLE(ret), nil
}
func StartService(hService HANDLE, lpServiceArgVectors []string) error {
l := len(lpServiceArgVectors)
var ret uintptr
if l == 0 {
ret, _, _ = procStartService.Call(
uintptr(hService),
0,
0)
} else {
lpArgs := make([]uintptr, l)
for i := 0; i < l; i++ {
lpArgs[i] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceArgVectors[i])))
}
ret, _, _ = procStartService.Call(
uintptr(hService),
uintptr(l),
uintptr(unsafe.Pointer(&lpArgs[0])))
}
if ret == 0 {
return syscall.GetLastError()
}
return nil
}
func ControlService(hService HANDLE, dwControl DWORD, lpServiceStatus *SERVICE_STATUS) bool {
if lpServiceStatus == nil {
panic("ControlService:lpServiceStatus cannot be nil")
}
ret, _, _ := procControlService.Call(
uintptr(hService),
uintptr(dwControl),
uintptr(unsafe.Pointer(lpServiceStatus)))
return ret != 0
}