From 0fdc69222f3bd314cfe8d03a7b01138764a0b73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=A0=EB=82=A8=ED=98=84?= Date: Sun, 15 Nov 2020 05:35:53 +0900 Subject: [PATCH] Update system.js, wmi.js --- lib/system.js | 20 ++++++----------- lib/wmi.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/lib/system.js b/lib/system.js index 8e07862..fe798ad 100644 --- a/lib/system.js +++ b/lib/system.js @@ -11,25 +11,19 @@ exports.require = global.require; exports.createProcess = function(cmd) { var SW_HIDE = 0; - var pid = 0; - - var wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2") - var si = wmi.Get("Win32_ProcessStartup").SpawnInstance_(); + var si = WMI.setClass("Win32_ProcessStartup").create(); si.ShowWindow = SW_HIDE; si.CreateFlags = 16777216; si.X = si.Y = si.XSize = si.ySize = 1; //wmi.Get("Win32_Process").Create(cmd, null, si, pid); - var w32proc = wmi.Get("Win32_Process"); + var process = WMI.setClass("Win32_Process").setMethod("Create").setParameters({ + "CommandLine": cmd, + "CurrentDirectory": null, + "ProcessStartupInformation": si + }).execute(); - var method = w32proc.Methods_.Item("Create"); - var inParams = method.InParameters.SpawnInstance_(); - inParams.CommandLine = cmd; - inParams.CurrentDirectory = null; - inParams.ProcessStartupInformation = si; - - var outParams = w32proc.ExecMethod_("Create", inParams); - return outParams.ProcessId; + return process.get("ProcessID"); }; exports.getEnvString = function(envName) { diff --git a/lib/wmi.js b/lib/wmi.js index b5f776a..0be04fa 100644 --- a/lib/wmi.js +++ b/lib/wmi.js @@ -2,7 +2,59 @@ // WMI(Windows Management Instrumentation) API //////////////////////////////////////////////////////////////////////// -var WMIObject = function() { +var WMIClassObject = function() { + this.interface = new WMIObject(); + this.class = null; + this.className = ""; + this.method = null; + this.methodName = ""; + this.inParams = {}; + this.outParams = {}; + + this.setClass = function(className) { + this.className = className; + this.class = this.interface.Get(className); + return this; + }; + this.create = function() { + var si = this.class.SpawnInstance_(); + return si; + }; + this.setMethod = function(methodName) { + this.methodName = methodName; + this.method = this.class.Methods_.Item(methodName); + return this; + }; + this.setParameter = function(key, value) { + this.inParams[key] = value; + return this; + }; + this.setParameters = function(params) { + if (typeof(params) !== "undefined") { + for (k in params) { + this.setParameter(k, params[k]); + } + } + return this; + }; + this.execute = function() { + var params = this.method.InParameters.SpawnInstance_(); + for (k in this.parameters) { + params[k] = this.inParams[k]; + } + this.outParams = this.class.ExecMethod_(this.methodName, params); + return this; + }; + this.get = function(key) { + if (key in this.outParams) { + return this.outParams[key]; + } else { + return ""; + } + }; +}; + +var WMIQueryObject = function() { var wbemFlagReturnImmediately = 0x10; var wbemFlagForwardOnly = 0x20; @@ -64,7 +116,11 @@ var WMIObject = function() { }; exports.execQuery = function(query) { - return (new WMIObject()).execQuery(query); + return (new WMIQueryObject()).execQuery(query); +}; + +exports.setClass = function(className) { + return (new WMIClassObject()).setClass(className); }; exports.VERSIONINFO = "WMI interface (wmi.js) version 0.1";