Jump to content
Sign in to follow this  
JLanning

Running Elevated Privileges 

Recommended Posts

I'm looking to run this with elevated privileges, there are items that aren't showing up for me when I run this from a compiled exe, compared to running it from a command prompt.  Any help would be great!!.

#include <AutoItConstants.au3>
#RequireAdmin

Func DiskCleanup($Drive = "C")
    ShellExecute("cleanmgr.exe", "/sageset:12","", "runas")
EndFunc

 

Share this post


Link to post
Share on other sites

Generally because of file system redirection, the following may work under 32-bit environment:

#RequireAdmin
#include <AutoItConstants.au3>

Func DiskCleanup($Drive = "C")
    DllCall("kernel32.dll", "boolean", "Wow64EnableWow64FsRedirection", "boolean", 0) ;~ Disable 64 Bit Redirection
        Run("cleanmgr.exe /sageset:12")
    DllCall("kernel32.dll", "boolean", "Wow64EnableWow64FsRedirection", "boolean", 1) ;~ Enable 64 Bit Redirection
EndFunc

More info here: https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_Wow64EnableWow64FsRedirection.htm

Edited by Subz

Share this post


Link to post
Share on other sites

@Subz

BOOL Wow64DisableWow64FsRedirection(
  PVOID *OldValue
);

I assume you intended to "DllCall" Wow64EnableWow64FsRedirection as your help link points to it.  Your posted "DllCall" is very wrong.

Share this post


Link to post
Share on other sites

@Tekk Thanks for pointing that out, you're correct, I've used that call for years (from within the forums) but never actually bothered to check it it was correct or not.  It should be using int but buggered if I know how to get the old value, from what I've read this can be null or 0 for example:

DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 0) ;~ Disable 64 Bit Redirection
    RunWait("notepad.exe")
DllCall("kernel32.dll", "int", "Wow64RevertWow64FsRedirection", "int", 0) ;~ Enable 64 Bit Redirection
    RunWait("notepad.exe")

 

Share this post


Link to post
Share on other sites

Subz, in your first example you're confusing Wow64EnableWow64FsRedirection with Wow64DisableWow64FsRedirection, the former uses boolean, the latter uses PVOID (void pointer). You'd want to use the former with True/False to enable/disable the redirection.

Your second example above is using INT when the function is asking for a PVOID, and should be the value obtained from the first function call, if I'm reading the documentation right.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

It should be more like ..

Global $g_aResult = DllCall("kernel32", "BOOL", "Wow64DisableWow64FsRedirection", "PTR*", 0)

If True = $g_aResult[0] Then

;~  do file stuff here.

    DllCall("kernel32", "BOOLEAN", "Wow64RevertWow64FsRedirection", "BOOLEAN", $g_aResult[1])
EndIf

Wow64DisableWow64FsRedirection fails for me when I try to call it with an invalid pointer value.

You can see an example of its use  here.

#include <WinAPIFiles.au3>
_WinAPI_Wow64EnableWow64FsRedirection ( $bEnable )

The above is a wrapper for Wow64EnableWow64FsRedirection which is a much more simple approach.  However documentation states "This function may not work reliably when there are nested calls."

I just wanted to bring attention to your posted code failing, at least on my system.  I don't mean to derail the thread.

Share this post


Link to post
Share on other sites

Thanks @Tekk & @BrewManNH, for some reason there are a number of variations in the forums, which all appear to work,, @Tekk, using your code I updated my personal UDF, thought I'd share it.

; #FUNCTION# ====================================================================================================================
; Name...........: _DisableWow64FsRedirection
; Description ...: Specifies whether to enable or disable the Wow 64 system folder redirection
; Syntax.........: _DisableWow64FsRedirection([$_bDisable = True])
; Parameters ....: $_bDisable - True  : Disable Wow64 system folder redirection (default)
;                             - False : Re-enable Wow64 System folder redirection
; Return values .: Success    - 1 and @error = 0
;                :            - @extended 1 = Disabled Wow64 system folder redirection
;                :            - @extended 2 = Re-enabled Wow64 system folder redirection
;                  Failure    - 0 and @error = 1
;                :            - @extended 1 = OS 32-bit
;                :            - @extended 2 = Script compiled as 64-bit
; Author ........: Subz
; Modified.......:
; Remarks .......:Thanks Tekk
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _DisableWow64FsRedirection($_bDisable = True)
    If @OSArch = "x64" And @AutoItX64 = 0 Then
        Local $tOldValue = DllStructCreate("dword")
        If $_bDisable = True Then
            DllCall("kernel32.dll", "BOOL", "Wow64DisableWow64FsRedirection", "ptr", DllStructGetPtr($tOldValue))
            Return SetError(0, 1, 1)
        Else
            DllCall("kernel32.dll", "BOOL", "Wow64RevertWow64FsRedirection", "ptr", DllStructGetPtr($tOldValue))
            Return SetError(0, 2, 1)
        EndIf
    EndIf
    Local $iError = @OSArch = "x86" ? 1 : 0
        $iError += @AutoItX64 = True ? 2 : 0
    Return SetError(1, $iError, 0)
EndFunc

 

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...