Open as an administrator only the first time in Delphi.

Asked

Viewed 588 times

3

I have put a method in my system to change the registry by making it launch together with windows, for this it is necessary that the executable runs in administrator mode. The problem is that every time the system starts, the application opens and shows the message asking if the user wants to start as administrator, which seems to me something boring.

The question I ask is: Is there a way to open the application in administrator mode only the first time? or maybe check if there is registry in windows and if yes run in normal mode?

  • http://chapmanworld.com/2015/06/08/elevated-privileges-for-delphi-applications/

  • 3

    That kind of change isn’t usually seen very well. I suggest you check on your system if the application was opened as an administrator, and if it was not, display a message warning the user that a particular function will not be executed because of the privileges, and request that next time you run the application as an administrator.

  • 1

    That’s what I did, Victor. I don’t think you can do it any other way.

  • You can make the application try to re-run with the administrator profile, and if the user accepts, close the "non-administrative' version. Once the application is configured as you want, simply do not call this upgrade function. It is an elegant solution as it will only "bother" the user with the UAC until it accepts (and in the case of a refusal, may offer an option not to insist, or to remember again in N days)

  • And in that case, you do not put the option in the manifest. You have to run in Runtime.

3 answers

2

Unable to open the privilege application only the first time if it already loads the manifest to open high. Also not interesting you start the high application and then run it without privileges.

Maybe I should do the following:

  1. Configure in the installation of your application.
  2. During the execution validate whether the application is on startup and, if not, notify the user. Then run another process/application to make this configuration.
  • 1

    Best answer! Just to complement, you can also use the HKEY_CURRENT_USER Software Microsoft Windows Currentversion Run key instead of HKEY_LOCAL_MACHINE, so you don’t need high privileges. The only detail is that it will be valid only for the user account where the program is running. Good luck!

2

As the use will be occasional, you can implement a C function to call by Delphi only when it needs lifting:

#include "windows.h"

SHELLEXECUTEINFO lpExecInfo;
memset(&lpExecInfo, 0, sizeof(SHELLEXECUTEINFO));

lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
lpExecInfo.lpFile = // aqui vai o nome do executável (no caso, o próprio aplicativo);
lpExecInfo.lpDirectory = // aqui vai o caminho no qual ele deve ser executado;
lpExecInfo.lpVerb = "runas";
lpExecInfo.lpParameters = "runasadmin";
lpExecInfo.nShow = SW_NORMAL;
lpExecInfo.fMask = 0;
lpExecInfo.hwnd = NULL;

ShellExecuteEx(&lpExecInfo); // retorna verdadeiro se o usuário aceitou

In this case, you do not specify in the manifest that you need administrative privileges, and only call the above function in case you need to do something like admin.

If the result of ShellExecuteEx for true, you can terminate the current instance because the user has accepted the UAC and is already running an admin version in parallel.

1

In project options/Application in the Manifest File section, check the option:

Enable Administrator Privileges to resolve.

  • 1

    He wants to open high only on the first run. This will not solve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.