.NET console application

Matze 36 Reputation points
2022-11-18T16:08:28.97+00:00

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

.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
320 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 47,626 Reputation points
    2022-11-18T16:41:37.357+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful