_popen problems

This is a discussion on _popen problems within the C++ Programming forums, part of the General Programming Boards category; Hey everyone I have the following code that I want to use on my web server to pipe output from ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    _popen problems

    Hey everyone

    I have the following code that I want to use on my web server to pipe output from a CGI interpreter (such as PHP).

    Code:
    // Assume that 
    Extension = "php";
    Options.CGI[Extension] = "c:\\PHP\\php.exe";
    RealFile = "c:\\webroot\\sample.php";
    
    bool CONNECTION::SendCGI()
    {
    	char psBuffer[128];
    	FILE * hPipe;
    	string CGIInterpreter = "\"";								// Put quotes around the name of the interpreter	
    	CGIInterpreter += Options.CGI[Extension];					// Interpreter
    	CGIInterpreter += "\" ";									// End quote
    	CGIInterpreter += RealFile;									// Add the file to be interpreted
    
    	if ((hPipe = _popen(CGIInterpreter.c_str(), "rt")) == NULL)			
    	{															// Open a pipe
    		send (SFD, "Content-type: text/html\n\nCouldn't open pipe :(",48, 0);
    	}
    	else 
    	{
    	while (!feof( hPipe ))										// Loop until output finishes
    	{
    		if( fread( psBuffer,1, 128, hPipe) )						// Get part of the output
    		{	
    			send (SFD, psBuffer, 128, 0);						// Send it
    		}
    	}
    	fclose(hPipe);												// Close pipe	
    	}												return true;
    }
    Now, for some reason when this runs I am told there is an illegal operation, and the service (it runs as an NT service) has to shut down.

    No output is ever sent to the client. Am I doing something wrong with the pointers? I don't like to use FILE *'s but this was the only example I could find to use _popen().

    If anyone has any other easier ways I could create a pipe (win32) or if you can see what I've done wrong with my code, I'd love to hear from you.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    this line looks a little suspicious.
    Code:
      string CGIInterpreter = "\"";
    You can use CreatePipe() for making pipes on a win32 platform. However, popen is much easier to use.

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Yeah, I add the "\"" so that the string ends up:

    _popen ( "\"C:\\PHP\\php.exe\" c:\\webroot\\sample.php", "rt");

    I put quotes around the name of the interpreter because there might be spaces in it (and so it wouldn't work without them).

    Thanks for the CreatePipe tip, I'll go to MSDN and look it up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21