CreateProcess with golang

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

CreateProcess with golang

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "syscall"
  5. )
  6. func main() {
  7. var pS syscall.SecurityAttributes
  8. var tS syscall.SecurityAttributes
  9. var iH bool = true
  10. var cF uint32
  11. var env uint16
  12. var cD uint16
  13. var sI syscall.StartupInfo
  14. var pI syscall.ProccessInformation
  15. var err error
  16. err = syscall.CreateProcess(
  17. syscall.StringToUTF16Ptr("c:\\windows\\system32\\calc.exe"),
  18. syscall.StringToUTF16Ptr(""),
  19. &pS,
  20. &tS,
  21. iH,
  22. cF,
  23. &env,
  24. &cD,
  25. &sI,
  26. &pI)
  27. fmt.Printf("Return: %d\n", err)
  28. }

希望对你有帮助!

英文:

Hello I am try to call CreateProcess from syscall

  1. 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.

  1. package main
  2. import (
  3. "fmt"
  4. "syscall"
  5. )
  6. func main() {
  7. var pS syscall.SecurityAttributes
  8. var tS syscall.SecurityAttributes
  9. var iH bool = true
  10. var cF uint32
  11. var env uint16
  12. var cD uint16
  13. var sI syscall.StartupInfo
  14. var pI syscall.ProccessInformation
  15. var err error
  16. err = syscall.CreateProcess(
  17. syscall.StringToUTF16Ptr("c:\\windows\\system32\\calc.exe"),
  18. syscall.StringToUTF16Ptr(""),
  19. &pS,
  20. &tS,
  21. iH,
  22. cF,
  23. &env,
  24. &cD,
  25. &sI,
  26. &pI)
  27. fmt.Printf("Return: %d\n", err)
  28. }

答案1

得分: 4

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

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "syscall"
  5. )
  6. func main() {
  7. var sI syscall.StartupInfo
  8. var pI syscall.ProcessInformation
  9. argv := syscall.StringToUTF16Ptr("c:\\windows\\system32\\calc.exe")
  10. err := syscall.CreateProcess(
  11. nil,
  12. argv,
  13. nil,
  14. nil,
  15. true,
  16. 0,
  17. nil,
  18. nil,
  19. &sI,
  20. &pI)
  21. fmt.Printf("Return: %d\n", err)
  22. }
英文:

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:

  1. package main
  2. import (
  3. "fmt"
  4. "syscall"
  5. )
  6. func main() {
  7. var sI syscall.StartupInfo
  8. var pI syscall.ProcessInformation
  9. argv := syscall.StringToUTF16Ptr("c:\\windows\\system32\\calc.exe")
  10. err := syscall.CreateProcess(
  11. nil,
  12. argv,
  13. nil,
  14. nil,
  15. true,
  16. 0,
  17. nil,
  18. nil,
  19. &sI,
  20. &pI)
  21. fmt.Printf("Return: %d\n", err)
  22. }

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:

确定