Solved

Execute a Shell command silently and get the Output

Posted on 2004-03-26
4
2,626 Views
Last Modified: 2007-12-19
Hello everyone,

I was looking for a way to execute a shell command (e.g. "ping localhost") without the user seeing a command shell(Win XP)
and recieve its output. Any ideas on that?

Thank you in advance,
Lord.
0
Comment
Question by:LordTTSeven
[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
  • 2
4 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 10691950
You can use _popen to open the command as a FILE *, and then use the standard fgets/fscanf/etc. functions to read the result, then use _pclose to close the stream.  Hope this helps.
0
 
LVL 23

Expert Comment

by:chensu
ID: 10692764
Call ShellExecute() with the SW_HIDE flag. Or call CreateProcess() with the DETACHED_PROCESS flag.
0
 

Author Comment

by:LordTTSeven
ID: 10694123
chensu:

I honestly dont get it how to recieve the Output here?

mnashadka:

This code:

#include <stdio.h>

void main(void) {
      FILE* cons;
      char s[999];
      cons = _popen("ping localhost","r");
      
      while(fscanf(cons,"%s",s))
            printf("%s",s);
      
      _pclose(cons);
      scanf("%s",s);
}

Provides me this(excuse my german Windows)

C:\User\VC>shellexec
Pinglord2[127.0.0.1]mit32BytesDaten:Antwortvon127.0.0.1:Bytes=32Zeit<1msTTL=128A
ntwortvon127.0.0.1:Bytes=32Zeit<1msTTL=128Antwortvon127.0.0.1:Bytes=32Zeit<1msTT
L=128Antwortvon127.0.0.1:Bytes=32Zeit<1msTTL=128Ping-Statistikfür127.0.0.1:Paket
e:Gesendet=4,Empfangen=4,Verloren=0(0%Verlust),Ca.ZeitangabeninMillisek.:Minimum
=0ms,Maximum=0ms,Mittelwert=0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0
ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms
0ms0ms0ms0ms0ms0ms0ms0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L 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 10695479
fscanf ignores whitespace, and i suggest that the end part is related to the buffer not being cleared out.  Change fscanf to fgets, and you'll get better results:

#include <stdio.h>

void main(void) {
     FILE* cons;
     char s[999];
     cons = _popen("ping localhost","r");
     
     while(fgets(s, 999, cons))
        printf("%s",s);
     
     _pclose(cons);
     scanf("%s",s);
}
0

Featured Post

Technology Partners: 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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

622 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