Close a running program - doesn't work!

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Close a running program - doesn't work!

27 Nov 2014, 09:51

Hi!
I try to close a program.
The program I run, is called "Label32.exe *32" in the task manager.
The program is made for 32-bit, WinXP, and this computer have Win7 64-bit, but it works fine in Win7.
It have ahk_class = TMainForm (from Window Spy)

When I try to close "Label" with the following commands.

Code: Select all

SetTitleMatchMode 2
Process Exist, Label32.exe
PID := ErrorLevel   ; To get the PID of Label-program
If WinExist "ahk_class TMainForm"
    {
    MsgBox 64, Row %A_KineNumber%  -> %A_ScriptName%, The program is found!
    WinClose
    }
Nothing happens... (Except that the text "The program is found!" is shown)

I have tried .:

Code: Select all

WinClose "ahk class TMainForm"

Code: Select all

WinClose ahk_pid %PID%

Code: Select all

ControlSend ,, {Alt Down}{F4}{Alt Up}
But nothing happens!

Any idea why nothing works?
Is there anything in Windows 7 that limits the AHK?
(My AHK ice version 1.1.16.05)

//jan
just me
Posts: 9557
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Close a running program - doesn't work!

27 Nov 2014, 09:57

If WinExist "ahk_class TMainForm". If you want to call the WinExist function, you have to add parentheses: If WinExist("ahk_class TMainForm")
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Close a running program - doesn't work!

27 Nov 2014, 10:08

just me wrote:If WinExist "ahk_class TMainForm". If you want to call the WinExist function, you have to add parentheses: If WinExist("ahk_class TMainForm")
Thanks!

Both works for me!
I got the message "The program is found!" in both case.
(but nothing more)

I thought that parentheses is only needed when "OR", "AND" and so on should be used.

//Jan
just me
Posts: 9557
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Close a running program - doesn't work!

27 Nov 2014, 10:32

If WinExist "ahk_class TMainForm"doesn't 'work' as you meight expect. Because of the missing parantheses AHK cannot detect a function call. Instead it takes WinExist as a variable name and concatenates the content (empty, because no such variable exists) with the string "ahk_class TMainForm". Because the result is neither empty nor zero, the condition is True.

If WinExist("ahk_class TMainForm") will find any window of class TMainForm. If the MsgBox comes up, an arbitrary window of this class was found.

BTW: What's the content of PID?
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Close a running program - doesn't work!

28 Nov 2014, 06:15

Thank you for the explanation!

Had intended to use the command (without spaces), like this!

Code: Select all

IfWinExist "ahk_class TMainForm"
But maybe it's the same thing as applies?
The value for the command must have parenthesis? (as you described?)
_________________

Code: Select all

SetTitleMatchMode 2
Process Exist, Label32.exe
PID := ErrorLevel   ; To get the PID of Label-program
If WinExist ("ahk_class TMainForm")
    {
    MsgBox 64, Row %A_KineNumber%  -> %A_ScriptName%, The PID of the program is %PID%
    WinMaximize ahk_pid %PID%
    ControlSend ,,{Alt Down}{F4}{Alt Up}, ahk_pid %PID%
    WinClose
    }
It works!
I got the right PID for "Label32.exe", but nothing else happens. (WinMaximize / Close the program ...)
Is something more wrong in the code?
//Jan

//Jan
just me
Posts: 9557
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Close a running program - doesn't work!

28 Nov 2014, 06:34

If WinExist ("ahk_class TMainForm")
You must not insert a space between the function name and its parameter list:
If WinExist("ahk_class TMainForm")

If you want to use the command instead, you must not enclose the parameter in double-quotes:
IfWinExist ahk_class TMainForm.

If you've got the right PID, you should add the PID to the window's title, too:
If WinExist("ahk_class TMainForm ahk_pid" . PID)
or
IfWinExist ahk_class TMainForm ahk_pid %PID%
Guest

Re: Close a running program - doesn't work!

28 Nov 2014, 08:41

Great thanks!

I'm trying to manage the program with the following code.

Code: Select all

Process Exist, Label32.exe
PID := ErrorLevel

WinActivate ahk_class TMainForm ahk_pid %PID%
WinWaitActive ahk_class TMainForm ahk_pid %PID%
WinMaximize ahk_class TMainForm ahk_pid %PID%

