-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelevate.go
More file actions
83 lines (71 loc) · 1.51 KB
/
elevate.go
File metadata and controls
83 lines (71 loc) · 1.51 KB
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
package main
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
const (
SEE_MASK_NOCLOSEPROCESS = 0x00000040
SW_NORMAL = 1
)
type SHELLEXECUTEINFO struct {
CbSize uint32
FMask uint32
Hwnd uintptr
LpVerb *uint16
LpFile *uint16
LpParameters *uint16
LpDirectory *uint16
NShow int32
HInstApp uintptr
LpIDList uintptr
LpClass *uint16
HkeyClass uintptr
DwHotKey uint32
HIcon uintptr
HProcess uintptr
}
func ShellExecuteEx(info *SHELLEXECUTEINFO) error {
r, _, err := syscall.SyscallN(
windows.NewLazySystemDLL("shell32.dll").NewProc("ShellExecuteExW").Addr(),
uintptr(unsafe.Pointer(info)),
)
if r == 0 {
return err
}
return nil
}
func elevate(app, params, cwd string) error {
verbPtr, err := windows.UTF16PtrFromString("runas")
if err != nil {
return err
}
appPtr, err := windows.UTF16PtrFromString(app)
if err != nil {
return err
}
paramsPtr, err := windows.UTF16PtrFromString(params)
if err != nil {
return err
}
dirPtr, err := windows.UTF16PtrFromString(cwd)
if err != nil {
return err
}
execInfo := &SHELLEXECUTEINFO{
CbSize: uint32(unsafe.Sizeof(SHELLEXECUTEINFO{})),
FMask: SEE_MASK_NOCLOSEPROCESS,
Hwnd: 0,
LpVerb: verbPtr,
LpFile: appPtr,
LpParameters: paramsPtr,
LpDirectory: dirPtr,
NShow: SW_NORMAL,
}
err = ShellExecuteEx(execInfo)
if err != nil {
return fmt.Errorf("ShellExecuteEx error: %v", err)
}
return nil
}