Solved How to get is interactive flag of CMD

June 12, 2012 at 07:22:21
Specs: Windows XP, not important
How can a process that is running in CMD know if it is running interactively?

The process in question wants to know if it is running as a part of some Login batch sequence (so it wants to simply do its work without any interruption) or if it was called by a user inside an interactive CMD session (so it knows it can ask dialog questions and get answers by that user).

I have already checked the capabilities of GetConsoleScreenBufferInfo, but this can only give me a hint if a given standard handle is not attached to the console (e.g. redirected).


See More: How to get is interactive flag of CMD

Report •


#1
June 12, 2012 at 13:34:11
I don't know if you can, but you might be looking in the wrong place. Console windows are a service provided by csrss, and batch files are the domain of cmd, which just happens to run in a console window.

How To Ask Questions The Smart Way


Report •

#2
June 13, 2012 at 08:01:29
Thanks for your reply, I have understood that a Console window and CMD.exe are completely different things.

I have only interfered to the Console thing while trying to get window dimensions, and by fortune it gave me a chance to get the information if my standard output/error handles are redirected (in this case the GetConsoleScreenBufferInfo call would fail). This helped me to compensate for the lack of the 'isatty' attribute of the standard handles.

So the question is if CMD.exe has such a 'interactive' or 'batch' flag or something similar, and how can a child process ask for this information?


Report •

#3
June 13, 2012 at 10:56:18
✔ Best Answer
There really isn't one that I've managed to find, and the general recommendation is to use a command line switch control your app's batch mode. The hard way would be to use Process32First/Next to find the parent PID, then reference that PPID while rooting around in the Windows kernel. If your parent has an open handle to a .BAT/.CMD/.VBS/.VBE/.JS/.JSE/.WSF/.WSH/.PS1, and your parent isn't Explorer, chances are good you're running as part of a script.


You seem to come from a POSIX background, so I'll link to the following blog entry: Biculturalism. The point of the post is that Windows programs are for end-users, while Linux programs are for programs. Knowing the differences, and the ramifications thereof, is going to be important if you want to be successful.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
June 14, 2012 at 02:28:04
Thanks for your reply...

(1) Indeed, I have already implemented a --batch switch, but wouldn't it be nice if a program simply would 'Do The Right Thing (TM)' ?
(2) Yes, I'll crawl the process list to find who is my parent -> I'll keep you informed if I succeed this way.
(3) Thanks for your sympathy with me POSIX-backgrounded guy, fooled by POSIX look of CMD, but woken up and offended by the lack of POSIX behaviour of CMD.

Thanks again for your help, good to know we're not alone.


Report •

#5
June 15, 2012 at 04:52:31
The correct way is probably to just show your dialogs, and have a 60 second timeout, or something.

As for CMD, it loosely emulates and expands on the behavior of MS-DOS' COMMAND.COM. MS-DOS itself was originally designed to emulate CP/M, so it's best to think of CMD as a completely separate command line environment emulating a CLI developed around the same time frame as UNIX for a completely different audience, and this emulation is grafted onto what is otherwise a completely graphical OS. Because it is.

How To Ask Questions The Smart Way


Report •


Ask Question