Reply to Re: popen on Windows

Your name:

Reply:


Posted by Daniel Klein on 12/26/07 16:27

On Tue, 25 Dec 2007 12:22:33 -0800 (PST), Csaba Gabor
<danswer@gmail.com> wrote:

>On Dec 23, 11:42 am, Daniel Klein <dani...@featherbrain.net> wrote:
>> On Sun, 23 Dec 2007 14:50:59 GMT, Daniel Klein
>>
>>
>>
>> <dani...@featherbrain.net> wrote:
>> >On Mon, 24 Dec 2007 00:44:14 +1000, "Vince Morgan"
>> ><vinharAtHereoptusnet.com.au> wrote:
>>
>> >>"Daniel Klein" <dani...@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");
>> >>> }
>>
>> 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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация