Run As Admin then I get error Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Rami
Posts: 55
Joined: 19 May 2016, 07:44

Run As Admin then I get error

15 Aug 2016, 07:49

Hi,

My script starts with:

Code: Select all

#Persistent
#SingleInstance, force
#NoEnv
#Include StdOutToVar.ahk

; *** Force to run as Administrator ***
if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"
   ExitApp
}
To insure that the script will run as admin. But what happen is when the elevator popup message appears and if I click NO the code gives the following error message:
Verb: <RunAs>
Params: <>
--------> Run *RunAs "%A_ScriptFullPath%"

Specifically: The operation was canceled by the user.
knowing that if I click YES everything goes well.
Any help what am I doing wrong? Or if this error is necessary after clicking NO, is there any way to pass that?

Thank you in advance.
User avatar
TheDewd
Posts: 1449
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Run As Admin then I get error  Topic is solved

15 Aug 2016, 15:35

See link: https://autohotkey.com/boards/viewtopic ... 6595#p6595

In short, UAC must be enabled at the time of your AutoHotkey installation, or else the *RunAs verb for .ahk files is not registered on your system.
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
Rami
Posts: 55
Joined: 19 May 2016, 07:44

Re: Run As Admin then I get error

15 Aug 2016, 15:43

Thank you @TheDewd, I will read and test the mentioned way in that link.
But i'm willing to send my script as EXE to friend who has no AHK installed, will it work on his computer?
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Run As Admin then I get error

15 Aug 2016, 20:18

It is normal for Run to raise an error if it was unable to run the program. Obviously it is unable to run the program if the user denies it access.

If you don't want to see an error dialog when Run fails, use a try statement or the UseErrorLevel option.

*RunAs does not need to be registered for .ahk files if you are running an .exe file.
Rami
Posts: 55
Joined: 19 May 2016, 07:44

Re: Run As Admin then I get error

16 Aug 2016, 07:04

Logically, I believe it should be IF Error = 1 Exitapp or something like that, but i'm only couple weeks in AHK :) so I would be thankful if you could provide me with small example of UseErrorLevel option to avoid seeing the error dialog when Run fails
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Run As Admin then I get error

16 Aug 2016, 07:37

Try this:

Code: Select all

#Persistent
#SingleInstance, force
#NoEnv
#Include StdOutToVar.ahk

; *** Force to run as Administrator ***
if not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%", , UseErrorLevel
    If A_LastError
        MsgBox, you did not allow the script to run!
    ExitApp
}
I hope that helps.
Rami
Posts: 55
Joined: 19 May 2016, 07:44

Re: Run As Admin then I get error

16 Aug 2016, 07:44

wolf_II wrote:Try this:

Code: Select all

#Persistent
#SingleInstance, force
#NoEnv
#Include StdOutToVar.ahk

; *** Force to run as Administrator ***
if not A_IsAdmin
{
    Run *RunAs "%A_ScriptFullPath%", , UseErrorLevel
    If A_LastError
        MsgBox, you did not allow the script to run!
    ExitApp
}
I hope that helps.
Actually, both, this code and your first code you wrote and deleted in 1 minute worked for me (I copied it, I was faster than you :) )

Code: Select all

if not A_IsAdmin
{
    try
    {
        Run *RunAs "%A_ScriptFullPath%"
        ExitApp
    }
    catch error
    {
        MsgBox, you did not allow the script to run!
    }
}
Thanks a lot for your help, i would mention that as the correct answer of my question, but seems i can't make 2 answers as a correct answers, or even cancel one and mentioned another...

Also, thank you @lexikos for the new information for me.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Run As Admin then I get error

16 Aug 2016, 07:50

I deleted the first answer, since it was not using UseErrorLevel, and that was requested, so I changed the script. :D
Rami
Posts: 55
Joined: 19 May 2016, 07:44

Re: Run As Admin then I get error

16 Aug 2016, 07:57

lol
Not only worked for me, Try and Catch both are new for me since I'm new here, so double thanks for you :)

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], effel, mikeyww, yagami19821223 and 51 guests