keeping DOS window open

Posted on 2000-03-01
Last Modified: 2010-04-10
I want to run a .exe console application in an ms-dos prompt window and keep this window open after the program is finished so the user can look at the results.  I cannot edit the program to make it pause when it is done.  I have a CString with the command line, and parameters are generated by my Visual C++ 6 program.  If there is a way to get the output of these program to the dos window and bring it into my application that would be even better, but I doubt that is possible.

Gabe Smedresman
Question by:GabeSmed

Expert Comment

ID: 2575360
For stopping the programming to finish without warning you can use the getch() function at end of program defined in conio.h.

There is one way to get the output of your console app .You redirect the program 's stdout handle to your application's pipe etc.Look at CreateProcess() help in StartupInfo portion.If your console is interactive you can use

FILE *_popen( const char *command, const char *mode );

Please see MS help for more details.

Author Comment

ID: 2578292
I don't think I made myself very clear.  I have an .exe program (tc32.exe) that I downloaded.  I CANNOT edit it, I did not create it.  The program I'm working on is a Visual C++ SDI Windows Application.  I need to run tc32.exe with parameters generated by the program (in a CString) and have the program dos window stay open after the program is finished.

Expert Comment

ID: 2578453
Have you tried creating a shortcut to it?  Then you can uncheck the "Close on Exit" box.

Author Comment

ID: 2578864
Then I wouldn't be able to run the shortcut with parameters from my program.

For anyone who wants it, here is my code:

void CTadsView::OnCompile()
      // TODO: Add your command handler code here
      CTadsDoc *pDoc = GetDocument();
            CString m_strLaunchedDir,m_strFileName, commandline;
            m_strLaunchedDir = pDoc->GetLaunchedDir();
            m_strFileName = pDoc->GetPathName();
            if(pDoc->IsModified()) { MessageBox("You must save the file before compiling!","Error"); }
                  commandline = "\"" + m_strLaunchedDir + "Exec\\tc32.exe\" -i \"" + m_strLaunchedDir + "Libs\" -o ";
                  commandline += "\"" + m_strFileName.Left(m_strFileName.GetLength()-2) + ".gam\" -ds2 ";
                  commandline += "\"" + m_strFileName + "\"";
                  //if(WinExec(commandline,SW_SHOWNORMAL) < 33)
                  static STARTUPINFO           StartupInfo;
                  static LPSTARTUPINFO         lpStartupInfo = &StartupInfo;
                  static PROCESS_INFORMATION   ProcessInfo;
                  static LPPROCESS_INFORMATION lpProcessInfo = &ProcessInfo;
                  lpStartupInfo->cb          = sizeof( STARTUPINFO );
                  lpStartupInfo->lpDesktop   = NULL;
                  char strExecuteProg[1000];
                  strcpy( strExecuteProg, commandline );
                  lpStartupInfo->lpTitle = strExecuteProg;
                  //lpStartupInfo->dwX         = 0;
                  //lpStartupInfo->dwY         = 0;
                  //lpStartupInfo->dwXSize     = 0;
                  //lpStartupInfo->dwYSize     = 0;
                  lpStartupInfo->dwFlags     = (DWORD) NULL;
                  lpStartupInfo->wShowWindow = SW_SHOW ;
                  lpProcessInfo->hProcess = NULL;
                  if( !CreateProcess(
                        (LPSECURITY_ATTRIBUTES) NULL,
                        (LPSECURITY_ATTRIBUTES) NULL,
                        (LPVOID) NULL,
                        (LPTSTR) NULL,
                        ) )
                        MessageBox("Compile Failed!","Error");
                  pDoc->compiled = true;
            MessageBox("You must save the file before compiling!","Error");

I need to figure out how to keep the DOS window open when tc32 finishes (it displays results that are very important)  It is aggravating because I know people are having difficulties doing the exact opposite: getting their dos window to close automatically!
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.


Expert Comment

ID: 2585302
this should help
put the following line at the beginning of your .cpp file

#include <iostream>

in your int main() body
before return 0;
put the following lines:

char Stop;

because after all your codes are executed, the computer will ask for an input from the user, only when the user input a value for Stop variable, the program then will terminate and the DOS window will be closed, thus the user can take a look at the result as long as he does not yet input the last character.

this is an easier approach, there is the other one i know. but you know, try the easier one first.

hope it will help


Author Comment

ID: 2589515
no... I am not editing the program in the dos window - I can't, I just downloaded.  I need to run it from my SDI application, using CreateProcess, WinExec, or something else, and keep the dos window open when it is done

Expert Comment

ID: 2589618
What if you write a batch file that runs the program, taking in arguments, and has a pause for the last line.

Here is the simplest form - this will output three inputs that you give it.
Example: if I type in "test one two three", then the output is "one two three" and you must hit a key to exit the batch file.

echo %1 %2 %3

Accepted Solution

TallBoy earned 30 total points
ID: 2592498
Change your first commandline to

commandline = "command /k \"" + m_strLaunchedDir + "Exec\\tc32.exe\" -i \"" + m_strLaunchedDir + "Libs\" -o ";

If you want to be really slick, you can check to see what version of Windows you are running, and either run "command" or "cmd" (Windows 9x for the former, or WinNT for the latter).

// *** Define vars
DWORD dwVersion;
CSTring strShell;

// *** Get os version
dwVersion = GetVersion();
// *** Branch based on versions
if (dwVersion < 0x80000000)
      // *** Windows NT
      strShell = "cmd";
      // *** Windows 9x
      strShell = "command";


commandline = strShell + " /k \"" + m_strLaunchedDir + "Exec\\tc32.exe\" -i \"" + m_strLaunchedDir + "Libs\" -o ";

Hope this helps.

Author Comment

ID: 2593804
Well, I ended up using my own solution... But tallboy's answer was the most detailed so I'm giving him the points anyway.

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now