KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Getting the PID of 4D on Windows
PRODUCT: 4D | VERSION: 16 | PLATFORM: Win
Published On: June 21, 2018
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

Sometimes it may be usefule to obtain the PID of the 4D application. This can be accomplished fairly easily on Windows by utilizing PowerShell.

In PowerShell, the PID of the parent process can be obtained like this:

(gwmi win32_process | ? processid -eq $PID).parentprocessid
Note: in Powershell, the current process PID is stored in the $PID environment variable.

When calling PowerShell from 4D, 4D becomes the Parent Process. Therefore, the 4D process can be obtained using the following 4D code:
C_LONGINT($0)
C_BLOB($blob)
C_TEXT($cmd;$in;$out;$err;$script;$pid)
$script:="(gwmi win32_process | ? processid -eq $PID).parentprocessid"
$cmd:="powershell -NoLogo -Command "+$script
SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")
LAUNCH EXTERNAL PROCESS($cmd;$in;$out;$err)
$pid:=Replace string($out;Char(Line feed);"")
$pid:=Replace string($pid;Char(Carriage return);"")
$0:=num($pid)


If the code above was saved in a project method named get4Dpid it could be used like this:
ALERT(STRING(get4Dpid))