Search code examples
windows-subsystem-for-linuxwindows-11

How to identify wslg.exe / wsl.exe from the Linux Distro?


I try to implement separate handling for wslg when "booting" my linux distro.

I have adapted my distro environment so that I can also start GUI apps from the wsl (PS) console, I find it difficult to intercept an environment variable or something like that that clearly indicates that the process started was wslg.exe.

Since I want to use wsl and wslg at the same time, I can't simply intercept the process (or pid).

The only idea I've got so far is to edit my generated shortcuts and add a "hint" (something like wslg.exe -fromWSLg -command) to identify wslg and pass it back to bash, which seems to a bad/incomplete approach.

Anyone have an idea or suggestion?

If I compare the env vars the only difference I see is PWD which is PWD=/root for wslg while wsl refers to the ps path...


Solution

  • The best solution I found is to grab the parent command line with PowerShell.

    That requires the ability to call powershell.exe from the wsl distro; I'm fine with this workaround:

    WSLGCHECK="$(powershell.exe -NoProfile -Command "\$parentProcess = Get-CimInstance Win32_Process -Filter \"ProcessId=\$PID\"; if (\$parentProcess) { \$parent = \$parentProcess.ParentProcessId; \$parentDetails = Get-CimInstance Win32_Process -Filter \"ProcessId=\$parent\"; if (\$parentDetails) { \$parentDetails.CommandLine } }")"
    if echo "$WSLGCHECK" | grep -qi "wslg.exe"; then
        echo "Found 'wslg.exe' in powershell parent process."
    fi
    

    Where the one-liner is like that:

    $parentProcess = Get-CimInstance Win32_Process -Filter \"ProcessId=$PID\"
    if ($parentProcess) { 
        $parent = $parentProcess.ParentProcessId
        $parentDetails = Get-CimInstance Win32_Process -Filter \"ProcessId=$parent\"
        if ($parentDetails) { 
            $parentDetails.CommandLine
        }
    }
    

    The output should be something like this:
    (path)wslg.exe(args) or (path)wsl.exe(args)