How to Start a Windows Gui Program Using PHP

How can I start a Windows GUI program using PHP?

To launch a program on the computer which runs the webserver:

<?php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');

The above will work on vista/win7 IF the webserver does not run as a windows service. For example, if you run apache and it automatically starts when your computer boots, you probably installed it as a service. You can check to see if apache shows up in the windows services tab/thingy.

If the webserver runs as a service, you'll need to look into enabling the "allow desktop interaction" option for the service. But otherwise:

An easy test using php's new built in webserver(php 5.4+). The key thing here is you manually start the server from a shell, so it runs as your user instead of as a service.

<?php
// C:\my\htdocs\script.php
exec('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\foo.php"');

start a webserver via a command window

C:\path\to\php.exe -S localhost:8000 -t C:\my\htdocs

Then in your browser
http://localhost:8000/script.php

PHP how to launch windows GUI application

I assume that PHP is running within Apache, which in turn is a service.

Now starting any application from service will not have its GUI displayed, as Service is running in separate session which does not allow interaction with users Desktop.

See this answer for more details:
Service starting a process wont show GUI C#

However there can be other ways to achieve this.

  1. Create a custom C++ (or equivalent) application that will create your target GUI application for the given user. The answer How can a Windows service execute a GUI application? explains CreateProcessAsUser() for that. This method will require user name and password to be specified.

  2. Create custom client-server kind of application. The server part will always be running inside in User mode where the GUI needs to be displayed. And the client will be called from PHP. When client is invoked, it will signal the Server part using IPC like event. Server can start the GUI application in turn.

  3. Use the Microsoft's PSEXEC utility to start the process in GUI. However this will need user name, password and session id.

    psexec.exe \\REMOTE -u USER -p PASS -i SESSION -d C:\Windows\Notepad.exe

    SESSION is the session id (Use Task Manager -> User Tab to see your session ID)

    USER, PASS is the user name and password for the user

php How do I start an external program running - Having trouble with system and exec

What behavior are you expecting? Calling system('notepad') works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).

If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).

Open .exe from php webapp and login

So, in light of the comment/chat we had under your question, using phpseclib, you should be able to connect to the remote machine, which will run your the application:

require_once('Net/SSH2.php');
$ssh = new Net_SSH2('winhostaddress');
//if needed:
if (!$ssh->login('user', 'pass'))
{
exit('Failed to connect to win');
}
$ssh->exec('C:\\Path\\to\\app.exe argument1 argument2 argument3');

That should do the trick. When using flags, like on *NIX systems: ls -a, you should know that the Windows counterpart is /. Other than that, it's all pretty much the same. escapeshellarg is not to be forgotten, when passing client-data to the executable.

Arguments containing spaces should be delimited using double quotes.

If you're not sure weather or not the application you're launching is accessible to the user you used to connect via ssh, try prepending this to your actual command:

runas /user:username C:\\Path\\To\\App.exe

Where username is a user of which you're sure that has the appropriate privileges. Note that you might be prompted for a pwd.

For any other issues, here's some related questions:

  • php How do I start an external program running - Having trouble with system and exec
  • How can I start a Windows GUI program using PHP?
  • PHP - Any chance to run GUI program through exec()?

Can PHP be used to control a GUI application?

You can definitely do it, but it would take a bit of hackery. One way to do it would be to write a Windows command-line app that would send button clicks and such to the Windows GUI app. You then call this command-line app using PHP and the exec and system calls.

For example, if you were passing it say, Firefox, you would have a firefox-wrapper app that could be called like this:

firefox-wrapper open-url http://stackoverflow.com
firefox-wrapper press-back
...


Related Topics



Leave a reply



Submit