@@ -22,17 +22,9 @@ import (
22
22
"fmt"
23
23
"os"
24
24
"os/exec"
25
-
26
- "github.com/StackExchange/wmi"
27
- "github.com/version-fox/vfox/internal/env"
25
+ "strings"
28
26
)
29
27
30
- type Win32_Process struct {
31
- ExecutablePath string
32
- CommandLine string
33
- ProcessId uint32
34
- }
35
-
36
28
type windowsProcess struct {}
37
29
38
30
var process = windowsProcess {}
@@ -42,44 +34,26 @@ func GetProcess() Process {
42
34
}
43
35
44
36
func (w windowsProcess ) Open (pid int ) error {
45
- // Check if shell has hooks configured
46
- if ! env .IsHookEnv () {
47
- return handleNoHookFallback (pid )
48
- }
49
-
50
- // Query WMI for process info
51
- var processes []Win32_Process
52
- query := fmt .Sprintf ("SELECT ExecutablePath FROM Win32_Process WHERE ProcessId = %d" , pid )
53
- if err := wmi .Query (query , & processes ); err != nil {
54
- return fmt .Errorf ("WMI query failed: %w" , err )
55
- }
56
-
57
- if len (processes ) == 0 {
58
- return fmt .Errorf ("process with PID %d not found" , pid )
37
+ // On Windows, os.FindProcess does not actually find the process.
38
+ // So, we use this workaround to get the parent process name.
39
+ cmd := exec .Command ("tasklist" , "/FI" , fmt .Sprintf ("PID eq %d" , pid ), "/NH" , "/FO" , "CSV" )
40
+ output , err := cmd .Output ()
41
+ if err != nil {
42
+ return err
59
43
}
60
-
61
- // Get executable path
62
- path := processes [0 ].ExecutablePath
63
- if path == "" {
64
- return fmt .Errorf ("executable path not found for PID %d" , pid )
44
+ cmd = exec .Command ("wmic" , "process" , "where" , fmt .Sprintf ("ProcessId=%d" , pid ), "get" , "ExecutablePath" , "/format:list" )
45
+ output , err = cmd .Output ()
46
+ if err != nil {
47
+ return err
65
48
}
66
-
67
- // Launch new shell process with proper environment
68
- cmd := exec .Command (path )
69
- cmd .Env = os .Environ ()
70
- cmd .Stdin = os .Stdin
71
- cmd .Stdout = os .Stdout
72
- cmd .Stderr = os .Stderr
73
-
74
- if err := cmd .Run (); err != nil {
75
- return fmt .Errorf ("failed to launch shell: %w" , err )
49
+ path := strings .TrimPrefix (strings .TrimSpace (string (output )), "ExecutablePath=" )
50
+ command := exec .Command (path )
51
+ command .Env = os .Environ ()
52
+ command .Stdin = os .Stdin
53
+ command .Stdout = os .Stdout
54
+ command .Stderr = os .Stderr
55
+ if err := command .Run (); err != nil {
56
+ return fmt .Errorf ("open a new shell failed, err:%w" , err )
76
57
}
77
-
78
- return nil
79
- }
80
-
81
- func handleNoHookFallback (pid int ) error {
82
- // Fall back to global scope if no hooks
83
- fmt .Println ("Warning: The current shell lacks hook support. Switching to global scope." )
84
58
return nil
85
59
}
0 commit comments