Solved

Execution dos command through VC++

Posted on 2004-04-03
8
1,513 Views
Last Modified: 2008-03-03
Hi,

I want to execute a dos based utility using my VC++ program and display the output generated by the program in the GUI. There are few ways to do this one is to use the system command but my utility take 5-10 secs. to complete and for this time the system command shows the dos window on top of everything.
The other way is to use the PIPE open the command output in the pipe and display the result but in that way as well the DOS window pops up.
Is there any way to achive this in which the utility will be executed in the background and i will get the output in a buffer.

Gourav Jain
0
Comment
Question by:gourav_jain
8 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10747001
Do u have the source code for the utility OR its a standard DOS utility

Amit
0
 
LVL 1

Author Comment

by:gourav_jain
ID: 10747027
I dont have the source code of that utility
Gourav Jain
0
 
LVL 17

Expert Comment

by:mokule
ID: 10747065
In what manner do You start  utilty?
In case

WinExec(...,SW_HIDE);

In case
CreateProcess();
in structure STARTUPINFO wShowWindow = SW_HIDE;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Accepted Solution

by:
ppk1981 earned 125 total points
ID: 10747083
Hi,

Try this .... I am not sure.....

Use "CreatePipe" to get handle ( read/write )to the PIPE . Redirect the "write handle" to standard Output. Then call "CreateProcess" to create a seperate process for your DOS utility. When Creating Process, Prevent the child from getting seperate console by setting flag( DETACHED_PROCESS ) and also the "STARTUPINFO" of the process contains the write handle ( PIPE ).

Look at this example.... You just reffer this.... ok


#include "stdafx.h"

#include <windows.h>
#include <stdio.h>

#define MY_BUF_SIZ 200
#define TRUE 1
#define FALSE 0

int main(int argc, char *argv[ ])
{
      char buffer[ MY_BUF_SIZ ];

      DWORD BytesRead;
      HANDLE hRead, hWrite;
      STARTUPINFO si;
      PROCESS_INFORMATION pi;
      SECURITY_ATTRIBUTES Sa ;
      BOOL bRetVal;

       ZeroMemory( &Sa, sizeof( Sa ) ) ;
      Sa.bInheritHandle = TRUE ;
      Sa.nLength = sizeof( Sa ) ;

      bRetVal = CreatePipe( &hRead, &hWrite, &Sa, 0 ) ;

      if (!bRetVal)
      {
         printf("Error creating pipe - exiting\n");
         exit(0);
      }

       ZeroMemory(&si, sizeof(si));

      si.cb = sizeof(si);
      si.dwFlags = STARTF_USESTDHANDLES;
      si.hStdOutput = hWrite;


      bRetVal = CreateProcess(
      NULL,
      "C:\\Praveen\\ChildCommunication\\Debug\\ChildCommunication",  
                 NULL,
      NULL,
      TRUE,                   // Child is inheriting pipe
      DETACHED_PROCESS, // prevent child getting console
      NULL,
      NULL,
      &si,
      &pi
              );

// We use DETACHED_PROCESS which means child will not have its
// own console - if this code were in a windows app, by default
// a console child would have a console window created for it
// even if stdio were re-directed.

      if (!bRetVal)
      {
            printf("Error launching child process - exiting\n");
            exit(0);
      }

      if( hWrite )
      {
            CloseHandle( hWrite );
            hWrite = 0 ;
      }

// Read from child until child closes its end of pipe

      do
      {
      if( WaitForSingleObject( pi.hProcess , 0 ) == WAIT_OBJECT_0 ) // wait until Child close
                  break ;
            
            bRetVal = ReadFile( hRead, &buffer, MY_BUF_SIZ, &BytesRead, NULL ) ;

            if( bRetVal && BytesRead )
            {
                  buffer[ BytesRead ] = 0; // null-terminate string
                  printf(buffer);
            }
      }
      while(BytesRead) ;
      
      CloseHandle(hRead) ;
      CloseHandle(pi.hProcess) ;
      CloseHandle(pi.hThread) ;

      return 0;
}

cheers,
Praveen
0
 
LVL 30

Expert Comment

by:Axter
ID: 10747421
A simple way is to use DOS redirect method.
Example:

system("myprog.exe > datafile.txt");

The above command will put the output into datafile.txt file.
0
 
LVL 30

Expert Comment

by:Axter
ID: 10747431
If you want to hide the process, you can use WinExec with the redirect command.

Example:
WinExec("myprog.exe > datafile.txt",SW_HIDE);
0
 
LVL 86

Expert Comment

by:jkr
ID: 10748647
>>WinExec("myprog.exe > datafile.txt",SW_HIDE);

*Cough*

WinExec("cmd.exe /c myprog.exe > datafile.txt",SW_HIDE);


BTW, a full sample on how to do that can be found at http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q190/3/51.ASP&NoWebContent=1 ("HOWTO: Spawn Console Processes with Redirected Standard Handles")
0
 
LVL 2

Expert Comment

by:ram_einstein
ID: 10822293
A small doubt: Where is WinExec() function defined?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

697 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