IfWinExist ahk_class TMainForm ahk_pid %PID%
	{
	MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, The program is found - PID .: %PID%
	WinClose ahk_PID %PID%
	ControlSend ,,{Alt Down}{F4}{Alt Up}, ahk_pid %PID%
	; WinActivate "ahk_class TMainForm"
	; WinActivate ("ahk_class TMainForm")
	WinKill %PID%
	}
But it does not work as I thought.
What works?
1. I got the right PID, same as in the task manager.
2. The "IF" works
3. The MsgBox works.
But nothing else happens!
Expected that the program would be maximized before "MsgBox"
and closed after "MsgBox"

//Jan
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Close a running program - doesn't work!

29 Nov 2014, 07:12

Could it be a setting in Windows 7?
To get Label32.exe to work well, the program must be run in administrator mode (some changes are made to the windows registry at times.)

If I create a scheduled task for my program, with administrator rights, and run that task from AHK, I don't need to disable UAC.
The command to start the Label32 from AHK looks as follows .:

Code: Select all

RunWait %comspec% /c "schtasks.exe /run /tn Skyltprogram"
If I run this program (A search window in Label32 shall be opened).:

Code: Select all

; 1. Get the PID for the program
Process Exist, Label32.exe
myPID := ErrorLevel
; MsgBox,,, PID = %myPID%

; 2. Try to send "F2" to "Label32"
WinActivate ahk_pid  %myPID%
WinWait ahk_pid  %myPID%
Send {F2}
If I right after the AHK code above have carried out, is pressing "F2", the window "search" opens from "Label32" (as expected).

Why does not the "Send {F2}" (or some other commands) from AHK work for me?

//Jan
just me
Posts: 9557
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Close a running program - doesn't work!

30 Nov 2014, 05:32

Did you try to run your script as administrator, too?
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Close a running program - doesn't work!

30 Nov 2014, 09:58

just me wrote:Did you try to run your script as administrator, too?
Many thanks!
Haven't really thought about it.
I thought UAC window, always would opens if the need existed.

