Solved

Execute a Shell command silently and get the Output

Posted on 2004-03-26
4
2,624 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
getting a fire fox page (view page source ) in c++ 6 48
best sources to up-to-date in C++? 8 115
C++ :Change value from  DisableCMD registry 4 80
max float value 3 75
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

752 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