Updated: 22/04/2017
/*
This identifier returns the process ID of mIRC
-Syntax-
$pid
-Examples-
//echo -a $pid
-Compatibility-
mIRC 6.17 or later.
-Comments-
Has two methods of PID detection.
The first method checks the PID for the existence of a single process in $mircexe
The second method checks the PID when there is more than one process in $mircexe
Tested on Windows 10 and XP
In Windows XP, in a Virtual Machine, the second method sometimes returned null, so I set the default to repeat at least once when it returns null
*/
alias pid {
if (!$isid) return
set -l %pid
set -l %id $ticks $+ $rand(1111,9999)
if ($com(pid_locator $+ %id)) .comclose pid_locator $+ %id
if ($com(pid_services $+ %id)) .comclose pid_services $+ %id
if ($com(pid_result $+ %id)) .comclose pid_result $+ %id
.comopen pid_locator $+ %id WbemScripting.SWbemLocator
if ($comerr) goto end
if (!$com(pid_locator $+ %id,ConnectServer,3,dispatch* pid_services $+ %id)) && ($comerr) goto end
if (!$com(pid_services $+ %id,ExecQuery,3,bstr,Select * from Win32_Process WHERE ExecutablePath = $qt($replace($mircexe,\,\\)),dispatch* pid_result $+ %id)) && ($comerr) goto end
if (!$com(pid_result $+ %id,Count,3)) goto end
if ($com(pid_result $+ %id).result == 1) %pid = $comval(pid_result $+ %id,1,ProcessId)
elseif ($v1 > 1) {
:a
set -l %cmd /c echo Get PID mIRC $uptime(mirc) $ticks $+ $rand(1111,9999) $+ >NUL && PING 10.255.10.255 -n 1 -w 5000>NUL
if ($com(pid_shell $+ %id)) .comclose pid_shell $+ %id
.comopen pid_shell $+ %id WScript.Shell
if ($comerr) goto end2
if (!$com(pid_shell $+ %id,Run,1,bstr,cmd.exe %cmd,int,0,bool,false)) goto end2
if ($com(pid_cmd $+ %id)) .comclose pid_cmd $+ %id
if (!$com(pid_services $+ %id,ExecQuery,3,bstr,Select * from Win32_Process WHERE Name = "cmd.exe",dispatch* pid_cmd $+ %id)) && ($comerr) goto end2
if (!$com(pid_cmd $+ %id,Count,3)) goto end2
set -l %x $com(pid_cmd $+ %id).result
while (%x) && ($+(*,%cmd) !iswmcs $comval(pid_cmd $+ %id,%x,CommandLine)) dec %x
if (%x) {
set -l %tmp $comval(pid_cmd $+ %id,%x,ParentProcessId)
if ($com(pid_mirc $+ %id)) .comclose pid_mirc $+ %id
if (!$com(pid_services $+ %id,ExecQuery,3,bstr,Select * from Win32_Process WHERE ProcessId = $qt(%tmp),dispatch* pid_mirc $+ %id)) && ($comerr) goto end2
if (!$com(pid_mirc $+ %id,Count,3)) || ($com(pid_mirc $+ %id).result == 0) goto end2
if (1 == $v1) && ($mircexe == $comval(pid_mirc $+ %id,1,ExecutablePath)) %pid = %tmp
}
:end2
if ($com(pid_shell $+ %id)) .comclose pid_shell $+ %id
if ($com(pid_cmd $+ %id)) .comclose pid_cmd $+ %id
if ($com(pid_mirc $+ %id)) .comclose pid_mirc $+ %id
if (!%pid) && (!%y) {
set -l %y 1
goto a
}
}
:end
if ($com(pid_locator $+ %id)) .comclose pid_locator $+ %id
if ($com(pid_services $+ %id)) .comclose pid_services $+ %id
if ($com(pid_result $+ %id)) .comclose pid_result $+ %id
return %pid
}