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

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

报告相同问题?

悬赏问题

  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面