You can't, directly. Windows doesn't really care about parent-child process relationships. It does have a well hidden process property that tells you the parent process but unlike Unix it is just informational and cares no real value. So if you really, really need to know this then you'd have to reach into Win32 to fetch it. You can do that using NtQueryInformationProcess
as discussed here. But be aware that this is Windows only.
If you have control over the calling of the program then I would recommend you just use a command line argument. For example it is common for an app to have both a UI and CLI based interface. Ideally different exes should be used (which then call into the same shared set of libraries). This solves all the problems. But in some cases apps allow you to specify a command line argument (e.g. -noui
). The app looks for the argument and, if found, assumes a console app load otherwise it displays the UI.
Be aware that you can start an app from far more processes then just cmd
or explorer
. You should identify the "default" mode of your app and then special case the processes you care about. For example cmd
and powershell
and pwsh
.