run exe as user (AHK is opened as Admin)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
xinxilas
Posts: 30
Joined: 18 Nov 2015, 16:54

run exe as user (AHK is opened as Admin)

Post by xinxilas » 28 May 2022, 21:24

hello friends!

is there anyway Run .exe as the user instead of administrator when i opened my AHK as admin?

Thank you very much!

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: run exe as user (AHK is opened as Admin)

Post by Rohwedder » 29 May 2022, 00:34

Hallo,
This is meant to be integrated into your main script.
Function: The main script is first started as User.
It deletes sub-script Run.ahk, rewrites it, integrates its own full path, starts it (hidden) as User and then makes itself Admin via restart.
Finally, when the main script runs as admin, its Run(target) function serves as an instruction to the UserScript Run.ahk to start the target.
To transfer the contents of the Target variable to Run.ahk, Run(Target) uses the clipboard.
With line: q::Run("Notepad.exe") Hotkey Q becomes the instruction: "Start Notepad.exe as User".
When the main script is closed, Run.ahk closes itself.

Code: Select all

#SingleInstance, Force
If !A_IsAdmin
{
    DetectHiddenWindows On
    WinGetTitle, Title, ahk_id %A_ScriptHwnd%
    FileDelete, Run.ahk
    FileAppend,
    (   LTrim
        #SingleInstance, Force
        #NoTrayIcon ; (hidden)
        DetectHiddenWindows On
        WinWaitClose,`% Title := "%Title%"
        WinWait,`% Title ;Admin-Script
        WinWaitClose
        ExitApp
        ^!#vkFF::Run,`% ClipBoard
    ), Run.ahk, UTF-8 ;User-Script
    Run, Run.ahk
    Run, *RunAs "%A_ScriptFullPath%",,, %ErrorLevel%
}
q::Run("Notepad.exe") ;Start Notepad.exe as User
Run(Target)
{
    ClipSaved := ClipBoardAll
    ClipBoard := Target
    Sleep, 100
    Send, ^!#{vkFF}
    Sleep, 100
    ClipBoard := ClipSaved
}

Post Reply

Return to “Ask for Help (v1)”