Solved

Execution dos command through VC++

Posted on 2004-04-03
8
1,506 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

705 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

20 Experts available now in Live!

Get 1:1 Help Now