Fetch a Windows process tree fast
npm install @vscode/windows-process-treets
import * as child_process from 'child_process';
import { getProcessTree } from '@vscode/windows-process-tree';
if (process.platform === 'win32') {
child_process.spawn('cmd.exe');
getProcessTree(process.pid, (tree) => {
console.log(tree);
});
// { pid: 11168,
// name: 'node.exe',
// children:
// [ { pid: 1472, name: 'cmd.exe', children:[] },
getProcessTree(0, (tree) => {
console.log(tree);
});
// undefined
}
`
For the full API look at the typings file.
Why a native node module?
The current convention is to run wmic.exe to query a particular process ID and then parse the output like so:
`js
let cp = require('child_process');
function getChildProcessDetails(pid, cb) {
let args = ['process', 'where', parentProcessId=${pid}, 'get', 'ExecutablePath,ProcessId'];
cp.execFile('wmic.exe', args, (err, stdout, stderr) => {
if (err) {
throw new Error(err);
}
if (stderr.length > 0) {
cb([]);
}
var childProcessLines = stdout.split('\n').slice(1).filter(str => !/^\s*$/.test(str));
var childProcessDetails = childProcessLines.map(str => {
var s = str.split(' ');
return { executable: s[0], pid: Number(s[1]) };
});
cb(childProcessDetails);
});
}
``