J. smith
J. smith

Reputation: 31

CreateProcess with golang

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\n", err)
}

Upvotes: 3

Views: 5387

Answers (1)

user4651282
user4651282

Reputation:

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\n", err)
} 

Upvotes: 4

Related Questions