473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Dec 22 '07 #1
15 8636
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
Dec 23 '07 #2
"Daniel Klein" <da*****@featherbrain.netwrote in message
news:u8********************************@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

Dec 23 '07 #3
On Sun, 23 Dec 2007 13:55:28 +0100, "Janwillem Borleffs"
<jw@jwscripts.comwrote:
>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
Dec 23 '07 #4
"Daniel Klein" <da*****@featherbrain.netwrote in message
news:u8********************************@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
Dec 23 '07 #5
On Mon, 24 Dec 2007 00:06:32 +1000, "Vince Morgan"
<vinharAtHereoptusnet.com.auwrote:
>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
Dec 23 '07 #6
On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
<vinharAtHereoptusnet.com.auwrote:
>"Daniel Klein" <da*****@featherbrain.netwrote in message
news:u8********************************@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
Dec 23 '07 #7
On Sun, 23 Dec 2007 14:50:59 GMT, Daniel Klein
<da*****@featherbrain.netwrote:
>On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
<vinharAtHereoptusnet.com.auwrote:
>>"Daniel Klein" <da*****@featherbrain.netwrote in message
news:u8********************************@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
Dec 23 '07 #8
"Vince Morgan" <vinharAtHereoptusnet.com.auwrote:
>"Daniel Klein" <da*****@featherbrain.netwrote:
>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, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 25 '07 #9
"Tim Roberts" <ti**@probo.comwrote in message
news:ag********************************@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.auwrote:
[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
Dec 25 '07 #10
On Dec 23, 11:42 am, Daniel Klein <dani...@featherbrain.netwrote:
On Sun, 23 Dec 2007 14:50:59 GMT, Daniel Klein

<dani...@featherbrain.netwrote:
On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
<vinharAtHereoptusnet.com.auwrote:
>"Daniel Klein" <dani...@featherbrain.netwrote in message
news:u8********************************@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");
}

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.
There are usually two issues with this type of code: (1) Is anything
getting executed at all? (2) Collecting the output. To verify that
(1) is working, you could use a file_put_contents(...) together with
date(...)

Also, popen has undergone changes in the last year so if you are
working with an older version of php (perhaps 5.1 or earlier?) things
may be slightly different.

Here is an example of collecting output from popen that works in a web
server context for me:

<?php
function getPOpenOutput ($cmd) {
$out = "";
$proc = popen ($cmd, "r");
while (!feof($proc)) { // wait for output to finish
$slurp = fgets($proc, 256);
if (strlen($slurp)>0) $out .= $slurp;
else com_message_pump(200); }
pclose ($proc);
return $out; }

$cmnd = "SchTasks";
$out = getPOpenOutput ($cmnd);
print "<pre>$out</pre>";
?>

You may have to set the web server to
"Allow service to interact with the Desktop",
which is found under Control Panel \ Services

Finally, if you are calling on more complicated
programs, you may need to escape characters
like double quotes, <, >, ^, et. al) within
$cmnd, which can be non trivial. For example:
$cmnd = "php -r echo('hi');";
but
$cmnd = "php -r \"echo('hi there');\"";

Csaba Gabor from Vancouver
Dec 25 '07 #11
On Tue, 25 Dec 2007 12:22:33 -0800 (PST), Csaba Gabor
<da*****@gmail.comwrote:
>On Dec 23, 11:42 am, Daniel Klein <dani...@featherbrain.netwrote:
>On Sun, 23 Dec 2007 14:50:59 GMT, Daniel Klein

<dani...@featherbrain.netwrote:
>On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
<vinharAtHereoptusnet.com.auwrote:
>>"Daniel Klein" <dani...@featherbrain.netwrote in message
news:u8********************************@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");
}

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.

There are usually two issues with this type of code: (1) Is anything
getting executed at all? (2) Collecting the output. To verify that
(1) is working, you could use a file_put_contents(...) together with
date(...)
I turned on error reporting and it appears that the popen is not
getting executed at all. I replaced the executable with one that
writes out a small file and it did not even get called. To be sure, I
ran the php code from CLI and sure enuf it ran fine.
>Also, popen has undergone changes in the last year so if you are
working with an older version of php (perhaps 5.1 or earlier?) things
may be slightly different.
I'm running PHP 5.2.5.
>Here is an example of collecting output from popen that works in a web
server context for me:

<?php
function getPOpenOutput ($cmd) {
$out = "";
$proc = popen ($cmd, "r");
while (!feof($proc)) { // wait for output to finish
$slurp = fgets($proc, 256);
if (strlen($slurp)>0) $out .= $slurp;
else com_message_pump(200); }
pclose ($proc);
return $out; }

$cmnd = "SchTasks";
$out = getPOpenOutput ($cmnd);
print "<pre>$out</pre>";
?>
Thanks for this code.
>You may have to set the web server to
"Allow service to interact with the Desktop",
which is found under Control Panel \ Services
Yes, I had already done this (should have mentioned it in my post so
as to not have to cover 'old' ground, sorry).
>Finally, if you are calling on more complicated
programs, you may need to escape characters
like double quotes, <, >, ^, et. al) within
$cmnd, which can be non trivial. For example:
$cmnd = "php -r echo('hi');";
but
$cmnd = "php -r \"echo('hi there');\"";
Good point!

***

Btw, I have since installed Apache 2.2 and this has resolved the issue
(nice way to spend the holiday, don't you think ;-). Again, I had to
make sure that the service was able to interact with the desktop and
that the process has the appropriate executable permissions.

In conclusion, the ability to use 'popen' or 'proc_open' or 'back
tics' on Windows appears to be a Windows/IIS limitation /
security-feature(?) / bug(?) / whatever...

Hope this conversation will be of use to others attempting the
'impossible' on Windows.

Daniel Klein
Cuyahoga Falls, OH
Dec 26 '07 #12
Vince Morgan wrote:
"Daniel Klein" <da*****@featherbrain.netwrote in message
news:u8********************************@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()".
Not a problem. fread() can use the handle returned by popen() also.
See further down where there is an example of that.
>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
Not knowing what hw.exe is doing, it's hard to say.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 28 '07 #13
Vince Morgan wrote:
"Tim Roberts" <ti**@probo.comwrote in message
news:ag********************************@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.auwrote:
[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
Sorry, Vince. I agree with Tim. You're grasping at straws here.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 28 '07 #14
Daniel Klein 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:

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
Daniel,

On question - does the web server user have permission to execute the
file? Have you verified this?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Dec 28 '07 #15
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:i8******************************@comcast.com. ..
>>"Vince Morgan" <vinharAtHereoptusnet.com.auwrote:
[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]
>
Sorry, Vince. I agree with Tim. You're grasping at straws here.
I "almost" agree with that assesment Jerry. However, what I wrote above is
what is in the current manual. However, if I had read further I would have
discovered it was deficient.
This pointer may be used with fgets(), fgetss(), and fwrite().
I was only stating what the manual says, not suggesting anything in that
respect. If quoting from the manual is a "suggestion" as opposed to an
observation I agree totaly. That they are all I/O functions should have
been obvious to me also, but I missed that too @@
Having said that I agree Tim was right that I should have stayed out of it.
And I apologise for my testy reply.
Highest regards,
Vince
Dec 28 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Bryan | last post by:
i have a batch file that contains these two lines: -- args.bat echo %1 echo %2 when i run args.bat from the command line it works correctly... notice that abc has quotes in the first...
2
by: Stuart McGraw | last post by:
When I run (Python 2.3.3) this script on a MS Windows 2000 machine: import os print "1st popen..." f = os.popen ("echo " + u"\u0054\u0045\u0053\u0054.txt") print f.read () print "2nd popen..."...
10
by: Nick Craig-Wood | last post by:
I'm trying to avoid using shell metacharacters in os.popen in a portable fashion. os.popen() only seems to take a string as the command which would need tricky quoting. os.popen2() can take a...
2
by: hubritic | last post by:
I am trying to set off commands on Windows 2003 from python. Specifically, I am trying to use diskpart with a script file (pointed to with path). cmd = p = Popen(cmd, shell=True) The script...
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
11
by: John Nagle | last post by:
I passed a dict for the "env" variable to Popen with Unicode strings for the dictionary values. Got: File "D:\Python24\lib\subprocess.py", line 706, in _execute_child TypeError: environment...
8
by: clyfish | last post by:
In cmd, I can use find like this. C:\>netstat -an | find "445" TCP 0.0.0.0:445 0.0.0.0:0 LISTENING UDP 0.0.0.0:445 *:* C:\> And os.system is OK....
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
1
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've written a class to work with the input and output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.