Solved

Execute a Shell command silently and get the Output

Posted on 2004-03-26
4
2,617 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
Comment Utility
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
Comment Utility
Call ShellExecute() with the SW_HIDE flag. Or call CreateProcess() with the DETACHED_PROCESS flag.
0
 

Author Comment

by:LordTTSeven
Comment Utility
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
s0ms0ms0ms0ms0ms0ms0ms0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ms0ms0
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now