Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the command prompt know when to wait for exit?

I was attempting to do a Windows command prompt re-code in C#. I was wondering how the command prompt knows when to wait for the process started to exit, and when not to wait for the called process to exit.

For example, if you type in the command prompt "notepad", Notepad will launch, but you can still execute other commands. However, if you open a utility such as more.com, ping.exe, or another utility, it will wait for the executing program to finish before letting you execute another command.

How does the command prompt know when to wait for exit, and how can this behavior be emulated in C#?

like image 798
Justine Krejcha Avatar asked Mar 08 '14 19:03

Justine Krejcha


People also ask

How do you wait in Command Prompt?

You can use timeout command to wait for command prompt or batch script for the specified amount of time. The time is defined in Seconds. The above commands will break the timeout process on pressing any key. You can use /NOBREAK ignore key presses and wait for the specified time.

What does exit do in cmd?

Purpose: Exits a secondary command processor. This command can be used when you are running an application program and want to start the DOS command processor, then return to your program.

Why does cmd open and close immediately?

Use Windows 10 System File Checker (SFC) If the system file is damaged, it may also cause the problem that CMD opens and closes immediately in Windows 10. For this, you can try to fix this problem by running the Windows 10 System File Checker (SFC).

Which command is used to exit window?

If you want to close the active window, you can also click Close Current Window on the File menu or press CTRL+F4.


2 Answers

If the application is a Win32 GUI application, it will just run and command prompt won't wait for it to exit.

If the application is a console application, it will run in the command prompt and you'll need to wait for it to finish to get the command prompt back.

EDIT:

OK. It seems you need technical explanation. If you want to emulate the same feature in your application, you can check IMAGE_OPTIONAL_HEADER of EXE files here.

Inside IMAGE_OPTIONAL_HEADER, there is:

 WORD                 Subsystem; 

If SubSystem == 0x02 it means it's a GUI application.

If SubSystem == 0x03 it means it's a command prompt app.

EDIT 2:

If you want to see it in action:

  1. Download http://www.ntcore.com/exsuite.php

  2. Copy calc.exe or notepad.exe to your desktop

  3. Open copied calc.exe in CFF Explorer

  4. Navigate to Nt Headers -> Optional Headers

  5. Change SubSystem from 0x002 to 0x003

  6. Save it

Now run the new modified calc and you'll see the command prompt wait for it to be terminated.

like image 155
72DFBF5B A0DF5BE9 Avatar answered Oct 11 '22 11:10

72DFBF5B A0DF5BE9


The default from a command prompt is to spawn the GUI program in a separate process/fork. However, you can run notepad (or other GUI program) inline with your command prompt / shell script:

start /w notepad.exe 

This way, the command prompt / shell script only continues after the notepad.exe process has terminated.

Hope this helps, TW

like image 30
Tw Bert Avatar answered Oct 11 '22 11:10

Tw Bert