popen on Windows

This is a discussion on popen on Windows within the PHP Language forums, part of the PHP Programming Forums category; I'm trying to get popen to work on Windows. Here's a simplified example of what I'm trying ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-22-2007
Daniel Klein
 
Posts: n/a
Default popen on Windows

I'm trying to get popen to work on Windows.

Here's a simplified example of what I'm trying to get working:

I have a hw.c program as follows:

#include <stdio.h>
main()
{
printf ("Hello World!\n");
}

And here's the 'popentest.php' code:

popen test...
<?php
$fp = popen('C:\\home\\bin\\hw.exe', 'r');
$data = fread($fp, 1024);
pclose($fp);
echo $data;
?>

It works when I run this from a command prompt:

C:\Inetpub\wwwroot\php>php popentest.php
popen test...
Hello World!

All I get in a web browser is:

popen test...

What am I missing?

Thanks
Daniel Klein
Reply With Quote
  #2 (permalink)  
Old 12-23-2007
Janwillem Borleffs
 
Posts: n/a
Default Re: popen on Windows

Daniel Klein wrote:
> All I get in a web browser is:
>
> popen test...
>
> What am I missing?
>


It's possible that the program only executes from a shell. In this case you
should start popen with a command shell (cmd, r+w) and call your program
from there.


JW


Reply With Quote
  #3 (permalink)  
Old 12-23-2007
Vince Morgan
 
Posts: n/a
Default Re: popen on Windows

"Daniel Klein" <danielk@featherbrain.net> wrote in message
news:u89qm352ob3hep0heags7bd82q3seqcdjb@4ax.com...
> I'm trying to get popen to work on Windows.
>
> Here's a simplified example of what I'm trying to get working:
>
> I have a hw.c program as follows:
>
> #include <stdio.h>
> main()
> {
> printf ("Hello World!\n");
> }
>
> And here's the 'popentest.php' code:
>
> popen test...
> <?php
> $fp = popen('C:\\home\\bin\\hw.exe', 'r');
> $data = fread($fp, 1024);


According to the manual the file pointer that is returned by "popen()" can
be used by the following, fgets(), fgetss(), and fwrite() whereas you are
calling "fread()".

> It works when I run this from a command prompt:
>
> C:\Inetpub\wwwroot\php>php popentest.php
> popen test...
> Hello World!

That is because hw.exe is outputting to the consol when it runs I would
think
http://www.php.net/popen
HTH,
Vince



Reply With Quote
  #4 (permalink)  
Old 12-23-2007
Daniel Klein
 
Posts: n/a
Default Re: popen on Windows

On Sun, 23 Dec 2007 13:55:28 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:

>Daniel Klein wrote:
>> All I get in a web browser is:
>>
>> popen test...
>>
>> What am I missing?
>>

>
>It's possible that the program only executes from a shell. In this case you
>should start popen with a command shell (cmd, r+w) and call your program
>from there.


Thanks for the suggestion. I changed the code to:

$fp = popen('C:\\windows\\system32\\cmd.exe /c
C:\\home\\bin\\hw.exe', 'r+w');

but this made no difference; iow it works from a command prompt but
not in a web browser (I tried it with IE, Firefox and Opera).

I might add that I made sure that permissions on the executable were
wide open eg: full Full Control to 'Everyone'.

Does anyone out there have popen (or proc_open) working on
Windows/IIS?

If I cannot get this working then my next step will be to try it with
Apache; trouble is that I have not ever used it so it will certainly
be a learning experience for me. However, I would really like to get
it working with IIS, if at all possible.

Daniel Klein
Reply With Quote
  #5 (permalink)  
Old 12-23-2007
Daniel Klein
 
Posts: n/a
Default Re: popen on Windows

On Mon, 24 Dec 2007 00:06:32 +1000, "Vince Morgan"
<vinharAtHereoptusnet.com.au> wrote:

>According to the manual the file pointer that is returned by "popen()" can
>be used by the following, fgets(), fgetss(), and fwrite() whereas you are
>calling "fread()".
>
>> It works when I run this from a command prompt:
>>
>> C:\Inetpub\wwwroot\php>php popentest.php
>> popen test...
>> Hello World!

>That is because hw.exe is outputting to the consol when it runs I would
>think
>http://www.php.net/popen



Good catch. I changed the code to:

$fp = popen("cmd /c c:\\home\\cp\\hw.exe", "r+w");
$myline = fgets($fp);
pclose($fp);
echo $myline;

which envokes the program via 'cmd', but it still does not work in a
web browser :-(

I have already Google'd this to death and cannot find anything that
helps.

Daniel Klein
Reply With Quote
  #6 (permalink)  
Old 12-23-2007
Vince Morgan
 
Posts: n/a
Default Re: popen on Windows

"Daniel Klein" <danielk@featherbrain.net> wrote in message
news:u89qm352ob3hep0heags7bd82q3seqcdjb@4ax.com...
> I'm trying to get popen to work on Windows.
>
> Here's a simplified example of what I'm trying to get working:
>
> I have a hw.c program as follows:
>
> #include <stdio.h>
> main()
> {
> printf ("Hello World!\n");
> }
>
> And here's the 'popentest.php' code:
>

I could be wrong, but "printf()" is going to want to output to a consul, and
I can't see how you are going to capture the output via "popen()" which is
expecting a file pointer. You need to capture the output as a string so
that it can be output to the server as HTML or whatever. However you cannot
return a string from a C executable.
HTH
Vince


Reply With Quote
  #7 (permalink)  
Old 12-23-2007
Daniel Klein
 
Posts: n/a
Default Re: popen on Windows

On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
<vinharAtHereoptusnet.com.au> wrote:

>"Daniel Klein" <danielk@featherbrain.net> wrote in message
>news:u89qm352ob3hep0heags7bd82q3seqcdjb@4ax.com.. .
>> I'm trying to get popen to work on Windows.
>>
>> Here's a simplified example of what I'm trying to get working:
>>
>> I have a hw.c program as follows:
>>
>> #include <stdio.h>
>> main()
>> {
>> printf ("Hello World!\n");
>> }
>>
>> And here's the 'popentest.php' code:
>>

>I could be wrong, but "printf()" is going to want to output to a consul, and
>I can't see how you are going to capture the output via "popen()" which is
>expecting a file pointer. You need to capture the output as a string so
>that it can be output to the server as HTML or whatever. However you cannot
>return a string from a C executable.


'printf()' is sending the output, as a string, to STDOUT so I would
think 'popen()' / 'fgets()' should be able to handle that.

Can anyone else substantiate this?

Daniel Klein
Reply With Quote
  #8 (permalink)  
Old 12-23-2007
Daniel Klein
 
Posts: n/a
Default Re: popen on Windows

On Sun, 23 Dec 2007 14:50:59 GMT, Daniel Klein
<danielk@featherbrain.net> wrote:

>On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
><vinharAtHereoptusnet.com.au> wrote:
>
>>"Daniel Klein" <danielk@featherbrain.net> wrote in message
>>news:u89qm352ob3hep0heags7bd82q3seqcdjb@4ax.com. ..
>>> I'm trying to get popen to work on Windows.
>>>
>>> Here's a simplified example of what I'm trying to get working:
>>>
>>> I have a hw.c program as follows:
>>>
>>> #include <stdio.h>
>>> main()
>>> {
>>> printf ("Hello World!\n");
>>> }
>>>
>>> And here's the 'popentest.php' code:
>>>

>>I could be wrong, but "printf()" is going to want to output to a consul, and
>>I can't see how you are going to capture the output via "popen()" which is
>>expecting a file pointer. You need to capture the output as a string so
>>that it can be output to the server as HTML or whatever. However you cannot
>>return a string from a C executable.

>
>'printf()' is sending the output, as a string, to STDOUT so I would
>think 'popen()' / 'fgets()' should be able to handle that.


Just to be thorough, I created a php script:

<?php
echo "Hi";
?>

and replaced the 'hw.exe' with 'php hi.php' - I got the same problem
as originally reported.

I also tested 'php pipetest.php' from a telnet session, which (I
think) proves that it does not require a 'console'.

I'm getting to the point where I'm wondering if this is a Windows
limitation, but I do not have a unix-like box to test out this theory.

Daniel Klein
Reply With Quote
  #9 (permalink)  
Old 12-25-2007
Tim Roberts
 
Posts: n/a
Default Re: popen on Windows

"Vince Morgan" <vinharAtHereoptusnet.com.au> wrote:

>"Daniel Klein" <danielk@featherbrain.net> wrote:
>
>> I'm trying to get popen to work on Windows.
>>
>> Here's a simplified example of what I'm trying to get working:
>>
>> I have a hw.c program as follows:
>>
>> #include <stdio.h>
>> main()
>> {
>> printf ("Hello World!\n");
>> }
>>
>> And here's the 'popentest.php' code:
>>

>I could be wrong, but "printf()" is going to want to output to a consul, and
>I can't see how you are going to capture the output via "popen()" which is
>expecting a file pointer.


You are wrong. "printf" writes to stdout. If you run it from a console,
then stdout is connected to the console, but that's certainly not the only
option. If you type:
hw > xxx.txt
then clearly "printf" is not going to the console.

popen runs the program with stdin and stdout redirected to the calling
program. The program as he describes it should work, assuming the PHP
popen is correctly implemented on Windows.

Your suggestion about fread is also off the mark. Any of the stdio
functions will work.

>You need to capture the output as a string so
>that it can be output to the server as HTML or whatever. However you cannot
>return a string from a C executable.


Not so. Perhaps you should hold off on replying until you are in more
familiar territory.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Reply With Quote
  #10 (permalink)  
Old 12-25-2007
Vince Morgan
 
Posts: n/a
Default Re: popen on Windows

"Tim Roberts" <timr@probo.com> wrote in message
news:ag71n3hb9lsf385c5pdf0k4os0s9pcjvfn@4ax.com...
> You are wrong. "printf" writes to stdout. If you run it from a console,
> then stdout is connected to the console, but that's certainly not the only
> option. If you type:
> hw > xxx.txt
> then clearly "printf" is not going to the console.


Clearly, thank you.

> Your suggestion about fread is also off the mark. Any of the stdio
> functions will work.


>>> "Vince Morgan" <vinharAtHereoptusnet.com.au> wrote:

[snip]
>>>According to the manual the file pointer that is returned by "popen()"

can
>>>be used by the following, fgets(), fgetss(), and fwrite() whereas you are
>>>calling "fread()".

[snip]

What suggestion was that? I can't see it in the above. You have another
source?

>
> >You need to capture the output as a string so
> >that it can be output to the server as HTML or whatever. However you

cannot
> >return a string from a C executable.

Ooops, pipes.
>
> Not so. Perhaps you should hold off on replying until you are in more
> familiar territory.


My appologies to Daniel.

I see your ego is much larger than you manners..
Regards,
Vince Morgan


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:04 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0