Skip to content

Instantly share code, notes, and snippets.

@u1f992
Last active April 22, 2023 05:34
Show Gist options
  • Save u1f992/b43760c85a4b29635285ba60a86ed0c5 to your computer and use it in GitHub Desktop.
Save u1f992/b43760c85a4b29635285ba60a86ed0c5 to your computer and use it in GitHub Desktop.
実行中のプロセスIDを取得するJScript
/**
* JSON polyfill
* https://www.bugbugnow.net/2018/05/wshjscriptjson.html
*/
var global = Function('return this')();
if (!global.JSON) {
global.JSON = {
parse: function (sJSON) { return eval('(' + sJSON + ')'); },
stringify: (function () {
var toString = Object.prototype.toString;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };
var escMap = { '"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t' };
var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); };
var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
return function stringify(value) {
if (value == null) {
return 'null';
} else if (typeof value === 'number') {
return isFinite(value) ? value.toString() : 'null';
} else if (typeof value === 'boolean') {
return value.toString();
} else if (typeof value === 'object') {
if (typeof value.toJSON === 'function') {
return stringify(value.toJSON());
} else if (isArray(value)) {
var res = '[';
for (var i = 0; i < value.length; i++)
res += (i ? ', ' : '') + stringify(value[i]);
return res + ']';
} else if (toString.call(value) === '[object Object]') {
var tmp = [];
for (var k in value) {
// in case "hasOwnProperty" has been shadowed
if (hasOwnProperty.call(value, k))
tmp.push(stringify(k) + ': ' + stringify(value[k]));
}
return '{' + tmp.join(', ') + '}';
}
}
return '"' + value.toString().replace(escRE, escFunc) + '"';
};
})()
};
}
Enumerator.prototype.toArray = function () { var r = []; var e = this; for (e.moveFirst(); !e.atEnd(); e.moveNext()) { var i = e.item(); r.push(i); } return r; }
var SWbemPropertySet = function (collection) {
var ret = {};
var array = new Enumerator(collection).toArray();
for (var i = 0; i < array.length; i++) {
var value = null
var property = array[i];
if (property.Value != null) {
if (property.IsArray) {
value = new VBArray(property.Value).toArray();
} else {
value = property.Value;
}
}
ret[property.Name] = value;
}
return ret;
};
var SWbemObjectSet = function (collection) {
var ret = [];
var array = new Enumerator(collection).toArray();
for (var i = 0; i < array.length; i++) {
ret.push(SWbemPropertySet(array[i].Properties_));
}
return ret;
};
function getChildProcessProperties(childProcess) {
var childProcessID = childProcess.ProcessID;
if (typeof childProcessID != 'number') {
throw new Error('The ProcessID of the child is not a number: ' + childProcessID);
}
var filtered = SWbemObjectSet(GetObject('winmgmts:root\\CIMV2').ExecQuery('SELECT * FROM Win32_Process WHERE ProcessID=' + childProcessID));
if (filtered.length != 1) {
throw new Error('The child process cannot be identified from the ProcessID: ' + filtered.length);
}
// WScript.Echo(JSON.stringify(filtered))
return filtered.pop();
}
function getProcessID() {
var catched = false;
var rawProcessID;
var childProcess = new ActiveXObject('WScript.Shell').Exec('mshta -');
try {
rawProcessID = getChildProcessProperties(childProcess).ParentProcessId;
} catch (e) {
catched = true;
} finally {
try {
childProcess.Terminate();
} catch (e) { }
}
if (catched) {
return getProcessID();
}
var processID = parseInt(rawProcessID);
if (isNaN(processID)) {
throw new Error('The ProcessID is not a number: ' + rawProcessID);
}
return processID;
}
pid = getProcessID();
WScript.Echo(pid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment