Page 1 of 1

[SOLVED] running commandline exe in windows

Posted: Wed Jun 08, 2005 5:39 pm
by batfastad
I've searched and gone through loads of information on the subject of using exec() on windows but I still can't seem to get this working.

PHP 4.3.11, Apache 1.3.33, Windows XP SP1

I have a command-line exe file located in c:\scripts
My PHP scripts are located in c:\htdocs

The command-line exe is called CLamp.exe and is a WinAmp plugin to check/control the status of winamp 5 on that machine.

When you call CLamp.exe with various switches you can do various things.
I'm trying to do this through a web interface.

I just need to get the result of the command

In the PHP manual there's lots going on about hiding the window, minimising the window, and frankly I don't really care.
It would be nice if it was completely hidden, but just so long as it works.

On the user notes of the exec() function in the php manual, I've tried using the example from chads2k2.yahoo.com and modifying it slightly.

Code: Select all

<?php
// Get a good ol' timestamp.  Unique and it works.
$unixtime = time();

// Sets up your exe or other path.
$cmd = 'c:\\scripts\\clamp.exe';

// Setup an array of arguments to be sent.
$arg[] = '1';
$arg[] = '2';
$arg[] = '3';
$arg[] = '4';
$arg[] = '5';

// Pick a place for the temp_file to be placed.
$outputfile = 'c:\\scripts\\unixtime.txt';

// Setup the command to run from "run"
$cmdline = "cmd /C $cmd " . implode(' ', $arg) . " > $outputfile";

// Make a new instance of the COM object
$WshShell = new COM("WScript.Shell"); 

// Make the command window but don't show it.
$oExec = $WshShell->Run($cmdline, 0, true);

// Read the file file.
$output = file($outputfile);

// Delete the temp_file.
unlink($outputfile);

// Take the output and break the array apart. 
// If you don't know what is coming out do:
// print_r("$output");
foreach($output as $temp_output)
{
   // Check the output's return code.
   if($temp_output == 1)
   {
     // Tell the user it was correct.
     echo "All is good";
   }else{
     // Tell the user something goofed.
     echo "Didn't go smoothly.";
   }
}
?>
All I get however is "Didn't go smoothly"

When the output I should get (hopefully) is "No option" (because I didn't specify any switches in $cmd

Any ideas on where I'm going wrong with this?

I've tried all the different methods mentioned on the manual page, but this method seems to be the one best suited to what I'm looking for here.

Thanks

Ben

Posted: Thu Jun 09, 2005 4:19 am
by Syranide
I honestly don't know what you are trying to do with the last foreach... each line in the file should be 1? what would be the purpose of that...

have you just tried looking inside the file and see if it is correct? you should always break it down as far as possible before doing other things, that could prove to be the error.

Posted: Thu Jun 09, 2005 9:01 am
by batfastad
I don't know what the purpose of the foreach is. It's just from an example given in the usernotes under the manual for exec()


Is there a better way to run this external script, and get the output to my page?

Thanks

Ben

Posted: Thu Jun 09, 2005 2:16 pm
by Benjamin
Well this code works. Do a google search for ShelExec.exe

Code: Select all

$command = 'D:\\sys\\bin\\ShelExec.exe calc';
$output = shell_exec("$command");

Posted: Thu Jun 09, 2005 5:50 pm
by batfastad
Still just getting a blank page - not the output of clamp.exe

I've got clamp.exe and shelexec.exe in c:\scripts
I've also put clamp.exe into my windows directory - in case it's a path related problem. But still the same happens.

Here's my code...

Code: Select all

$command = 'C:\\scripts\\ShelExec.exe clamp.exe /?';
$output = shell_exec("$command");

echo ($output);

When I try this...

Code: Select all

$command = 'C:\\scripts\\ShelExec.exe calc.exe';
$output = shell_exec("$command");

echo ($output);
And take a look at the processes running on the server, sure enough a copy of calc is running (mysteriously nowhere to be seen).

Any idea why the output isn't coming out?


I've tried
$command = 'C:\\scripts\\ShelExec.exe C:\\scripts\\clamp.exe /?';
$command = 'C:\\scripts\\ShelExec.exe C:\scripts\clamp.exe /?';
$command = 'C:\\scripts\\ShelExec.exe clamp.exe';
$command = 'C:\\scripts\\ShelExec.exe clamp.exe /?';
And none seem to work.

I don't really understand the syntax of shelexec.exe though - what's the /WAIT, /EXE, /VERB switches about - their mentioned in passing and in the examples in the documentation.

Any ideas guys?

Thanks

Ben

Posted: Fri Jun 10, 2005 4:03 pm
by Benjamin
Yeah there is a buried setting you need to change.

Click on start > run > type MMC > OK

Then click File > Add Remove Snap In
Then click Add > Services > Finish > Close > OK

Then click Services > Right Click on Apache2 or Apache

Then click Properties > Log On

Check the box that says, "ALLOW SERVICES TO INTERACT WITH DESKTOP"

Click ok then restart Apache and the programs started by apache will show up on the desktop!

Posted: Mon Jun 13, 2005 2:13 pm
by batfastad
Hi guys

I'm still struggling with this!

Interact with desktop is now ticked and something seems to be happening...

Code: Select all

$command = 'C:\\scripts\\ShelExec.exe /WAIT C:\\scripts\\clamp.exe /?';
$output = shell_exec("$command");

if ($output === FALSE) {
	echo ("<b>false</b>");
} else {
	echo ($output);
}
When I execute that, the black window of the CLI of clamp.exe pops up briefly, and appears as a process briefly.

But I don't get the output returned.

Though I don't get 'false' returned either which is a bit more encouraging.

Would the process be exiting too quickly for the output to be sent to php?


At least I can get the process started, just need to get the output now.

Any ideas?

Thanks

Ben

Posted: Mon Jun 13, 2005 6:34 pm
by froth
I'm going to go completely out on a limb here and say that the output is displayed but you just don't see it because the window closed so fast. You know, like Windows command-line C++. You had to send "Pause" as a parameter to some exec method of the System class so that the window would stay open after all the output is printed.

Posted: Wed Jun 29, 2005 9:04 am
by batfastad
In case anyone was worried, I have found a solution.

So I found that the command was being run successfully. For some reason using ShelRun was actually screwing things up.

But I needed to get the output in my script.

Of course I'd forgotten from my DOS days that you can pipe the output of pretty much any command to a text file, by adding >textfile.txt to the end of the command.

So I run the command, piping the output to a text file.
Handily when running the commandline exe script through PHP on windows, the working directory it runs in is whatever the directory of the PHP script.
So the text file was being generated in the same directory as my PHP files.

Then a simple file_get_contents() to pull in the result.

I now have a fully functioning web interface for a copy of winamp on my home media server.
Result!