-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: os: add a way to start process from full command line #29841
Comments
What if you pass |
@jordanrh1 Ok, it works. package main
import (
"fmt"
"os"
"os/exec"
"syscall"
)
func main() {
if len(os.Args) > 1 {
for i, arg := range os.Args[1:] {
fmt.Printf("arg #%d: %s\n", i, arg)
}
return
}
p, _ := os.Executable()
cmdLine := fmt.Sprintf(`"%s" foo "bar"`, p)
fmt.Printf("cmdLine: %s\n", cmdLine)
cmd := exec.Command("cmd.exe")
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c "%s"`, cmdLine)}
output, err := cmd.CombinedOutput()
fmt.Printf("output:\n%s\n", output)
if err != nil {
fmt.Printf("error: %+v\n", err)
}
} Output:
I think it'd be good to have this method documented somewhere. Thanks. |
BTW, this method spawns |
I'm glad it worked for you. In this case, since you're given a full command line string, I think cmd.exe is the better option since you can pass the command line string unmodified to cmd.exe, and let the system parse it. I think the overhead of spawning cmd.exe is quite small since the system does this sort of thing all the time. |
Got it, I'll close this issue. Thank you for your support. |
Currently it's not possible to start a process directly from full command line:
go/src/syscall/exec_windows.go
Lines 263 to 266 in 50bd1c4
go/src/syscall/exec_windows.go
Lines 325 to 329 in 50bd1c4
In the code above,
argv0p
wouldn't benil
, thus we can't use the functionality that CreateProcess(AsUser) API gives, by passinglpApplicationName
aNULL
to uselpCommandLine
only.Such kind of process is needed when dealing with, for example,
UninstallString
registry value. Keys underHKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
usually haveUninstallString
value, and that is just a command line string to execute when uninstalling a software(within control panel). And to execute that command line, I have to useCreateProcess
Windows API directly, without seeing benefits of usingos.StartProcess
orexec.Command
api.Even using
syscall.SysProcAttr.CmdLine
, this behavior cannot be achieved. It is still needed to parse executable path manually.Actually, there is a function
commandLineToArgv
that converts full command line intoos.StartProcess
-cousumable chunks:go/src/os/exec_windows.go
Lines 159 to 174 in ff7b245
and works well for this situation(https://play.golang.org/p/lRhDlagni41), but unfortunately it is not exported.
So it would be good to have
commandLineToArgv
function exported, or to have seperate api that acts likesystem()
function in C. Thanks.The text was updated successfully, but these errors were encountered: