Skip to content

Commit 4f93026

Browse files
author
Popog
committedOct 27, 2012
go fmt
1 parent 40381bd commit 4f93026

17 files changed

+3121
-3121
lines changed
 

‎advapi32.go

+164-164
Original file line numberDiff line numberDiff line change
@@ -5,217 +5,217 @@
55
package w32
66

77
import (
8-
"fmt"
9-
"syscall"
10-
"unsafe"
8+
"fmt"
9+
"syscall"
10+
"unsafe"
1111
)
1212

1313
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")
2929
)
3030

3131
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
4444
}
4545

4646
func RegCloseKey(hKey HKEY) {
47-
ret, _, _ := procRegCloseKey.Call(
48-
uintptr(hKey))
47+
ret, _, _ := procRegCloseKey.Call(
48+
uintptr(hKey))
4949

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+
}
5353
}
5454

5555
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)
8585
}
8686

8787
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)
9797
}
9898

9999
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)
112112
}
113113

114114
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)))
118118

119-
return HANDLE(ret)
119+
return HANDLE(ret)
120120
}
121121

122122
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
133133
}
134134

135135
func CloseEventLog(eventlog HANDLE) bool {
136-
ret, _, _ := procCloseEventLog.Call(
137-
uintptr(eventlog))
136+
ret, _, _ := procCloseEventLog.Call(
137+
uintptr(eventlog))
138138

139-
return ret != 0
139+
return ret != 0
140140
}
141141

142142
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
160160
}
161161

162162
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
168168
}
169169

170170
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))
175175

176-
if ret == 0 {
177-
return 0, syscall.GetLastError()
178-
}
176+
if ret == 0 {
177+
return 0, syscall.GetLastError()
178+
}
179179

180-
return HANDLE(ret), nil
180+
return HANDLE(ret), nil
181181
}
182182

183183
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
208208
}
209209

210210
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+
}
214214

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)))
219219

220-
return ret != 0
220+
return ret != 0
221221
}

0 commit comments

Comments
 (0)
Please sign in to comment.