Skip to content

Commit f822f0f

Browse files
authored
hotfix: wmic deprecated (#402)
WMIC is deprecated as of Windows 10, version 21H1; and as of the 21H1 semi-annual channel release of Windows Server. This utility is superseded by Windows PowerShell for WMI; see Chapter 7 - Working with WMI. This deprecation applies only to the WMIC utility. Windows Management Instrumentation (WMI) itself is not affected. Also see Windows 10 features we're no longer developing. https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmic
1 parent e1f3f56 commit f822f0f

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ require (
2121

2222
require (
2323
atomicgo.dev/schedule v0.1.0 // indirect
24+
github.com/StackExchange/wmi v1.2.1 // indirect
2425
github.com/andybalholm/brotli v1.1.0 // indirect
2526
github.com/andybalholm/cascadia v1.3.2 // indirect
2627
github.com/bodgit/plumbing v1.3.0 // indirect
2728
github.com/bodgit/windows v1.0.1 // indirect
2829
github.com/containerd/console v1.0.4 // indirect
2930
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
31+
github.com/go-ole/go-ole v1.2.5 // indirect
3032
github.com/gookit/color v1.5.4 // indirect
3133
github.com/hashicorp/errwrap v1.0.0 // indirect
3234
github.com/hashicorp/go-multierror v1.1.1 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ github.com/MarvinJWendt/testza v0.5.2 h1:53KDo64C1z/h/d/stCYCPY69bt/OSwjq5KpFNwi
3636
github.com/MarvinJWendt/testza v0.5.2/go.mod h1:xu53QFE5sCdjtMCKk8YMQ2MnymimEctc4n3EjyIYvEY=
3737
github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
3838
github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
39+
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
40+
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
3941
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
4042
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
4143
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
@@ -64,6 +66,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
6466
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
6567
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6668
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
69+
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
70+
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
6771
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
6872
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
6973
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -263,6 +267,7 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7w
263267
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
264268
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
265269
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
270+
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
266271
golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
267272
golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
268273
golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

internal/shell/windows_process.go

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ import (
2222
"fmt"
2323
"os"
2424
"os/exec"
25-
"strings"
25+
26+
"github.com/StackExchange/wmi"
27+
"github.com/version-fox/vfox/internal/env"
2628
)
2729

30+
type Win32_Process struct {
31+
ExecutablePath string
32+
CommandLine string
33+
ProcessId uint32
34+
}
35+
2836
type windowsProcess struct{}
2937

3038
var process = windowsProcess{}
@@ -34,26 +42,44 @@ func GetProcess() Process {
3442
}
3543

3644
func (w windowsProcess) Open(pid int) error {
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
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)
4359
}
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
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)
4865
}
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)
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)
5776
}
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.")
5884
return nil
5985
}

0 commit comments

Comments
 (0)