0

I am a php developer, and would like to use php + some windows api to perform a task.

The task is: asking nicely a running windows application to close itself.

It's certainly not natural to do this with php, but I'm writing my small script in php as this is the language I'm using these days and comfortable with.

shealtiel
  • 8,020
  • 18
  • 50
  • 82

2 Answers2

1

There is some support in PHP to call the win32 API. However, it looks like the stability of that extension isn't very good.

I think the best you can do is to use the system call.

kokx
  • 1,706
  • 13
  • 19
  • This nice utility http://www.nirsoft.net/utils/nircmd2.html did the job for me. it can: nircmd closeprocess processname I run it with a system call – shealtiel Jan 07 '13 at 19:20
1

The php.net site offers some guidance:

http://php.net/manual/en/book.w32api.php

w32api_deftype w32api_init_dtype w32api_ invoke_function w32api_register_function w32api_set_call_method

The cleanest way is to send a WM_CLOSE message to a Windows program.

I more brutal but effective way to kill a Win32 process is to use pskill.exe.

You can download it here: http://technet.microsoft.com/en-us/sysinternals/bb896683.aspx

You can also call into a vbscript program as follows:

' ProcessKillLocal.vbs
' Sample VBScript to kill a program
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.7 - December 2010
' ------------------------ -------------------------------' 
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill 
strComputer = "."
'YOU NEED TO REPLACE calc.exe WITH THE APP YOU WISH TO KILL
strProcessKill = "'calc.exe'" 

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next 
WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer
WScript.Quit 
' End of WMI Example of a Kill Process 
Bruno
  • 498
  • 1
  • 4
  • 9