Closing A Program Using Exec("")

 
#1

Quick question. As the title suggests, after opening a program or file in Windows 8 using the EXEC() command (e.g, opening a picture), how can you close that program or file using the execute command?

Thanks.

#2

From a script I would do something like this...

Code:


$attributes = "/IM process.exe>nul"
Exec( "taskkill", $attributes )



process.exe would be the name of the exe that you want to kill.

This is a post on another forum that I found about a similar topic.

"The proper way to close/exit a program ultimately depends upon the software. However, generally the best practice is for Windows programs to close whenever they receive the WM_CLOSE message. Properly releasing memory and closing handles. There are other messages that can signal the close of the application, but it is up to the author of the software how each message is handled.

taskkill /IM process.exe
taskkill sends the WM_CLOSE message and it is then up to the application whether to properly close. You may also want to use the /T option to also signal child processes.

Only use the /F option if you want to force the termination of the process.

WM_CLOSE vs SC_CLOSE
How to Gracefully Terminate a Process
How To Terminate an Application "Cleanly" in Win32

Other options would include sending the Alt+F4 keys, using PowerShell, or 3rd party applications.

Update to Updated Question

Ignore, the errors. Chrome generates many processes. The errors are caused when an process does not acknowledge the WM_CLOSE message that TASKKILL sends. Only processes with a message loop will be able to receive the message, therefore, the processes that do not have a message loop will generate that error. Most likely, these processes are the chrome extensions and plugins.

To hide the errors capture the output

taskkill /IM chrome.exe >nul
Summary: TASKKILL is the proper way via command line to close applications per its WM_CLOSE implementation and the Microsoft KB Article that I have linked."

#3

Thanks David. I'll give it a try.

#4

@David.

I gave it a shot. When I open the CMD prompt window and type"TaskList" the list shows shows what's running and had success using "TaskKill" closing my web browser, but I can't seem to find the correct image name or PID code for things like Windows photo viewer or video player which are open. They are more than likely there but don't recognise the correct image name.

Haven't tried your script yet.

#5

let me see if I can find the name for the photo viewer or video player. I am sure they are named something weird. Give me a moment and I will try to find what they are called.

#6

taskkill /F /IM wmplayer.exe for Media Player. Trying to find the Windows Photo Viewer.

#7

taskkill /F /IM PhotosApp.exe will work for the Photos in Windows 8.

#8

Not sure what I'm doing wrong, taskkill /F /IM PhotosApp.exe didn't close the app down for me.

Code:

exec("C:\Users\steve_000\Pictures\EZ robot project showcase\k9 1.png")

sleep(2000)

$attributes = "IM PhotosApp.exe>nul"
exec("taskkill",$attributes)

#9

Code:


exec("C:\Users\steve_000\Pictures\EZ robot project showcase\k9 1.png")

sleep(2000)

$attributes = "/F /IM PhotosApp.exe>nul"
exec("taskkill",$attributes)
sleep(1000)
$attributes = "/F /IM dllhost.exe>nul"
exec("taskkill",$attributes)

#10

Lol, still not doing what it should.

What is the "PhotosApp" for? Is it for the "Windows Photo viewer" app or the "Photos" app? Either way, even with the script you just supplied David, it opens the photo, but doesn't close it (although I do see the CMD window flash up very quickly after the 2 second sleep).