Merge pull request #1796 from RomainMuller/master

fix: `Process.CmdlineSliceWithContext` incorrect on Windows
pull/1794/head
shirou 3 weeks ago committed by GitHub
commit 4f2f472f0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -408,6 +408,11 @@ func (p *Process) Cmdline() (string, error) {
// CmdlineSlice returns the command line arguments of the process as a slice with each
// element being an argument.
//
// On Windows, this assumes the command line is encoded according to the convention accepted by
// [golang.org/x/sys/windows.CmdlineToArgv] (the most common convention). If this is not suitable,
// you should instead use [Process.Cmdline] and parse the command line according to your specific
// requirements.
func (p *Process) CmdlineSlice() ([]string, error) {
return p.CmdlineSliceWithContext(context.Background())
}

@ -12,7 +12,6 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"syscall"
"time"
"unicode/utf16"
@ -381,7 +380,27 @@ func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error)
if err != nil {
return nil, err
}
return strings.Split(cmdline, " "), nil
return parseCmdline(cmdline)
}
func parseCmdline(cmdline string) ([]string, error) {
cmdlineptr, err := windows.UTF16PtrFromString(cmdline)
if err != nil {
return nil, err
}
var argc int32
argvptr, err := windows.CommandLineToArgv(cmdlineptr, &argc)
if err != nil {
return nil, err
}
defer windows.LocalFree(windows.Handle(uintptr(unsafe.Pointer(argvptr))))
argv := make([]string, argc)
for i, v := range (*argvptr)[:argc] {
argv[i] = windows.UTF16ToString((*v)[:])
}
return argv, nil
}
func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {

Loading…
Cancel
Save