Force user to run application with privileges

0

I need to create a script in VBS that forces the user to click CONTINUE when Administrator permission is requested. If he clicks on CANCEL, the same window asking for the privileges will return his screen (as if it were a LOOP) until he click CONTINUE, what I have so far is just that:

If WScript.Arguments.length =0 Then
  Set objShell = CreateObject("Shell.Application")
  'Pass a bogus argument with leading blank space, say [ uac]
  objShell.ShellExecute "wscript.exe", Chr(34) & _
  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
  'Add your code here
  WScript.Echo "Hello World"
End If
    
asked by anonymous 20.09.2015 / 00:58

2 answers

0

I found this code in bat, it might be useful:

@echo off  


REM --> Checar permissão 
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"  

REM --> Checar erro

if '%errorlevel%' NEQ '0' (   
goto UACPrompt
) else ( goto gotAdmin )  

:UACPrompt  
 echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"  
 set params = %*:"=""  
 echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"  

 "%temp%\getadmin.vbs"  
 del "%temp%\getadmin.vbs"  
 exit /B  

:gotAdmin  
  pushd "%CD%"  
  CD /D "%~dp0"
  echo Administrador consedido!
  PAUSE

This code checks for permission and if there is no request. When it consedido it pauses the shell, of adapt with vbs to ask until it does not give the permission.

    
27.09.2015 / 00:17
-1

This would only be possible through another application, ie the application that will have to run as an administrator, you would need to run it through this other application. You would have to do a routine with a "Loop" to check if the other application was executed or not. If you make the other application (the call) in Delphi, the code would look like this:

//...
function processExists(exeFileName: string): Boolean;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  Result := False;
  while Integer(ContinueLoop) <> 0 do
  begin
    if pos(UpperCase(exeFileName),UpperCase(FProcessEntry32.szExeFile)) > 0 then
      exeFileName := FProcessEntry32.szExeFile;

    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then
      Result := True;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

//...
function CreateProcessSimple(cmd : string) : boolean;
var
suinfo: tstartupinfo;
procinfo: tprocessinformation;
begin
  fillchar(suinfo, sizeof(suinfo),#0);
  suinfo.cb := sizeof(suinfo);
  suinfo.dwflags := startf_useshowwindow;
  suinfo.wshowwindow := SW_HIDE;
  result := createprocess(nil,
    pchar(cmd),
    nil,
    nil,
    false,
    CREATE_NEW_CONSOLE or
    NORMAL_PRIORITY_CLASS,
    nil,
    nil,
    suinfo,
    procinfo);
  if (result) then
  begin
    waitforsingleobject(procinfo.hprocess, INFINITE);
    CloseHandle(procinfo.hprocess);
    CloseHandle(procinfo.hthread);
  end;
end;
//...

procedure BitBtn1Click(Sender: TObject);
begin
  while not ProcessExists('AplicacaoAdmin.exe') do
    CreateProcessSimple('AplicacaoAdmin.exe');
end;

Work your code as needed within the Loop so there is no crash.

    
03.10.2015 / 03:28