Solved

Execution dos command through VC++

Posted on 2004-04-03
8
1,511 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 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