Control Windows from PHP (esp. run .bat files)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    Control Windows from PHP (esp. run .bat files)

    I have tried posting in PHP forum but no reply. I think it's not a problem with PHP anyway so perhaps someone in this forum can help.

    When I use the code below to try and launch notepad.exe from within a PHP script using the exec() function, I can see the process has been added to the process list (from task manager) but notepad hasn't started! The process cannot be forced to quit either.

    The exec() function seems to work but I want the application to launch properly - is there a trick when using the command prompt to having a programme actually launch, as compared to just having the process start?


    [CODE=php]
    $cmd='notepad.e xe'
    exec($cmd);
    [/CODE]

    My system is: win2k pro, apache 2, php 5.2

    Thanks!
  • dafodil
    Contributor
    • Jul 2007
    • 389

    #2
    Originally posted by henryrhenryr
    I have tried posting in PHP forum but no reply. I think it's not a problem with PHP anyway so perhaps someone in this forum can help.

    When I use the code below to try and launch notepad.exe from within a PHP script using the exec() function, I can see the process has been added to the process list (from task manager) but notepad hasn't started! The process cannot be forced to quit either.

    The exec() function seems to work but I want the application to launch properly - is there a trick when using the command prompt to having a programme actually launch, as compared to just having the process start?


    [CODE=php]
    $cmd='notepad.e xe'
    exec($cmd);
    [/CODE]

    My system is: win2k pro, apache 2, php 5.2

    Thanks!
    Try to download this program, it helps:


    After installing it....

    try to use this format:
    [code=php]
    exec("psexec -d blah.bat");
    [/code]
    -------------------------------------------------------------------------------------------------------------

    If you don't like the above solution

    You can also use the run.......

    [code=php]

    //start Notepad.exe minimized in the background:
    $WshShell = new COM("WScript.Sh ell");
    $oExec = $WshShell->Run("notepad.e xe", 7, false);

    //start a shell command invisible in the background:
    $WshShell = new COM("WScript.Sh ell");
    $oExec = $WshShell->Run("cmd /C dir /S %windir%", 0, false);

    /*start MSPaint maximized and wait for you to close it before continuing the
    script:*/
    $WshShell = new COM("WScript.Sh ell");
    $oExec = $WshShell->Run("mspaint.e xe", 3, true);

    [/code]

    Hope this helps....

    Comment

    • henryrhenryr
      New Member
      • Jun 2007
      • 103

      #3
      Hi dafodil

      Thanks very much for your reply - I think I'm very close.

      Tried both methods. They both seem to work, except that the application is never maximised.

      On the second method, for the first one (...->Run(notepad.ex e, 7, false); ), the process launches (according to task manager) and I can close it with task manager.

      For the third one (...->Run(mspaint, 3, true); ), the script continues running and the application starts in the background but doesn't maximise. The script only stops running when I close the process in the task manager.

      Perhaps it's something preventing Apache telling windows to maximise it? I've had a look on some sites for some clues but I can't seem to work it out...

      Comment

      • dafodil
        Contributor
        • Jul 2007
        • 389

        #4
        Originally posted by henryrhenryr
        Hi dafodil

        Thanks very much for your reply - I think I'm very close.

        Tried both methods. They both seem to work, except that the application is never maximised.

        On the second method, for the first one (...->Run(notepad.ex e, 7, false); ), the process launches (according to task manager) and I can close it with task manager.

        For the third one (...->Run(mspaint, 3, true); ), the script continues running and the application starts in the background but doesn't maximise. The script only stops running when I close the process in the task manager.

        Perhaps it's something preventing Apache telling windows to maximise it? I've had a look on some sites for some clues but I can't seem to work it out...
        Try this to give Apache permission to interact with your desktop:

        Start>Run>servi ces.msc
        Right click "Apache..." , select properties.
        Click on the "LOG ON" tab
        Check the box "Allow this service to interact with desktop"
        Click OK
        Restart Apache

        Comment

        • henryrhenryr
          New Member
          • Jun 2007
          • 103

          #5
          Hi Dafodil

          I don't seem to be having much luck here. Followed those steps but no effect.

          The exec() function definitely works because I can run commands directly through it. Simple example would be ipconfig which works and returns expected output with the following:

          [CODE=php]
          exec("ipconfig /all",$output);
          var_dump($outpu t);
          [/CODE]

          I don't know why it refuses to maximise or start applications. Also seems to have trouble executing batch files - exactly the same command in a batch file doesn't work.

          I suppose I'll stick to using simple commands with the exec() function for now. Any further ideas would be really welcome but thanks for your help up to now!

          Comment

          • henryrhenryr
            New Member
            • Jun 2007
            • 103

            #6
            Hi Dafodil

            The problem seems to be fixed. Think the last part helped although only after I restarted my whole system for some reason.

            And I realised that some of my commands in exec() weren't properly escaped so they weren't being recognised.

            Thanks for your help!

            Henry

            Comment

            • dafodil
              Contributor
              • Jul 2007
              • 389

              #7
              Originally posted by henryrhenryr
              Hi Dafodil

              The problem seems to be fixed. Think the last part helped although only after I restarted my whole system for some reason.

              And I realised that some of my commands in exec() weren't properly escaped so they weren't being recognised.

              Thanks for your help!

              Henry
              Welcome. Post again whenever you face any problem.

              Comment

              Working...