If i run the script as administrator, the "Send F2" works. (Maybe even WinMaximize - haven't tested yet).
But now opens the window "User Account Control", and asks if it's "OK" everytime.
(I don´t want to disable "UAC" control.)
Is that possible to handle the "UAC-window" with Autohotkey?


If I add "RunAs Administratör, xxx" (Swedish) in the script, and run the script as usual, it doesn't work.

Is it possible to control that the RunAs works as expected?
Does the "RunAs Administratör" works even if the User is not an administrator?

(this user is an administrator)

//Jan
lexikos
Posts: 9683
Joined: 30 Sep 2013, 04:07
Contact:

Re: Close a running program - doesn't work!

04 Dec 2014, 21:58

I do not believe you can use RunAs for this purpose - the process will run as some other user, but still without admin privileges. You must use Run *RunAs command to elevate the process. You cannot automate the UAC prompt from an unprivileged process; that would defeat the purpose of the prompt.

It is possible to allow AutoHotkey to automate higher-privilege processes, with some limitations: EnableUIAccess.
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Close a running program - doesn't work!

08 Dec 2014, 14:36

Thank´s for EnableUIAccess
maybe, this is my solution.
The only downside I can see, is that you always have to remember to create a new EXE-file adapted to a specific computer.

Been thinking about another option which I didn't manage, get to work.
Simply automatically answer "yes" when "User Access Control" -window appears on the screen. something like this .:

Code: Select all

#Persistent
SetTimer CheckUAC, 500
; Return


RunAsAdmin()
MsgBox 64, Rad %A_LineNumber% -> %A_ScriptName%, % A_IsAdmin	;%;

Process Exist, Label32.exe
myPID := ErrorLevel

WinActivate ahk_pid  %myPID%
WinWait ahk_pid  %myPID%

; WinGet LabelId, ID, ahk_class TMainForm ahk_pid %myPID%
; WinMaximize ahk_id %LabelId% ahk_pid %myPID% ahk_class TMainForm

ControlSend ,,{F2}, ahk_pid %myPID%
; Send {F2}
ExitApp

RunAsAdmin() {
	Global 0
	IfEqual, A_IsAdmin, 1, Return 0
	Loop, %0%
		params .= A_Space . %A_Index%
	DllCall("shell32\ShellExecute" (A_IsUnicode ? "":"A"),uint,0,str,"RunAs",str,(A_IsCompiled ? A_ScriptFullPath
	: A_AhkPath),str,(A_IsCompiled ? "": """" . A_ScriptFullPath . """" . A_Space) params,str,A_WorkingDir,int,1)
	ExitApp
}

CheckUAC:
	IfWinNotExist User Account Control, Process Complete
		Return
	; Otherwise:
	SetTimer CheckUAC, Off  ; i.e. the timer turns itself off here.
	SplashTextOn ,,, Close - User Account Control - Now
	Sleep 500
	SplashTextOff
Return
The window "User Access Control" is opened, but nothing more happens.
_________________________________________________

The third way is to run the program through the
How To Use The Task Scheduler To Launch Programs Without UAC Prompts
Again a unique installation on each computer is required.

//Jan
just me
Posts: 9557
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Close a running program - doesn't work!

09 Dec 2014, 02:22

lexikos wrote:You cannot automate the UAC prompt from an unprivileged process; that would defeat the purpose of the prompt.
lexikos
Posts: 9683
Joined: 30 Sep 2013, 04:07
Contact:

Re: Close a running program - doesn't work!

09 Dec 2014, 03:29

Albireo wrote:The third way is to run the program through the
How To Use The Task Scheduler To Launch Programs Without UAC Prompts
Again a unique installation on each computer is required.
Don't you install AutoHotkey on each computer? So just automate the process. Using AutoHotkey, if you like. ;)
Albireo
Posts: 1777
Joined: 16 Oct 2013, 13:53

Re: Close a running program - doesn't work!

09 Dec 2014, 17:44

Thanks!
But I don't get EnableUIAccess to work. :?
(My AHK is version is 1.1.16.05 and Windows 7 64-bit swe)

I get no error message when the EXE-file is created with "EnableUIAccess", or the created EXE-file is running.

Does it make any difference where the program "EnableUIAccess" is installed?
The EnableUIAccess.ahk (and lib) is now installed in "c:\Program files\autohotkey"
I tried to create the modified exe-file in "c:\Program files\autohotkey\TestUAC.exe"
When I run the created exe-file, a blue UAC-window (not yellow) is opened, before the program is running.
___________________________________

Is it possible to use "EnableUIAccess" on every EXE-file I want to disable the UAC-window?
(not only converted AHK-files?)
___________________________________

I have a program that I have to run as Administrator (reads and writes in the windows registry)
If I want to start and control that program with AHK. Must both the "Exe-program" and "AHK-file" be modified with "EnableUIAccess".
___________________________________

Is there any difference between running a "EnableUIAccess" created file, or to use the "Task Scheduler" to launch programs without an UAC prompts?

//Jan
lexikos
Posts: 9683
Joined: 30 Sep 2013, 04:07
Contact:

Re: Close a running program - doesn't work!

10 Dec 2014, 02:30

Albireo wrote:I get no error message when the EXE-file is created with "EnableUIAccess",
That should mean it was successful. You can verify by using a PE resource editor/viewer (such as Resource Hacker or PEBrowsePro) to check that the manifest resource (ID 24) contains the string uiAccess="true".
Does it make any difference where the program "EnableUIAccess" is installed?
For EnableUIAccess.ahk itself, No. For the executable it creates, Yes. The script warns you if you are saving the file in a non-trusted location. It must be in Program Files or Windows\System32.
When I run the created exe-file, a blue UAC-window (not yellow) is opened, before the program is running.
This shows that the executable has a valid digital signature. But why are you running it as admin? That makes EnableUIAccess redundant.
Is it possible to use "EnableUIAccess" on every EXE-file I want to disable the UAC-window?
(not only converted AHK-files?)
No. It might fail if the EXE does not have an XML manifest resource, or if it does not contain the expected XML tags.
I have a program that I have to run as Administrator (reads and writes in the windows registry)
If I want to start and control that program with AHK. Must both the "Exe-program" and "AHK-file" be modified with "EnableUIAccess".
You are mistaking the purpose of EnableUIAccess. It does just as it's name implies; enables AutoHotkey to access the UI of administrative programs - without running as admin (as stated in the forum topic). It does not enable AutoHotkey to run as admin without a UAC prompt.
Is there any difference between running a "EnableUIAccess" created file, or to use the "Task Scheduler" to launch programs without an UAC prompts?
It does not give the script admin privileges. Since the script is not running as admin, programs launched by the script do not automatically get admin privileges either. It doesn't bypass the UAC prompt; there's just no prompt, because it's not running as admin.
User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: Close a running program - doesn't work!

03 Dec 2016, 06:25

FWIW Process Close works if the invoked level of the AHK script is equal to or greater than the level of the process. If the level is less than that of the target process, the process is not closed but the function returns success.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AshuraIsHim, Google [Bot], GroggyOtter and 115 guests