Solved

keeping DOS window open

Posted on 2000-03-01
9
355 Views
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.

Thanks,
Gabe Smedresman
0
Comment
Question by:GabeSmed
9 Comments
 
LVL 3

Expert Comment

by:ufolk123
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.
0
 

Author Comment

by:GabeSmed
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.
0
 

Expert Comment

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

Author Comment

by:GabeSmed
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();
      if(!pDoc->GetPathName().IsEmpty())
      {
            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"); }
            else
            {
                  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(
                        NULL,
                        strExecuteProg,
                        
                        (LPSECURITY_ATTRIBUTES) NULL,
                        
                        (LPSECURITY_ATTRIBUTES) NULL,
                        true,
                        NULL,
                        (LPVOID) NULL,
                        (LPTSTR) NULL,
                        lpStartupInfo,
                        lpProcessInfo
                        ) )
                  {
                        MessageBox("Compile Failed!","Error");
                  }
                  pDoc->compiled = true;
            }
            
      }
      else
      {
            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!
0
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

by:HONG
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;
cin>>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


0
 

Author Comment

by:GabeSmed
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
0
 

Expert Comment

by:lexen
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.

--test.bat--
echo %1 %2 %3
pause
0
 
LVL 2

Accepted Solution

by:
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";
}
else
{
      // *** Windows 9x
      strShell = "command";
}

 ...

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

Hope this helps.
0
 

Author Comment

by:GabeSmed
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.
0

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