You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
star/uac/uac.go

76 lines
1.4 KiB
Go

2 years ago
//go:build windows
package uac
import (
"b612.me/wincmd"
"strings"
)
func RunAsUacSimple(fpath string, cmdArgs string, workDir string, showWindow bool) error {
intShow := 0
if showWindow {
intShow = 1
}
return wincmd.StartProcess(fpath, cmdArgs, workDir, true, intShow)
}
func RunAsUac(cmdLine, workDir string, showWindow bool) error {
intShow := 0
if showWindow {
intShow = 1
}
fpath, args := getPathArgsFromString(cmdLine)
return wincmd.StartProcess(fpath, strings.Join(args, " "), workDir, true, intShow)
}
func getPathArgsFromString(cmdLine string) (string, []string) {
var fpath string
var cmdArgs []string
var markStart rune = -1
var tmp []rune
var lastRune rune
cmdLine = strings.TrimSpace(cmdLine)
for k, v := range cmdLine {
if v == ' ' || v == '"' || v == '\'' {
if k == 0 {
markStart = v
continue
}
if markStart == v && v == lastRune {
continue
}
if lastRune != '\\' {
lastRune = v
if 0 == markStart {
markStart = v
continue
} else if markStart == v || markStart == -1 {
markStart = 0
if v == ' ' {
markStart = v
}
if fpath == "" {
fpath = string(tmp)
} else {
cmdArgs = append(cmdArgs, string(tmp))
}
tmp = []rune{}
continue
}
}
}
lastRune = v
tmp = append(tmp, v)
}
if len(tmp) != 0 {
if fpath == "" {
fpath = string(tmp)
} else {
cmdArgs = append(cmdArgs, string(tmp))
}
}
return fpath, cmdArgs
}