| 1 |
package win32 |
| 2 |
|
| 3 |
import ( |
| 4 |
"syscall" |
| 5 |
|
| 6 |
"github.com/crc-org/crc/v2/pkg/crc/logging" |
| 7 |
"golang.org/x/sys/windows" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
HwndDesktop = windows.Handle(0) |
| 12 |
) |
| 13 |
|
| 14 |
// Uses "runas" as verb to execute as Elevated privileges |
| 15 |
func ShellExecuteAsAdmin(reason string, hwnd windows.Handle, file, parameters, directory string, showCmd int) error { |
| 16 |
logging.Infof("Will run as admin: %s", reason) |
| 17 |
return ShellExecute(hwnd, "runas", file, parameters, directory, showCmd) |
| 18 |
} |
| 19 |
|
| 20 |
func toUint16ptr(input string) *uint16 { |
| 21 |
uint16ptr, err := syscall.UTF16PtrFromString(input) |
| 22 |
if err != nil { |
| 23 |
logging.Warnf("Failed to convert %s to UTF16: %v", input, err) |
| 24 |
} |
| 25 |
|
| 26 |
return uint16ptr |
| 27 |
} |
| 28 |
|
| 29 |
func ShellExecute(hwnd windows.Handle, verb, file, parameters, directory string, showCmd int) error { |
| 30 |
var op, params, dir *uint16 |
| 31 |
if len(verb) != 0 { |
| 32 |
op = toUint16ptr(verb) |
| 33 |
} |
| 34 |
if len(parameters) != 0 { |
| 35 |
params = toUint16ptr(parameters) |
| 36 |
} |
| 37 |
if len(directory) != 0 { |
| 38 |
dir = toUint16ptr(directory) |
| 39 |
} |
| 40 |
return windows.ShellExecute(hwnd, op, toUint16ptr(file), params, dir, int32(showCmd)) |
| 41 |
} |