Solved

Execute a Shell command silently and get the Output

Posted on 2004-03-26
4
2,622 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
  • 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
0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0m
s0ms0ms0ms0ms0ms0ms0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ms0ms0ms0
ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms
0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0m
s0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0
ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms
0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0m
s0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0
ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms
0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0m
s0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0
ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms
0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms^C

It somehow does neither get CRLFs nor Spaces, and the last word(?) is repeatet untill i press CTRL+C...

Thank you, Lord.
0
 
LVL 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

809 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