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 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错