Solved

Execution dos command through VC++

Posted on 2004-04-03
8
1,512 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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