Link to home
Start Free TrialLog in
Avatar of Chris_m
Chris_m

asked on

Running cmd.exe from a C program

I would like to launch the cmd.exe program from within a C program and then run ping commands, is that possible and how can I best achieve it.
Thanks and regards
Avatar of sunnycoder
sunnycoder
Flag of India image

1. fork and exec
2. system()
3. popen
1. fork and exec ...

you can spawn a new process from your current process using fork ... In this new process you can call a member of exec family which will load the cmd.exe and then execute it

2. system("<command>");
    will execute <command> as if it were typed on your prompt

3. popen works similar to system() but here you get an additional pipe to either read or write data ... i.e. either you can read data from the program that you are launching, or you can send data to both

If you wish to have two way communication, then you need to fork and establish two pipes
typo
>>or you can send data to both
should be
or you can send data but not both
coming to your spefic usage

you can use some thing like

system ( " ping hostname > results.txt");
and then read the results from the file results.txt

OR

FILE * fp;
fp = popen ( "ping hostname", "r" );
while ( fgets (...) != NULL )
{
     /* you are receiving ping results here */
}

you can consider fork and exec method too but that is a bit more complex and will require some study on your part
Avatar of Chris_m
Chris_m

ASKER

The cmd is launched, but I don't seem to be able to run any command in it; I tried:
system("cmd.exe");
      char* szPing = "ping yahoo.com > c:\\ping.txt";
      system(szPing);
      szPing = "ping www.microsoft.com > c:\\ping2.txt";
      system(szPing);

but the pings do not execute, I must be doing something wrong.
Regards
try system ( " ping www.microsoft.com > results.txt");
Avatar of Chris_m

ASKER

That also doesn't work; the cmd window opens OK, but that's all.
Regards
check contents of the file results.txt
Avatar of Chris_m

ASKER

There is no such file being created.
try searching for it... It should have been created with the results of the ping
Avatar of Chris_m

ASKER

I have searched for it.  It is not there (it works if I don't launch the CMD program and run the run the test program from a cmd prompt manually).
Regards

Have you tried

system("c:/winnt/system32/ping.exe www.microsoft.com");

or

system("c:\\winnt\\system32\\ping.exe www.microsoft.com");

where

"c:/winnt/system32/ping.exe"

is the full path to the ping executable on your system?

Thomas



ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


you can ping from your program...

https://www.experts-exchange.com/questions/20721517/source-code-for-the-ping-utility.html

if you open a command prompt to ping and redirect the output to a file, you have to parse through it and that doesn't seem like the best practice


follow the links and download the souce code


hey...oops...that is your question from before...sorry about that
Avatar of Chris_m

ASKER

That's it sunnycoder.  Is there anyway I can stop the window from closing after it has completed its task?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey i was trying a simliar thing but output re-direction is not happ.  via system?
 my code is like :

int main( void ) {
      printf("Hello World.\n");
      //system("ls > c://test1.txt");
      system(" c:\h1.exe > c:\wierd");
      system("pause");
      system("cls");
      printf("after");
      return 0;
}
even that pause statement is not happ.
why is this so?
the file test1.txt or wierd is not getting created
try
system(" c:\\h1.exe > c:\\wierd");

make sure that h1.exe exists
Nope sunny.. its not happ.
h1 does exist . tried out even with
system("ls >c:\\wierd")
BTW iam trying dis out in Windows. FYI
ls is not a windows command, so it will not run
how about this
system(" c:\\h1 > c:\\wierd");

on the command prompt
does h1.exe > C:\test.txt produce or h1 > C:\test.txt correct ouput

not_an_xpert? Do you have an "ls" command at all?
or did you mean system("dir > c:\\wierd.txt"); ?

Thomas
there is ls in my system.. also dir... norse
i  have tried what u told me sunny
in the cmd  prompt
both h1.exe >wierd
and h1 >wierd produce an output
on  doing the same with system . its not happening
that's really wierd... try posting a different question
why a diff. question sunnyCoder. I hope the problem is understood..
iam trying out output re-direction using system in windows and its not happening
whereas the same output-redirection is happ. in windows via d command prompt.

not_an_xpert :(

cuz

1. that is all I have to offer and other experts on the forum have no knowledge of your problem ... If you post a new question, then atleast they can view it and comment
2. Ethically too you should be posting a new question

btw what is not working for you just worked for chris_m ... So I am really suspicious of your code

copy paste it here and I'll try to locate the problem

my guess is that
1. It is embedded in an if else statement and hence is not getting executed at all
2. fork is failing for some strange reason ... what is the value returned by system()

Well, not_an_xpert. Let's try it the other way round. Create
a new console project with VS 6.0, name it "systemredirect", and choose
the option that it supports MFC, then replace the "main" code
by

#---------------------------

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
      int nRetCode = 0;

      // initialize MFC and print and error on failure
      if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      {
            // TODO: change error code to suit your needs
            cerr << _T("Fatal Error: MFC initialization failed") << endl;
            nRetCode = 1;
      }
      else
      {
            system("DIR");
            system("DIR > C:\\TEMP\\OUT.TXT");;
      }

      return nRetCode;
}

#-----------------------------------------------

Then let it run. Q1: Do you get the on screen output ? Q2: Do you get
the file c:\temp\out.txt ?

Thomas