3

I try to create a simple UI which runs a command prompt in the background (but the windows console must not disappear) while clicking on each button, resp.

But before, I try something like system("start dir"); to see if the button works.

Here is the problem: when I click on the left button the windows console appear and don't exit unit I close it. But this only work with system("start dir");. If I change dir to ipconfig (or another call-function) the windows console will appear for a second and the exit. I tried something like system("PAUSE"); or getch(); etc, but it doesn't work.

Why does this command work with dir but not with another command?

Code UI

mantal
  • 1,093
  • 1
  • 20
  • 38
aGer
  • 466
  • 1
  • 4
  • 17

1 Answers1

2

There is a fundamental difference between DIR and IPCONFIG, the DIR command is built into the command processor (aka shell), IPCONFIG is a separate program stored in c:\windows\system32.

When you type START /? at the command line then you can see why it treats them differently:

If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.

If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.

The alternative is to ask the command processor to execute the command and exit afterwards. You do with the /c option:

  system("cmd.exe /c dir");

Or simpler yet, since system() automatically passes off the job to the command processor:

  system("dir");

Just stop using start :)

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you! Now I know the difference between DIR and IPCONFIG. But how can I fix my problem? – aGer Jul 01 '15 at 13:57
  • Erm, I gave specific snippets, the only possible thing you can do wrong is not trying them. – Hans Passant Jul 01 '15 at 13:59
  • Of course, I tried system("cmd.exe /c ipconfig"); and system("ipconfig"); but the console appear for a second and then disappear. – aGer Jul 01 '15 at 14:00
  • @RaymondChen I have read his answer more than one time. Anyway, your code worked for me. Thank you. :) – aGer Jul 01 '15 at 14:10
  • 1
    @aGer it was not clear from your question whether you wanted both to pause, or you wanted neither to pause. Hans and I thought you wanted neither to pause. But the way to get both to pause was also given in Hans's answer. – Raymond Chen Jul 01 '15 at 15:04