Forum > Windows
A button require administrator level
(1/1)
badmintonfan:
Is there any way that a button require administrator level to run some codes which is assigned instead of the main program
the attached picture as a sample
Fibonacci:
To add a shield icon to the button
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SendMessage(Button1.Handle, $160C{BCM_SETSHIELD}, 0, 1{1 add shield, 0 remove});
Then spawn a new process using ShellExecuteEx. Pass SHELLEXECUTEINFO struct with lpVerb set to "runas". Only new process can be elevated.
furious programming:
This is quite simple to do, but as @Fibonacci wrote, you cannot raise privileges for a current process — you have to start a new one with administrator rights. You can run another program, that is, another executable file, prepared specifically to perform this special task, but you can also launch a second instance of the same application and provide it with arguments that will allow it to perform this special task, return operation code (process exit code) and exit immediately.
In my editors, I added the ability to edit the contents of the registry. The editor itself does not require elevated privileges to run, but modifying the HKEY_LOCAL_MACHINE branch in the registry does. So when the user chooses, for example, the option to register file extensions so that files with these extensions are run by default in my editor, the editor starts itself (with appropriate arguments), detects these arguments, modifies the registry, sets the exit code and closes — skips form creation and normal Application handling. The main instance reads the exit code and thus knows whether the operation was successful, and if not, it displays an appropriate error message based on this code.
The code that opens a new instance with elevated privileges and waits for the task to be executed looks like this (shortened):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Windows, ShellAPI; function TEditorUtilsInstances.OpenRegister(const AParameters: String): Integer;var ShellInfo: SHELLEXECUTEINFO; Filename: String;begin // Prepare path for the application to run (in this case, the second instance of the editor). Filename := FontEditor.Utils.Instances.Location; Filename += ExtractFileName(Application.ExeName); ShellInfo := Default(SHELLEXECUTEINFO); ShellInfo.cbSize := SizeOf(ShellInfo); ShellInfo.lpVerb := 'runas'; // Force displays a UAC dialog for confirmation. ShellInfo.lpFile := PChar(Filename); // Application path to run. ShellInfo.lpParameters := PChar(AParameters); // Arguments that allow the application to detect that it has been launched only to perform a special task. ShellInfo.fMask := SEE_MASK_NOCLOSEPROCESS; // Wait until the process finishes. ShellInfo.nShow := SW_SHOW; // Display UAC window and wait for confirmation. if ShellExecuteExA(@ShellInfo) then begin // The user has accepted the UAC dialog, so wait for the second instance to complete. WaitForSingleObject(ShellInfo.hProcess, INFINITE); // Read the process exit code containing the result of the task execution (should never fail). if not GetExitCodeProcess(ShellInfo.hProcess, LongWord(Result)) then Result := EDITOR_ERROR_REGISTER_SUCCESS; // Close the process handle. CloseHandle(ShellInfo.hProcess); end else // The user cancelled the UAC dialog (pressed "no" or close button), so do nothing. Result := EDITOR_ERROR_REGISTER_CANCELLED;end;
The code of the main editor module looks like this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---begin case Editor.FontEditor.Utils.Instances.OpenedReason() of EDITOR_OPEN_REASON_LOCATION_REGISTER: ExitCode := Editor.FontEditor.Utils.Register.LocationRegister(); EDITOR_OPEN_REASON_LOCATION_UNREGISTER: ExitCode := Editor.FontEditor.Utils.Register.LocationUnregister(); EDITOR_OPEN_REASON_ASSOCIATION_REGISTER: ExitCode := Editor.FontEditor.Utils.Register.AssociationRegister(); EDITOR_OPEN_REASON_ASSOCIATION_UNREGISTER: ExitCode := Editor.FontEditor.Utils.Register.AssociationUnegister(); otherwise Editor.FontEditor.Utils.Instances.Open(); endend.
First, the reason for launching the application is checked based on run arguments:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TEditorUtilsInstances.OpenedReason(): Integer;begin Result := EDITOR_OPEN_REASON_NORMAL; case Application.ParamCount of 1: case Application.Params[1] of EDITOR_PARAMETER_LOCATION_REGISTER: Result := EDITOR_OPEN_REASON_LOCATION_REGISTER; EDITOR_PARAMETER_LOCATION_UNREGISTER: Result := EDITOR_OPEN_REASON_LOCATION_UNREGISTER; EDITOR_PARAMETER_ASSOCIATION_REGISTER: Result := EDITOR_OPEN_REASON_ASSOCIATION_REGISTER; EDITOR_PARAMETER_ASSOCIATION_UNREGISTER: Result := EDITOR_OPEN_REASON_ASSOCIATION_UNREGISTER; otherwise Result := EDITOR_OPEN_REASON_EDIT; end; 2: case Application.Params[1] of EDITOR_PARAMETER_OPEN_CLONED: Result := EDITOR_OPEN_REASON_CLONE; end; end;end;
If the argument is one of those specifying a special task (e.g. EDITOR_OPEN_REASON_ASSOCIATION_REGISTER, i.e. registering file extensions in the registry), the editor performs its task, sets the exit code and closes. However, if it was started normally, it creates a form and runs normally:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TEditorUtilsInstances.Open();begin RequireDerivedFormResource := True; Application.Title := 'Project: Kids — Font Editor'; Application.Scaled := True; Application.Initialize(); Application.CreateForm(TFormMain, FormMain); Application.Run();end;
badmintonfan:
thanks every one
KodeZwerg:
Thats how I do if neeeded.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure RunUnelevated(const FileName: string);var ProcessInfo: TProcessInformation; StartupInfo: TStartupInfo;begin FillChar(StartupInfo, SizeOf(TStartupInfo), 0); StartupInfo.cb := SizeOf(TStartupInfo); if CreateProcess(nil, PChar('explorer.exe "' + FileName + '"'), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo) then begin CloseHandle(ProcessInfo.hProcess); CloseHandle(ProcessInfo.hThread); end;end; function RunElevated(const FileName: WideString; const Params: WideString): Boolean;var ShellExecuteInfo: TShellExecuteInfoW;begin Result := False; FillChar(ShellExecuteInfo, SizeOf(ShellExecuteInfo), 0); ShellExecuteInfo.cbSize := SizeOf(ShellExecuteInfo); ShellExecuteInfo.fMask := SEE_MASK_NOCLOSEPROCESS; ShellExecuteInfo.Wnd := GetActiveWindow; // or 0 if you don't have a window handle ShellExecuteInfo.lpVerb := 'runas'; // Request UAC elevation ShellExecuteInfo.lpFile := PWideChar(FileName); ShellExecuteInfo.lpParameters := PWideChar(Params); ShellExecuteInfo.nShow := SW_NORMAL; if ShellExecuteExW(@ShellExecuteInfo) then begin WaitForSingleObject(ShellExecuteInfo.hProcess, INFINITE); Result := True; end;end;
Navigation
[0] Message Index