Solved

Execution dos command through VC++

Posted on 2004-04-03
8
1,517 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

623 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