dongtan9066 2018-02-26 06:51
浏览 309

使用exec仅在后台启动程序

I am trying to make a go program that calls another program (with a GUI) by command line.

Using os/exec package. The external program starts, but it always runs in the background. (Even if I try to start "Notepad.exe" or a .bat file which then calls the actual program.)

In the task manager you can see the process but there no way to interact with it.

Here's an example of code I've been experimenting with:

cmd := exec.Command("cmd.exe", "/C", "start", "\"\"", `Notepad.exe`)

log.Println("cmd.exe", "/C", "start", "\"\"", `Notepad.exe`)

cmd.Stdout = os.Stdout
if err := cmd.Start(); err != nil {
    log.Println("Error: ", err)
}

Is there a way to alter the code so it starts "Notepad.exe" in a way it can be interacted with via the UI?

I am trying this with a program on Windows compiled on Mac.

  • 写回答

1条回答 默认 最新

  • doufen2769 2018-02-26 08:53
    关注

    You don't need that empty "" parameter, simply use:

    cmd := exec.Command("cmd.exe", "/C", "start", "notepad.exe")
    

    Also notepad.exe won't write anything to its standard output, so you don't need to set it. Simply run it like:

    s := []string{"cmd.exe", "/C", "start", "notepad.exe"}
    log.Println("Starting", s)
    
    cmd := exec.Command(s[0], s[1:]...)
    if err := cmd.Run(); err != nil {
        log.Println("Error:", err)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料