duandu8202 2012-03-18 22:47 采纳率: 0%
浏览 45
已采纳

php exec()与windows 8 metro应用程序的响应不同

I wanted to change the tile icons for desktop applications in the new windows 8 start menu. So they would fit in with the other metro apps.

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

<?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 launch this file from chrome it echos "Programm is running".

That's great!

If I launch it from windows start 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');

From the start menu. It will open a new Chrome window regardless of if chrome is already open.

So I guess my question is : What can I do that will allow my php file to check if chrome is open and if it is not , to open it?

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

My best guess is that it has do less with my commands and more to do with chrome itself. It could be a target that I need to add, I don't know.

  • 写回答

1条回答 默认 最新

  • doudandui1592 2012-04-23 19:37
    关注

    You can use Windows Management Instrumentation:

    If you have not used wmic before you should install it by running wmic from cmd.exe. It should then say something like:

    WMIC Installing... please wait.
    

    After that wmic is ready for 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
    ]*/','/[^0-9]+
    |
    $/','/
    /'), 
            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 have not tested this and just wrote it out of my head so there might be some fixing and you should check wmic's output by running it from commandline with same args to see if preg_replace() is doing it right (get pid from wmic's output).

    UPDATE:

    Tested and it seems that wmic does not return any status codes so updated my php function to reflect this bahavior.

    UPDATE:

    Now it handles multiple processes too and returns all pids as indexed array or false when no process running.

    About WMI:

    Windows Management Instrumentation is very powerful interface and so is wmic commandline tool. Here is listed some of WMI features

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?