keeping DOS window open

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
Who is Participating?
TallBoyConnect With a Mentor Commented:
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.
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.
GabeSmedAuthor Commented:
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.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Have you tried creating a shortcut to it?  Then you can uncheck the "Close on Exit" box.
GabeSmedAuthor Commented:
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!
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

GabeSmedAuthor Commented:
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
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
GabeSmedAuthor Commented:
Well, I ended up using my own solution... But tallboy's answer was the most detailed so I'm giving him the points anyway.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.