CreateProcess with golang

huangapple go评论82阅读模式
英文:

CreateProcess with golang

问题

你好,我会帮你翻译这段代码。以下是翻译的结果:

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)
}

希望对你有帮助!

英文:

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)
}

答案1

得分: 4

你错误地设置了参数lpCurrentDirectory(来自https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx):

进程的当前目录的完整路径。该字符串还可以指定一个UNC路径。
如果此参数为NULL,则新进程将具有与调用进程相同的当前驱动器和目录。(此功能主要用于需要启动应用程序并指定其初始驱动器和工作目录的shell。)

如果将其设置为nil,则会起作用。
然而,如果以这个为例,那么你的代码可以重写为:

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)
} 
英文:

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)
} 

huangapple
  • 本文由 发表于 2016年2月11日 18:04:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/35336131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定