dongshuogai2343 2018-06-17 09:32
浏览 122
已采纳

如何通过exec os / exec执行sc create命令?

How can i execute my sc command through exec.Command?

Code:

cmd := exec.Command("cmd.exe", "sc", "create", "Simpler", "binpath="+os.Getenv("APPDATA")+"\\Simpler\\cc.exe", "displayname=MY SERVICE");
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true};
out, _ := cmd.Output(); fmt.Println(string(out))

This i get in command prompt (open command prompt in current command prompt -_-):

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>cd c:\prog\logs

c:\prog\logs>go build test.go

c:\prog\logs>test.exe
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

c:\prog\logs>

c:\prog\logs>

Followed command cannot be executed. I do not get anything! Where is the problem?

Command been started if i execute sc.exe in current command prompt but not started if i execute in new command prompt.

That is if i remove first argument "cmd.exe" my program succesfully execute with excepted result. But how to execute in a new window?

  • 写回答

2条回答 默认 最新

  • dongwo5589 2018-06-17 11:13
    关注

    Well, the immediate problem is that what you do is not how cmd.exe should be told to execute an external command—in order to make it execute one, you should pass cmd.exe either the /C or /K command-line option, like in

    C:\>cmd.exe /C sc whatever sc_arg1 sc_arg2 ...
    

    What you observe with your call is that cmd.exe ignores the rest of the arguments you pass to it and just runs—that's exactly what you're observing.

    Start with studying the output of cmd.exe /? and the built-in Windows help system to get better idea of how it works.

    The next problem is that there is absolutely no reason to make cmd.exe involved in running sc—just run sc directly rather than jumping around cmd.exe to make it execute sc.

    The next problem is that you're ignoring the error value returned by the call to os/exec.Cmd.Output. Since Go (luckily) does not have exceptions, handing all error values is of paramount importance—it's better to ignore everything else but not errors. If you're writing highly-experimental code, you might start with a dumb helper like

    func check(err error) {
        if err != nil {
            panic(err)
        }
    }
    

    and then roll like

    out, err := cmd.Output()
    check(err)
    

    This approach is not for production-grade code, but allows for faster code churn when experimenting.

    You might also consider calling directly into Win32 API to manage services as it will make error handling substantially easier.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