Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Close an application - doesn't work!


  • Please log in to reply
32 replies to this topic
Albireo
  • Members
  • 558 posts
  • Last active: Jul 27 2019 10:36 AM
  • Joined: 01 Feb 2006

Hello!

I have a program that sometimes is stop to work.

When I try to close this program with AHK - it doesn't work.

 

But if I open Task Manager and right-click on the process, the program is stopped and closes.

 

I have tried to close the program, with the following instructions .: (but failed)

#NoEnv
#SingleInstance force
SetBatchLines -1
SetWorkingdir  %A_ScriptDir%

ProcessName := "Pvxwin32.exe"
SCserverTxt := "Star Communication Server"

; Get the ID for the process
Process Wait, %ProcessName%, 5.5
NewPID = %ErrorLevel%  ; Save the value immediately since ErrorLevel is often changed.
If NewPID = 0
   {
   MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, Unable to find process "Pvxwin32.exe" in 5.5 seconds
   Return
   }

; DetectHiddenWindows on
WinGetTitle WinTitle, ahk_pid %NewPID%    ; Otherwise, get the Win Title for the program
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IfWinExist %WinTitle%, %SCserverTxt%
   {
   MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, Found .: `n%WinTitle% `n`nText .: %SCserverTxt%
   
   ; - - - PostMessage
   PostMessage 0x112, 0xF060,,, %WinTitle%, %SCserverTxt%    ; Maybe the same instruction as WinClose
   If ErrorLevel
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The following program could not be stopped with "PostMessage"!. `n`nWinTitle = %WinTitle% `n`nPID .: %NewPID%
   else
      {
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The program could be stopped with "PostMessage" `nErrorLevel = %ErrorLevel%. (not true - ExitApp is remarked)
      ; I don't know why the ErrorLevel is "0" - even if the desired window is not closed.
      ; ExitApp    ; The mission completed
      }

   
   ; - - - WinWaitClose   
   WinWaitClose %WinTitle%, %SCserverTxt%, 10
   If ErrorLevel    
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The following program could not be stopped with "WinWaitClose"!. `n`nWinTitle = %WinTitle% `n`nPID .: %NewPID%
   else
      {
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The program could be stopped with "WinWaitClose".
      ExitApp    ; The mission completed
      }

   ; - - - WinKill
   WinKill %WinTitle%, %SCserverTxt%, 10
   If ErrorLevel
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The following program could not be stopped with "WinKill"!. `n`nWinTitle = %WinTitle% `n`nPID .: %NewPID%
   else
      {
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The program could be stopped with "WinKill".
      ExitApp    ; The mission completed
      }
       
   ; - - - Process Close
   Process Close, %NewPID%
   }

Any Idea how to close my program in same way as with the task manager?#NoEnv
#SingleInstance force
SetBatchLines -1
SetWorkingdir  %A_ScriptDir%

ProcessName := "Pvxwin32.exe"
SCserverTxt := "Star Communication Server"

; Get the ID for the process
Process Wait, %ProcessName%, 5.5
NewPID = %ErrorLevel%  ; Save the value immediately since ErrorLevel is often changed.
If NewPID = 0
   {
   MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, Unable to find process "Pvxwin32.exe" in 5.5 seconds
   Return
   }

; DetectHiddenWindows on
WinGetTitle WinTitle, ahk_pid %NewPID%    ; Otherwise, get the Win Title for the program
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IfWinExist %WinTitle%, %SCserverTxt%
   {
   MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, Found .: `n%WinTitle% `n`nText .: %SCserverTxt%
   
   ; - - - PostMessage
   PostMessage 0x112, 0xF060,,, %WinTitle%, %SCserverTxt%    ; Maybe the same instruction as WinClose
   If ErrorLevel
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The following program could not be stopped with "PostMessage"!. `n`nWinTitle = %WinTitle% `n`nPID .: %NewPID%
   else
      {
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The program could be stopped with "PostMessage" `nErrorLevel = %ErrorLevel%. (not true - ExitApp is remarked)
      ; I don't know why the ErrorLevel is "0" - even if the desired window is not closed.
      ; ExitApp    ; The mission completed
      }

   
   ; - - - WinWaitClose   
   WinWaitClose %WinTitle%, %SCserverTxt%, 10
   If ErrorLevel    
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The following program could not be stopped with "WinWaitClose"!. `n`nWinTitle = %WinTitle% `n`nPID .: %NewPID%
   else
      {
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The program could be stopped with "WinWaitClose".
      ExitApp    ; The mission completed
      }

   ; - - - WinKill
   WinKill %WinTitle%, %SCserverTxt%, 10
   If ErrorLevel
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The following program could not be stopped with "WinKill"!. `n`nWinTitle = %WinTitle% `n`nPID .: %NewPID%
   else
      {
      MsgBox 64, %A_ScriptName% - Rad %A_LineNumber%, The program could be stopped with "WinKill".
      ExitApp    ; The mission completed
      }
       
   ; - - - Process Close
   Process Close, %NewPID%
   }

