Hello,

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); 

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

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

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

      send("exit\r", 150);
      }
   
   system("pause");  
   
   _pclose(fp);
   }

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.

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 127.0.0.1 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.