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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化