Any idea how to close an process on the same way as "right click" in the task manager?

 

(I haven't tried .: run as Administrator. But I have the administrators rights)

 

//Jan

 

 

 

 

 



tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007

settitlematchmode, slow?


Never lose.
WIN or LEARN.

girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010

have you looked at Process, Close ?


The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


Albireo
  • Members
  • 558 posts
  • Last active: Jul 27 2019 10:36 AM
  • Joined: 01 Feb 2006

settitlematchmode, slow?

No difference!

I got the PID for the process, and the first 'If statement' works.

 

 

have you looked at Process, Close ?

(I tried with this instruction as the last command in my test application.)

I got ErrorLevel = 0, (I don't know how ErrorLevel works together with Process Close)

And the program (Pvxwin32) does not quit.

 

The program that I am testing against, has stopped working, but it is not a problem to get the PID or WinTxt and so on.

 

I forgot to say .: I forgot to say that the OS on the computer is Win 8.1

 

//Jan



GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013

Could this be of any help in this case?

Process, Exist, Pvxwin32.exe
 If !ErrorLevel	; is not running
  {
   MsgBox, Process "Pvxwin32.exe" does not exist
	  return
  }
 ; else 	; is running
 pid := ErrorLevel
 IfWinNotExist, % "ahk_pid " pid
 {
   Process, Close, Pvxwin32.exe
 }  


Albireo
  • Members
  • 558 posts
  • Last active: Jul 27 2019 10:36 AM
  • Joined: 01 Feb 2006

Thanks for your try / idea!

 

Could this be of any help in this case?

Process, Exist, Pvxwin32.exe
 If !ErrorLevel	; is not running
  {
   MsgBox, Process "Pvxwin32.exe" does not exist
	  return
  }
 ; else 	; is running
 pid := ErrorLevel
 IfWinNotExist, % "ahk_pid " pid
 {
   Process, Close, Pvxwin32.exe
 }  

The same problems as Previously - nothing happened, the program still exists. (does not close)



trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

What are the contents of A_LastError and Errorlevel after invoking the Process, Close command?

 

// a request for the moderators: could the duplicate posts please be deleted, as it seems there is no button to delete the post.


New Autohotkey forum: http://ahkscript.org.


trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

_


New Autohotkey forum: http://ahkscript.org.


trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

_


New Autohotkey forum: http://ahkscript.org.


Oldman
  • Members
  • 2475 posts
  • Last active: Feb 18 2015 04:57 PM
  • Joined: 01 Dec 2013

You could try the Taskkill command or the Tskill command.

It depends on the existence of the command.

If both exist, it's better to use Taskkill.

RunWait, taskkill /f /t /im Pvxwin32.exe,, Hide UserErrorLevel
; or
RunWait, tskill Pvxwin32,, Hide UserErrorLevel

Si ton labeur est dur et que tes résultats sont minces, souviens toi du grand chêne qui avant n'était qu'un gland....comme toi ! (anonyme) ;)

L'art de lire, c'est l'art de penser avec un peu d'aide. (É. Faguet)

Windows 3.1. Collector's Edition.     (www.avaaz.org)


trismarck
  • Members
  • 390 posts
  • Last active: Nov 25 2015 12:35 PM
  • Joined: 02 Dec 2010

In addition to what Oldman has posted, please post the value of A_LastError and Errorlevel as we would like to know, why Process, Close has failed. Perhaps the reason could be obtained by looking at A_LastError.


New Autohotkey forum: http://ahkscript.org.


Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

I propose the idea that Process, Close didn't fail and perhaps the said program is being re-opened by another process/service. Check all child/parent processes and services, that may be the cause.


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Oldman
  • Members
  • 2475 posts
  • Last active: Feb 18 2015 04:57 PM
  • Joined: 01 Dec 2013

It seems that the "Process, Close" failed. Albireo reported an ErrorLevel = 0.


Si ton labeur est dur et que tes résultats sont minces, souviens toi du grand chêne qui avant n'était qu'un gland....comme toi ! (anonyme) ;)

L'art de lire, c'est l'art de penser avec un peu d'aide. (É. Faguet)

Windows 3.1. Collector's Edition.     (www.avaaz.org)


Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

Ah alright. Reading back through the comments, I don't believe it was mentioned if Process, Exist actually worked. Is it possible that the program has access/privilege restrictions?


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007

yes. some processes are prevented from being ended


Never lose.
WIN or LEARN.