Php exec () does not respond like windows 8 metro application

I wanted to change the tile icons for desktop applications in the new Windows 8 launch menu. In this way, they will fit into other metro applications.

I made a simple metro application that calls a simple php file localhost

<?php // check if the chrome is in the task list exec('tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL | find /I /N "chrome.exe">NUL'); // get a return value I can check $runing = exec('if "%ERRORLEVEL%"=="0" echo Programm is running'); if ($runing === 'Programm is running'){ // the program is open already echo $runing; } else { // the program is not running and should be opened exec('C:\Users\Gerdy\AppData\Local\Google\Chrome\Application\chrome.exe'); } ?> 

If I run this file with chrome, its echo is "The program is running."

It's great!

If I started it from Windows startup and Chrome is not running, Chrome does not start.

If I exclude the if statement and just run.

 exec('C:\Users\Gerdy\AppData\Local\Google\Chrome\Application\chrome.exe'); 

On the Start menu. It will open a new Chrome window regardless of whether chrome is open.

So, I think, my question is: What can I do to let my php file check if chrome is open, and if not, open it?

This model actually works for any other program, not browsers.

My best guess is that he does less with my teams and is more connected with chrome itself. This may be the goal I need to add, I do not know.

+4
source share
1 answer

You can use the Windows management tools:

If you have not used wmic before installing it, run wmic from cmd.exe . Then he should say something like:

 WMIC Installing... please wait. 

After that, wmic ready to use:

 function getProcessId( $imagename ) { ob_start(); passthru('wmic process where (name="'.$imagename.'") get ProcessId'); $wmic_output = ob_get_contents(); ob_end_clean(); // Remove everything but numbers and commas between numbers from output: $wmic_output = preg_replace( array('/[^0-9\n]*/','/[^0-9]+\n|\n$/','/\n/'), array('','',','), $wmic_output ); if ($wmic_output != '') { // WMIC returned valid PId, should be safe to convert to int: $wmic_output = explode(',', $pids); foreach ($wmic_output as $k => $v) { $wmic_output[$k] = (int)$v; } return $wmic_output; } else { // WMIC did not return valid PId return false; } } // Find out process id's: if ($pids = getProcessId( "chrome.exe" )) { foreach ($pids as $pid) { echo "Chrome.exe is running with pid $pid"; } } else { echo "Chrone.exe is not running"; } 

I did not test this and just wrote it from the head so that there could be some corrections, and you should check the wmic output by running it from the command line with the same arguments to check if this makes preg_replace() correct (get pid from wmic) .

UPDATE:

It is tested and it seems that wmic does not return any status codes, therefore updated my php function to reflect this behavior.

UPDATE:

Now it processes several processes and returns all pids as an indexed array or false when the process is not running.

About WMI:

The Windows Management Tool is a very powerful interface, so the wmic command line tool. Listed below are some WMI features

+2
source

Source: https://habr.com/ru/post/1402146/


All Articles