diff --git a/system/syscall_unix.go b/system/syscall_unix.go index f1497c5..3ae9128 100644 --- a/system/syscall_unix.go +++ b/system/syscall_unix.go @@ -9,3 +9,9 @@ import "syscall" func Unmount(dest string) error { return syscall.Unmount(dest, 0) } + +// CommandLineToArgv should not be used on Unix. +// It simply returns commandLine in the only element in the returned array. +func CommandLineToArgv(commandLine string) ([]string, error) { + return []string{commandLine}, nil +} diff --git a/system/syscall_windows.go b/system/syscall_windows.go index 273aa23..061e220 100644 --- a/system/syscall_windows.go +++ b/system/syscall_windows.go @@ -3,6 +3,7 @@ package system import ( "fmt" "syscall" + "unsafe" ) // OSVersion is a wrapper for Windows version information @@ -34,3 +35,26 @@ func GetOSVersion() (OSVersion, error) { func Unmount(dest string) error { return nil } + +// CommandLineToArgv wraps the Windows syscall to turn a commandline into an argument array. +func CommandLineToArgv(commandLine string) ([]string, error) { + var argc int32 + + argsPtr, err := syscall.UTF16PtrFromString(commandLine) + if err != nil { + return nil, err + } + + argv, err := syscall.CommandLineToArgv(argsPtr, &argc) + if err != nil { + return nil, err + } + defer syscall.LocalFree(syscall.Handle(uintptr(unsafe.Pointer(argv)))) + + newArgs := make([]string, argc) + for i, v := range (*argv)[:argc] { + newArgs[i] = string(syscall.UTF16ToString((*v)[:])) + } + + return newArgs, nil +}