United Kingdom
Asked — Edited

Closing A Program Using Exec()

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.


ARC Pro

Upgrade to ARC Pro

Harnessing the power of ARC Pro, your robot can be more than just a simple automated machine.

#1  

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


$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."

United Kingdom
#2  

Thanks David. I'll give it a try.

United Kingdom
#3  

@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.

#4  

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.

#5  

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

#6  

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

United Kingdom
#7  

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

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

sleep(2000)

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

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)


United Kingdom
#9  

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).

#10  

hmm, interesting. The DLLHost exe is what was keeping it open for me. Try this...

Open the photo.

From a command line try this taskkill /F /IM PhotosApp.exe

Tell me what the next line says.

Then, try this taskkill /F /IM dllhost.exe

Tell me what the next line says.

I am looking to see if a line like this shows up "SUCCESS: The process "PhotosApp.exe" with PID 8312 has been terminated." and then "SUCCESS: The process "dllhost.exe" with PID 7568 has been terminated."

United Kingdom
#11  

Having the photos open with Windows PhotonViewer as default, opening a photo and using the PID code for "dllhost.exe" closes the photo showing...

"SUCCSESS: Sent termination signal to the process with PID 6464.

so all good there.

But within ARC, it's a different matter. Running the script opens the photo, sleeps for 2 seconds, then I see the CMD window flash once, but photo remains open with the script console saying "Done".


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

sleep(2000)

$attributes = "/F /IM dllhost.exe>nul"
exec("taskkill",$attributes)
#12  

This works for me from ARC script.


exec("C:\current build progress.jpg")
sleep(2000)
$attributes = "/F /IM dllhost.exe"
Exec( "taskkill", $attributes )

United Kingdom
#13  

Thanks buddy. I'll keep trying things out my end.

#14  

it may be the >nul that was causing the issue. Try to remove that.

United Kingdom
#15  

Yep, you nailed it my friend. Your last script worked for me too.

That's 2 for 2 David. Once again, thanks very much for your help. Most grateful.:)

#16  

Glad we found a solution and happy to help. Thanks Steve.