Avatar of LordTTSeven
LordTTSeven

asked on 

Execute a Shell command silently and get the Output

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.
C++

Avatar of undefined
Last Comment
mnashadka
Avatar of mnashadka
mnashadka

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.
Avatar of chensu
chensu
Flag of Canada image

Call ShellExecute() with the SW_HIDE flag. Or call CreateProcess() with the DETACHED_PROCESS flag.
Avatar of LordTTSeven
LordTTSeven

ASKER

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
s0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0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ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms0ms
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.
ASKER CERTIFIED SOLUTION
Avatar of mnashadka
mnashadka

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo