question

Matze-8645 avatar image
0 Votes"
Matze-8645 asked cooldadtx answered

.NET console application

Hello,
how can i detect if i start a net console application in cmd.exe or form explorer.exe?

dotnet-cli
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.