Does anyone know how to use _popen in windows xp sp3 (console application) to allow writing to a process?

I've tried using _popen to read from a process. This works as expected.

When I try _popen with write access, the process starts, but no commands are interpreted.

My code is:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string>
#include <sstream>

using namespace std;

#define send(msg, dly) fprintf(fp, msg); fflush(fp); Sleep(dly); fflush(fp); 

   // we're going to be sending commands directly to the command processor
   FILE *fp = _popen("cmd.exe", "w+"); 
      // log onto programming DA-660
      send("telnet\r", 150);
      send("root\rroot\r", 150);
      // log on to the DA-660 to be programmed, enable the ramdisk
      send("telnet", 150);
      send("root\rroot\r", 150);
      send("upramdisk\r", 150);
      send("exit\r", 150);

      // transfer the installation package
      send("scp /sscinstall.tgz root@\r", 500);
      send("root\r", 1500);

      // ### a whole heap more commands ###

      send("exit\r", 150);

I know the above looks like it would be ideal for a script, but I couldn't get the password to pipe into the scp (or ftp either if I remember correctly); that is why I'm trying the popen method.

8 Years
Discussion Span
Last Post by dougy83

Thanks. That worked (I can send commands to the command prompt).

The problem is now that Telnet won't accept commands. I also tried opening telnet directly with popen; no luck.
Any ideas?


Yes it works when I run it from the command line.

I tried using a local tcp server on the ftp port (23); from the command line I can log on and maintain a connection, send/receive/etc., but when I do it through popen(), telnet opens up, connects, then disconnects really quickly without reporting the lost connection.

Telnet doesn't seem to appreciate anyone playing with it's pipes - e.g. echo testing | telnet also makes telnet have a spaz (and no data is sent).

I might just use a dirty keystroke sender to an open command prompt.. yuk. or work out how the telnet [do]/[don't] [will]/[won't] works, and get away from the telnet program altogether.

Anyway, thanks for the help guys.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.