|
5 | 5 | package w32
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "fmt" |
9 |
| - "syscall" |
10 |
| - "unsafe" |
| 8 | + "fmt" |
| 9 | + "syscall" |
| 10 | + "unsafe" |
11 | 11 | )
|
12 | 12 |
|
13 | 13 | var (
|
14 |
| - modadvapi32 = syscall.NewLazyDLL("advapi32.dll") |
15 |
| - |
16 |
| - procRegOpenKeyEx = modadvapi32.NewProc("RegOpenKeyExW") |
17 |
| - procRegCloseKey = modadvapi32.NewProc("RegCloseKey") |
18 |
| - procRegGetValue = modadvapi32.NewProc("RegGetValueW") |
19 |
| - procRegEnumKeyEx = modadvapi32.NewProc("RegEnumKeyExW") |
20 |
| - procRegSetKeyValue = modadvapi32.NewProc("RegSetKeyValueW") |
21 |
| - procOpenEventLog = modadvapi32.NewProc("OpenEventLogW") |
22 |
| - procReadEventLog = modadvapi32.NewProc("ReadEventLogW") |
23 |
| - procCloseEventLog = modadvapi32.NewProc("CloseEventLog") |
24 |
| - procOpenSCManager = modadvapi32.NewProc("OpenSCManagerW") |
25 |
| - procCloseServiceHandle = modadvapi32.NewProc("CloseServiceHandle") |
26 |
| - procOpenService = modadvapi32.NewProc("OpenServiceW") |
27 |
| - procStartService = modadvapi32.NewProc("StartServiceW") |
28 |
| - procControlService = modadvapi32.NewProc("ControlService") |
| 14 | + modadvapi32 = syscall.NewLazyDLL("advapi32.dll") |
| 15 | + |
| 16 | + procRegOpenKeyEx = modadvapi32.NewProc("RegOpenKeyExW") |
| 17 | + procRegCloseKey = modadvapi32.NewProc("RegCloseKey") |
| 18 | + procRegGetValue = modadvapi32.NewProc("RegGetValueW") |
| 19 | + procRegEnumKeyEx = modadvapi32.NewProc("RegEnumKeyExW") |
| 20 | + procRegSetKeyValue = modadvapi32.NewProc("RegSetKeyValueW") |
| 21 | + procOpenEventLog = modadvapi32.NewProc("OpenEventLogW") |
| 22 | + procReadEventLog = modadvapi32.NewProc("ReadEventLogW") |
| 23 | + procCloseEventLog = modadvapi32.NewProc("CloseEventLog") |
| 24 | + procOpenSCManager = modadvapi32.NewProc("OpenSCManagerW") |
| 25 | + procCloseServiceHandle = modadvapi32.NewProc("CloseServiceHandle") |
| 26 | + procOpenService = modadvapi32.NewProc("OpenServiceW") |
| 27 | + procStartService = modadvapi32.NewProc("StartServiceW") |
| 28 | + procControlService = modadvapi32.NewProc("ControlService") |
29 | 29 | )
|
30 | 30 |
|
31 | 31 | func RegOpenKeyEx(hKey HKEY, subKey string, samDesired uint32) HKEY {
|
32 |
| - var result HKEY |
33 |
| - ret, _, _ := procRegOpenKeyEx.Call( |
34 |
| - uintptr(hKey), |
35 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
36 |
| - uintptr(0), |
37 |
| - uintptr(samDesired), |
38 |
| - uintptr(unsafe.Pointer(&result))) |
39 |
| - |
40 |
| - if ret != ERROR_SUCCESS { |
41 |
| - panic(fmt.Sprintf("RegOpenKeyEx(%d, %s, %d) failed", hKey, subKey, samDesired)) |
42 |
| - } |
43 |
| - return result |
| 32 | + var result HKEY |
| 33 | + ret, _, _ := procRegOpenKeyEx.Call( |
| 34 | + uintptr(hKey), |
| 35 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
| 36 | + uintptr(0), |
| 37 | + uintptr(samDesired), |
| 38 | + uintptr(unsafe.Pointer(&result))) |
| 39 | + |
| 40 | + if ret != ERROR_SUCCESS { |
| 41 | + panic(fmt.Sprintf("RegOpenKeyEx(%d, %s, %d) failed", hKey, subKey, samDesired)) |
| 42 | + } |
| 43 | + return result |
44 | 44 | }
|
45 | 45 |
|
46 | 46 | func RegCloseKey(hKey HKEY) {
|
47 |
| - ret, _, _ := procRegCloseKey.Call( |
48 |
| - uintptr(hKey)) |
| 47 | + ret, _, _ := procRegCloseKey.Call( |
| 48 | + uintptr(hKey)) |
49 | 49 |
|
50 |
| - if ret != ERROR_SUCCESS { |
51 |
| - panic(fmt.Sprintf("RegCloseKey(%d) failed", hKey)) |
52 |
| - } |
| 50 | + if ret != ERROR_SUCCESS { |
| 51 | + panic(fmt.Sprintf("RegCloseKey(%d) failed", hKey)) |
| 52 | + } |
53 | 53 | }
|
54 | 54 |
|
55 | 55 | func RegGetString(hKey HKEY, subKey string, value string) string {
|
56 |
| - var bufLen uint32 |
57 |
| - procRegGetValue.Call( |
58 |
| - uintptr(hKey), |
59 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
60 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), |
61 |
| - uintptr(RRF_RT_REG_SZ), |
62 |
| - 0, |
63 |
| - 0, |
64 |
| - uintptr(unsafe.Pointer(&bufLen))) |
65 |
| - |
66 |
| - if bufLen == 0 { |
67 |
| - return "" |
68 |
| - } |
69 |
| - |
70 |
| - buf := make([]uint16, bufLen) |
71 |
| - ret, _, _ := procRegGetValue.Call( |
72 |
| - uintptr(hKey), |
73 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
74 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), |
75 |
| - uintptr(RRF_RT_REG_SZ), |
76 |
| - 0, |
77 |
| - uintptr(unsafe.Pointer(&buf[0])), |
78 |
| - uintptr(unsafe.Pointer(&bufLen))) |
79 |
| - |
80 |
| - if ret != ERROR_SUCCESS { |
81 |
| - return "" |
82 |
| - } |
83 |
| - |
84 |
| - return syscall.UTF16ToString(buf) |
| 56 | + var bufLen uint32 |
| 57 | + procRegGetValue.Call( |
| 58 | + uintptr(hKey), |
| 59 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
| 60 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), |
| 61 | + uintptr(RRF_RT_REG_SZ), |
| 62 | + 0, |
| 63 | + 0, |
| 64 | + uintptr(unsafe.Pointer(&bufLen))) |
| 65 | + |
| 66 | + if bufLen == 0 { |
| 67 | + return "" |
| 68 | + } |
| 69 | + |
| 70 | + buf := make([]uint16, bufLen) |
| 71 | + ret, _, _ := procRegGetValue.Call( |
| 72 | + uintptr(hKey), |
| 73 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
| 74 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(value))), |
| 75 | + uintptr(RRF_RT_REG_SZ), |
| 76 | + 0, |
| 77 | + uintptr(unsafe.Pointer(&buf[0])), |
| 78 | + uintptr(unsafe.Pointer(&bufLen))) |
| 79 | + |
| 80 | + if ret != ERROR_SUCCESS { |
| 81 | + return "" |
| 82 | + } |
| 83 | + |
| 84 | + return syscall.UTF16ToString(buf) |
85 | 85 | }
|
86 | 86 |
|
87 | 87 | func RegSetKeyValue(hKey HKEY, subKey string, valueName string, dwType DWORD, data uintptr, cbData uint16) (errno int) {
|
88 |
| - ret, _, _ := procRegSetKeyValue.Call( |
89 |
| - uintptr(hKey), |
90 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
91 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(valueName))), |
92 |
| - uintptr(dwType), |
93 |
| - data, |
94 |
| - uintptr(cbData)) |
95 |
| - |
96 |
| - return int(ret) |
| 88 | + ret, _, _ := procRegSetKeyValue.Call( |
| 89 | + uintptr(hKey), |
| 90 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(subKey))), |
| 91 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(valueName))), |
| 92 | + uintptr(dwType), |
| 93 | + data, |
| 94 | + uintptr(cbData)) |
| 95 | + |
| 96 | + return int(ret) |
97 | 97 | }
|
98 | 98 |
|
99 | 99 | func RegEnumKeyEx(hKey HKEY, index DWORD) string {
|
100 |
| - var bufLen uint32 = 255 |
101 |
| - buf := make([]uint16, bufLen) |
102 |
| - procRegEnumKeyEx.Call( |
103 |
| - uintptr(hKey), |
104 |
| - uintptr(index), |
105 |
| - uintptr(unsafe.Pointer(&buf[0])), |
106 |
| - uintptr(unsafe.Pointer(&bufLen)), |
107 |
| - 0, |
108 |
| - 0, |
109 |
| - 0, |
110 |
| - 0) |
111 |
| - return syscall.UTF16ToString(buf) |
| 100 | + var bufLen uint32 = 255 |
| 101 | + buf := make([]uint16, bufLen) |
| 102 | + procRegEnumKeyEx.Call( |
| 103 | + uintptr(hKey), |
| 104 | + uintptr(index), |
| 105 | + uintptr(unsafe.Pointer(&buf[0])), |
| 106 | + uintptr(unsafe.Pointer(&bufLen)), |
| 107 | + 0, |
| 108 | + 0, |
| 109 | + 0, |
| 110 | + 0) |
| 111 | + return syscall.UTF16ToString(buf) |
112 | 112 | }
|
113 | 113 |
|
114 | 114 | func OpenEventLog(servername, sourcename *uint16) HANDLE {
|
115 |
| - ret, _, _ := procOpenEventLog.Call( |
116 |
| - uintptr(unsafe.Pointer(servername)), |
117 |
| - uintptr(unsafe.Pointer(sourcename))) |
| 115 | + ret, _, _ := procOpenEventLog.Call( |
| 116 | + uintptr(unsafe.Pointer(servername)), |
| 117 | + uintptr(unsafe.Pointer(sourcename))) |
118 | 118 |
|
119 |
| - return HANDLE(ret) |
| 119 | + return HANDLE(ret) |
120 | 120 | }
|
121 | 121 |
|
122 | 122 | func ReadEventLog(eventlog HANDLE, readflags, recordoffset uint32, buffer []byte, numberofbytestoread uint32, bytesread, minnumberofbytesneeded *uint32) bool {
|
123 |
| - ret, _, _ := procReadEventLog.Call( |
124 |
| - uintptr(eventlog), |
125 |
| - uintptr(readflags), |
126 |
| - uintptr(recordoffset), |
127 |
| - uintptr(unsafe.Pointer(&buffer[0])), |
128 |
| - uintptr(numberofbytestoread), |
129 |
| - uintptr(unsafe.Pointer(bytesread)), |
130 |
| - uintptr(unsafe.Pointer(minnumberofbytesneeded))) |
131 |
| - |
132 |
| - return ret != 0 |
| 123 | + ret, _, _ := procReadEventLog.Call( |
| 124 | + uintptr(eventlog), |
| 125 | + uintptr(readflags), |
| 126 | + uintptr(recordoffset), |
| 127 | + uintptr(unsafe.Pointer(&buffer[0])), |
| 128 | + uintptr(numberofbytestoread), |
| 129 | + uintptr(unsafe.Pointer(bytesread)), |
| 130 | + uintptr(unsafe.Pointer(minnumberofbytesneeded))) |
| 131 | + |
| 132 | + return ret != 0 |
133 | 133 | }
|
134 | 134 |
|
135 | 135 | func CloseEventLog(eventlog HANDLE) bool {
|
136 |
| - ret, _, _ := procCloseEventLog.Call( |
137 |
| - uintptr(eventlog)) |
| 136 | + ret, _, _ := procCloseEventLog.Call( |
| 137 | + uintptr(eventlog)) |
138 | 138 |
|
139 |
| - return ret != 0 |
| 139 | + return ret != 0 |
140 | 140 | }
|
141 | 141 |
|
142 | 142 | func OpenSCManager(lpMachineName, lpDatabaseName string, dwDesiredAccess DWORD) (HANDLE, error) {
|
143 |
| - var p1, p2 uintptr |
144 |
| - if len(lpMachineName) > 0 { |
145 |
| - p1 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpMachineName))) |
146 |
| - } |
147 |
| - if len(lpDatabaseName) > 0 { |
148 |
| - p2 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpDatabaseName))) |
149 |
| - } |
150 |
| - ret, _, _ := procOpenSCManager.Call( |
151 |
| - p1, |
152 |
| - p2, |
153 |
| - uintptr(dwDesiredAccess)) |
154 |
| - |
155 |
| - if ret == 0 { |
156 |
| - return 0, syscall.GetLastError() |
157 |
| - } |
158 |
| - |
159 |
| - return HANDLE(ret), nil |
| 143 | + var p1, p2 uintptr |
| 144 | + if len(lpMachineName) > 0 { |
| 145 | + p1 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpMachineName))) |
| 146 | + } |
| 147 | + if len(lpDatabaseName) > 0 { |
| 148 | + p2 = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpDatabaseName))) |
| 149 | + } |
| 150 | + ret, _, _ := procOpenSCManager.Call( |
| 151 | + p1, |
| 152 | + p2, |
| 153 | + uintptr(dwDesiredAccess)) |
| 154 | + |
| 155 | + if ret == 0 { |
| 156 | + return 0, syscall.GetLastError() |
| 157 | + } |
| 158 | + |
| 159 | + return HANDLE(ret), nil |
160 | 160 | }
|
161 | 161 |
|
162 | 162 | func CloseServiceHandle(hSCObject HANDLE) error {
|
163 |
| - ret, _, _ := procCloseServiceHandle.Call(uintptr(hSCObject)) |
164 |
| - if ret == 0 { |
165 |
| - return syscall.GetLastError() |
166 |
| - } |
167 |
| - return nil |
| 163 | + ret, _, _ := procCloseServiceHandle.Call(uintptr(hSCObject)) |
| 164 | + if ret == 0 { |
| 165 | + return syscall.GetLastError() |
| 166 | + } |
| 167 | + return nil |
168 | 168 | }
|
169 | 169 |
|
170 | 170 | func OpenService(hSCManager HANDLE, lpServiceName string, dwDesiredAccess DWORD) (HANDLE, error) {
|
171 |
| - ret, _, _ := procOpenService.Call( |
172 |
| - uintptr(hSCManager), |
173 |
| - uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceName))), |
174 |
| - uintptr(dwDesiredAccess)) |
| 171 | + ret, _, _ := procOpenService.Call( |
| 172 | + uintptr(hSCManager), |
| 173 | + uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceName))), |
| 174 | + uintptr(dwDesiredAccess)) |
175 | 175 |
|
176 |
| - if ret == 0 { |
177 |
| - return 0, syscall.GetLastError() |
178 |
| - } |
| 176 | + if ret == 0 { |
| 177 | + return 0, syscall.GetLastError() |
| 178 | + } |
179 | 179 |
|
180 |
| - return HANDLE(ret), nil |
| 180 | + return HANDLE(ret), nil |
181 | 181 | }
|
182 | 182 |
|
183 | 183 | func StartService(hService HANDLE, lpServiceArgVectors []string) error {
|
184 |
| - l := len(lpServiceArgVectors) |
185 |
| - var ret uintptr |
186 |
| - if l == 0 { |
187 |
| - ret, _, _ = procStartService.Call( |
188 |
| - uintptr(hService), |
189 |
| - 0, |
190 |
| - 0) |
191 |
| - } else { |
192 |
| - lpArgs := make([]uintptr, l) |
193 |
| - for i := 0; i < l; i++ { |
194 |
| - lpArgs[i] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceArgVectors[i]))) |
195 |
| - } |
196 |
| - |
197 |
| - ret, _, _ = procStartService.Call( |
198 |
| - uintptr(hService), |
199 |
| - uintptr(l), |
200 |
| - uintptr(unsafe.Pointer(&lpArgs[0]))) |
201 |
| - } |
202 |
| - |
203 |
| - if ret == 0 { |
204 |
| - return syscall.GetLastError() |
205 |
| - } |
206 |
| - |
207 |
| - return nil |
| 184 | + l := len(lpServiceArgVectors) |
| 185 | + var ret uintptr |
| 186 | + if l == 0 { |
| 187 | + ret, _, _ = procStartService.Call( |
| 188 | + uintptr(hService), |
| 189 | + 0, |
| 190 | + 0) |
| 191 | + } else { |
| 192 | + lpArgs := make([]uintptr, l) |
| 193 | + for i := 0; i < l; i++ { |
| 194 | + lpArgs[i] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpServiceArgVectors[i]))) |
| 195 | + } |
| 196 | + |
| 197 | + ret, _, _ = procStartService.Call( |
| 198 | + uintptr(hService), |
| 199 | + uintptr(l), |
| 200 | + uintptr(unsafe.Pointer(&lpArgs[0]))) |
| 201 | + } |
| 202 | + |
| 203 | + if ret == 0 { |
| 204 | + return syscall.GetLastError() |
| 205 | + } |
| 206 | + |
| 207 | + return nil |
208 | 208 | }
|
209 | 209 |
|
210 | 210 | func ControlService(hService HANDLE, dwControl DWORD, lpServiceStatus *SERVICE_STATUS) bool {
|
211 |
| - if lpServiceStatus == nil { |
212 |
| - panic("ControlService:lpServiceStatus cannot be nil") |
213 |
| - } |
| 211 | + if lpServiceStatus == nil { |
| 212 | + panic("ControlService:lpServiceStatus cannot be nil") |
| 213 | + } |
214 | 214 |
|
215 |
| - ret, _, _ := procControlService.Call( |
216 |
| - uintptr(hService), |
217 |
| - uintptr(dwControl), |
218 |
| - uintptr(unsafe.Pointer(lpServiceStatus))) |
| 215 | + ret, _, _ := procControlService.Call( |
| 216 | + uintptr(hService), |
| 217 | + uintptr(dwControl), |
| 218 | + uintptr(unsafe.Pointer(lpServiceStatus))) |
219 | 219 |
|
220 |
| - return ret != 0 |
| 220 | + return ret != 0 |
221 | 221 | }
|
0 commit comments