Jump to content

run autoit function as non-elevated

Recommended Posts



i have a script that has several functions that require elevation.  however there are a few that will only work if not elevated.  i know i can create a separate script to run those functions with runas.  is there another way to run those functions non elevated within the elevated script?

would it be possible with something along the lines of 

RunWait(@AutoItExe & ' /AutoIt3ExecuteLine ... )

thanks in advance

Share this post

Link to post
Share on other sites

Hello Yes you can.


#RequireAdmin ; for this example to have sense

#include <MsgBoxConstants.au3>
#include <ProcessConstants.au3>
#include <Security.au3>
#include <SecurityConstants.au3>
#include <StructureConstants.au3>
#include <WinAPI.au3>

Local $sCommand = _
        'Local ' & _
        '$Dummy1 = MsgBox(0,"Am I Admin?", "IsAdmin: " & IsAdmin())'

$sCommand = '"' & StringReplace($sCommand, '"', '""') & '"'
ConsoleWrite($sCommand & @CRLF)
;~ Run(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)

_RunNonElevatedAndWait(@AutoItExe & ' /AutoIt3ExecuteLine ' & $sCommand)

Func _RunNonElevatedAndWait($sCommandLine = "")
    If Not IsAdmin() Then Return Run($sCommandLine) ; if current process is run non-elevated then just Run new one.

    ; Structures needed for creating process
    Local $tSTARTUPINFO = DllStructCreate($tagSTARTUPINFO)

    ; Process handle of some process that's run non-elevated. For example "Explorer"
    Local $hProcess = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, 0, ProcessExists("explorer.exe"))

    ; If successful
    If $hProcess Then
        ; Token...
        Local $hTokOriginal = _Security__OpenProcessToken($hProcess, $TOKEN_ALL_ACCESS)
        ; Process handle is no longer needed. Close it
        ; If successful
        If $hTokOriginal Then
            ; Duplicate the original token
            Local $hTokDuplicate = _Security__DuplicateTokenEx($hTokOriginal, $TOKEN_ALL_ACCESS, $SECURITYIMPERSONATION, $TOKENPRIMARY)
            ; Close the original token
            ; If successful
            If $hTokDuplicate Then
                ; Create process with this new token
                _Security__CreateProcessWithToken($hTokDuplicate, 0, $sCommandLine, 0, @ScriptDir, $tSTARTUPINFO, $tPROCESS_INFORMATION)

                ; Close that token
                ; Close get handles
                _WinAPI_CloseHandle(DllStructGetData($tPROCESS_INFORMATION, "hProcess"))
                _WinAPI_CloseHandle(DllStructGetData($tPROCESS_INFORMATION, "hThread"))
                ; Return PID of newly created process
                Return ProcessWaitClose(DllStructGetData($tPROCESS_INFORMATION, "ProcessID"))
EndFunc   ;==>_RunNonElevated



  • Like 2

Share this post

Link to post
Share on other sites

That's for an exe though mostly right?

I wanted to run autoit functions like ie_create and inet_get

Don't think that's possible with that script?  


Share this post

Link to post
Share on other sites

Do a try ;-)



Share this post

Link to post
Share on other sites

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


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.