douji1853 2016-02-11 10:04
浏览 323

使用golang的CreateProcess

Hello I am try to call CreateProcess from syscall

func CreateProcess(appName *uint16, commandLine *uint16, procSecurity *SecurityAttributes, threadSecurity *SecurityAttributes, inheritHandles bool, creationFlags uint32, env *uint16, currentDir *uint16, startupInfo *StartupInfo, outProcInfo *ProcessInformation) (err error) 

But I got error num 123 ("The filename, directory name, or volume label syntax is incorrect."), The path of the calc.exe is correct.

package main
import (
        "fmt"
        "syscall"
)

func main() {
    var pS syscall.SecurityAttributes
    var tS syscall.SecurityAttributes
    var iH bool = true
    var cF uint32
    var env uint16
    var cD uint16
    var sI syscall.StartupInfo
    var pI syscall.ProccessInformation
    var err error

    err = syscall.CreateProcess(
        syscall.StringToUTF16Ptr("c:\\windows\\system32\\calc.exe"),
        syscall.StringToUTF16Ptr(""),
        &pS,
        &tS,
        iH,
        cF,
        &env,
        &cD,
        &sI,
        &pI)

        fmt.Printf("Return: %d
", err)
}
  • 写回答

1条回答 默认 最新

  • dqp4933 2016-02-11 16:39
    关注

    You incorrectly set parameter lpCurrentDirectory(from https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx) :

    The full path to the current directory for the process. The string can also specify a UNC path. If this parameter is NULL, the new process will have the same current drive and directory as the calling process. (This feature is provided primarily for shells that need to start an application and specify its initial drive and working directory.)

    If you make it nil, then it will work. However, if to take for a sample example from this, then your code can be rewritten as:

    package main
    
    import (
        "fmt"
        "syscall"
    )
    
    func main() {
    
        var sI syscall.StartupInfo
        var pI syscall.ProcessInformation
    
        argv := syscall.StringToUTF16Ptr("c:\\windows\\system32\\calc.exe")
    
        err := syscall.CreateProcess(
            nil,
            argv,
            nil,
            nil,
            true,
            0,
            nil,
            nil,
            &sI,
            &pI)
    
        fmt.Printf("Return: %d
    ", err)
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。