Post by Helge KrusePost by Helge KruseHello,
Post by Helge KruseI need to get the proceess identifier of the process that executes my
JScript.
This information is provided by the Win32 API function
GetCurrentProcessId.
But how can I call this function from my .js program? I know, that I could
write a in-process COM server that performs the actual call, but this looks
like overkill for calling one function.
function getPID(){
var objWMIService = GetObject("winmgmts:\\\\.\\root\\cimv2"),
sql = "SELECT * FROM Win32_Process " +
"WHERE (Name='cscript.exe' OR Name='wscript.exe') " +
" AND CommandLine LIKE '%" + WScript.ScriptName +
"%'",
e = new Enumerator(objWMIService.ExecQuery(sql,"WQL",48)
), pid;
for (;!e.atEnd();e.moveNext()){
var objItem = e.item();
return objItem.ProcessId
}
return void 0;
}
You near to it. The goal is to identify, if the script is hosted by wscript
or cscript. Unfortunately this doesn't work, since the query does not
distinguish between two instances of the same script.
Further I need to execute this in a WSC component implemented with JScript.
There is no WScript available in this context.
I noticed your post here and it was a bit more enlightening:
https://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.scripting.vbscript&tid=60fa2d0d-20fb-4cfd-8464-de80dc63b615&cat=&lang=&cr=&sloc=&p=1
The problem is that there is no way to directly discover the caller of
a component if a reference is not passed, so anything that could be
come up with will probably be little more than a hack I think.
Here is an alternative hack I've came up with:
http://pastebin.com/k8Vy1GdH
The general idea I used is to query all the possibly relevant
processes at the time of component creation and then to filter out all
but the latest one.
The sorting is probably bugged since I was lazy and wanted to convey
the general idea. The non-lazy idea would be to use a regular
expression on the CreationDates then feed those into 2 date objects
for comparison.
Hopefully this can get you on the right path.