Jump to content

Require Admin only when it is necessary


Go to solution Solved by KaFu,

Recommended Posts

Posted

hello,

Sometimes my script need to

#RequireAdmin

but not always.

so i want to do that the script will require admin when i call to some function or if some condition is exist.

Unfortunately it does not work.

in this test:

MsgBox(0,"","test")

Func test()
    #RequireAdmin
EndFunc

I expected that the script will not require admin because I didn't call to test().

but what actually happened is that the script requireed admin anyway..

Do you know a method to do what I'm trying to do?

  • Moderators
Posted

gil900,

Directive like #requireadmin cannot be conditional - AutoIt looks for them at runtime and actions any that exist before running the script. :(

The only way I know to work around this is to use a wrapper script that determines whether #requireadmin is needed and then calls the relevant version of the working script (with and one without the directive). :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

  On 10/25/2013 at 1:17 PM, Melba23 said:

gil900,

Directive like #requireadmin cannot be conditional - AutoIt looks for them at runtime and actions any that exist before running the script. :(

The only way I know to work around this is to use a wrapper script that determines whether #requireadmin is needed and then calls the relevant version of the working script (with and one without the directive). :)

M23

If it requires another exe file, so it's not an option for me ...

Anyone know another way?

  • Solution
  • Moderators
Posted

gil900,

 

  Quote

If it requires another exe file, so it's not an option for me

You can do it with .au3 or .a3x files - just FileInstall the one you need and get the original .exe to run it. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

  On 10/25/2013 at 1:24 PM, KaFu said:

Thank you.

this solution is almost perfect.

You said in this thread that you heard that someone's been working on a solution to do this in runtime.

Does anyone already done such a solution?

if not then i will use your example

thank you!

Posted

  On 10/25/2013 at 2:07 PM, Mrbenkali said:

 

Another solution   :bye: 

If Not _GetDebugPrivilegeRtl() Then
    #RequireAdmin
EndIf

Func _GetDebugPrivilegeRtl()
    Local $aRet = DllCall('Ntdll.dll', "long", "RtlAdjustPrivilege", "ulong", 20, "bool", True, "bool", False, "bool*", 0)
    Return $aRet[0] = 0
EndFunc   ;==>_GetDebugPrivilegeRtl

Have you checked what you gave?

It does not work and I think it also should not work ...

But thanks for trying to help

  • Moderators
Posted

Mrbenkali,

As I explained above, that code will NOT work as AutoIt looks for and actions all directives BEFORE running the script. So you CANNOT have conditional directives and need a workaround - either restarting the script (as Kafu has very cleverly done) or running different versions of a child script. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

If no one will bring a better solution in the next 5 hours, KaFu win the best answer vote (Mark Solved)

Thanks you for your help!

Posted (edited)

  On 10/25/2013 at 5:15 PM, DXRW4E said:
MsgBox(0,"","test")

If Not IsAdmin() Then _GetAdminRight()

MsgBox(0,"","test2")


Func _GetAdminRight($sCmdLineRaw = "")
    If Not IsAdmin() Then
        If Not $sCmdLineRaw Then $sCmdLineRaw = $CmdLineRaw
        ShellExecute(@AutoItExe, $sCmdLineRaw, "", "runas")
        ProcessClose(@AutoItPID)
        Exit
    EndIf
EndFunc

Ciao.

 

What you have done is a better packaging of the same solution.

I'm not looking better packaging ..

I was looking for a better solution.

But no matter .. I have been implementing the solution of KaFu anyway...

It seems to be good enough for me :)

Edited by Guest
Posted (edited)

  On 10/25/2013 at 5:53 PM, gil900 said:

What you have done is a better packaging of the same solution.

I'm not looking better packaging .

I was looking for a better solution.

I gave you just a alternative to what you asked (not logical and bad example) in the first post of the topic, nothing else OKKKKKK

Ciao.

Edited by DXRW4E

apps-odrive.pngdrive_app_badge.png box-logo.png new_logo.png MEGA_Logo.png

Posted

  On 10/25/2013 at 5:57 PM, DXRW4E said:

I gave you just a alternative to what you asked (not logical and bad example) in the first post of the topic, nothing else OKKKKKK

Ciao.

 
It's okay ..

Thank you for the help!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...