Skip to content

Commit 20b15fb

Browse files
authored
Merge pull request #1369 from Lomanic/issue1368
[process][windows] Retrieve process name as basename of executable
2 parents 400a453 + 980cc82 commit 20b15fb

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

process/process_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func Test_Process_Name(t *testing.T) {
294294
t.Errorf("getting name error %v", err)
295295
}
296296
if !strings.Contains(n, "process.test") {
297-
t.Errorf("invalid Exe %s", n)
297+
t.Errorf("invalid Name %s", n)
298298
}
299299
}
300300

process/process_windows.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"os"
13+
"path/filepath"
1314
"reflect"
1415
"strings"
1516
"syscall"
@@ -319,18 +320,19 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
319320
}
320321

321322
func (p *Process) NameWithContext(ctx context.Context) (string, error) {
322-
ppid, _, name, err := getFromSnapProcess(p.Pid)
323-
if err != nil {
324-
return "", fmt.Errorf("could not get Name: %s", err)
323+
if p.Pid == 0 {
324+
return "System Idle Process", nil
325+
}
326+
if p.Pid == 4 {
327+
return "System", nil
325328
}
326329

327-
// if no errors and not cached already, cache ppid
328-
p.parent = ppid
329-
if 0 == p.getPpid() {
330-
p.setPpid(ppid)
330+
exe, err := p.ExeWithContext(ctx)
331+
if err != nil {
332+
return "", fmt.Errorf("could not get Name: %s", err)
331333
}
332334

333-
return name, nil
335+
return filepath.Base(exe), nil
334336
}
335337

336338
func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
@@ -987,15 +989,9 @@ func is32BitProcess(h windows.Handle) bool {
987989

988990
var procIs32Bits bool
989991
switch processorArchitecture {
990-
case PROCESSOR_ARCHITECTURE_INTEL:
991-
fallthrough
992-
case PROCESSOR_ARCHITECTURE_ARM:
992+
case PROCESSOR_ARCHITECTURE_INTEL, PROCESSOR_ARCHITECTURE_ARM:
993993
procIs32Bits = true
994-
case PROCESSOR_ARCHITECTURE_ARM64:
995-
fallthrough
996-
case PROCESSOR_ARCHITECTURE_IA64:
997-
fallthrough
998-
case PROCESSOR_ARCHITECTURE_AMD64:
994+
case PROCESSOR_ARCHITECTURE_ARM64, PROCESSOR_ARCHITECTURE_IA64, PROCESSOR_ARCHITECTURE_AMD64:
999995
var wow64 uint
1000996

1001997
ret, _, _ := common.ProcNtQueryInformationProcess.Call(

0 commit comments

Comments
 (0)