Windows administrative privilege lazarus / 21-General ---------------------------------------------------------------------- bigeno · 2023-10-10 10:21 Hi, is there any way to RELOAD application with administrative privilege ? my application works ok with standard user, but I've one functionality that requires admin. Is there any way to increase privileges just before windows api call ? ---------------------------------------------------------------------- Fibonacci · 2023-10-10 10:29 AFAIK no, you need to restart your app You can do this powershell start notepad -verb runas Or call ShellExecuteEx with lpVerb = "runas" https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns- shellapi-shellexecuteinfoa Quoterunas: Launches an application as Administrator. User Account Control (UAC) will prompt the user for consent to run the application elevated or enter the credentials of an administrator account used to run the application. ---------------------------------------------------------------------- Zvoni · 2023-10-10 10:32 Quote from: bigeno on October 10, 2023, 12:21:03 pmHi, is there any way to RELOAD application with administrative privilege ? my application works ok with standard user, but I've one functionality that requires admin. Is there any way to increase privileges just before windows api call ? https://stackoverflow.com/questions/16835673/handling-uac- administrative-token ---------------------------------------------------------------------- TRon · 2023-10-10 10:34 Also no need for Remy to repeat himself: https://stackoverflow.com/questions/31844584/is-there-an-api-call-to- prompt-user-for-uac-elevation?rq=3 ---------------------------------------------------------------------- Thaddy · 2023-10-10 14:07 This needs no code, just a properly formatted resource, a manifest. We have written many examples over the years on this forum. It seems a recurring issue, but that is really all. A manifest resource is written in XML. You do not even have to include it: during debugging it is enough to put the resource, with the same name as your executable, but with the extension manifest in the same directory. In general you would include it in the final build as a res file. Just search the forum. The link given by TRon is helpful, but not necessary at all. The correct way is a manifest resource. ---------------------------------------------------------------------- marcov · 2023-10-10 14:24 Quote from: Zvoni on October 10, 2023, 12:32:11 pmQuote from: bigeno on October 10, 2023, 12:21:03 pmHi, is there any way to RELOAD application with administrative privilege ? my application works ok with standard user, but I've one functionality that requires admin. Is there any way to increase privileges just before windows api call ? https://stackoverflow.com/questions/16835673/handling-uac- administrative-token There is a "IsWindowsAdmin" in  unit winutils. It is somewhat different from the version there. (Maybe works around issues with older windows versions) ---------------------------------------------------------------------- Zvoni · 2023-10-10 14:47 Quote from: Thaddy on October 10, 2023, 04:07:56 pmThis needs no code, just a properly formatted resource, a manifest. We have written many examples over the years on this forum. It seems a recurring issue, but that is really all. A manifest resource is written in XML. You do not even have to include it: during debugging it is enough to put the resource, with the same name as your executable, but with the extension manifest in the same directory. In general you would include it in the final build as a res file. Just search the forum. The link given by TRon is helpful, but not necessary at all. The correct way is a manifest resource. Wouldn't that mean, it ALWAYS runs elevated? ---------------------------------------------------------------------- Thaddy · 2023-10-10 15:20 Yes, of course. But it can be mitigated by using highestAvailable instead of requireAdministrator, in most cases this is the same and shows a dialog if not already logged in as admin. You also have autoElevate, but for that you need to pay... (or have an msdn subscription, for which you also pay - more -) and is not recommended, unless you absolutely definitely want what OP proposed in his question. ---------------------------------------------------------------------- Remy Lebeau · 2023-10-10 20:47 Quote from: TRon on October 10, 2023, 12:34:39 pmAlso no need for Remy to repeat himself: https://stackoverflow.com/questions/31844584/is- there-an-api-call-to-prompt-user-for-uac-elevation?rq=3 Quote from: Thaddy on October 10, 2023, 04:07:56 pmThis needs no code, just a properly formatted resource, a manifest. That is overkill when the majority of the app is designed to run without elevated privileges.  Better to split out the elevated tasks into a separate process or COM object and then elevate only that when needed.  Yes, it's more work, but it's safer and offers a better admin experience. ---------------------------------------------------------------------- 440bx · 2023-10-10 20:56 Quote from: Remy Lebeau on October 10, 2023, 10:47:11 pmBetter to split out the elevated tasks into a separate process or COM object and then elevate only that when needed.  Yes, it's more work, but it's safer and offers a better admin experience. By any chance, would you happen to have (or know of) an example of how it is done using a COM object ?  Preferably something simple that is specifically to illustrate how it's done using a COM object. ---------------------------------------------------------------------- KodeZwerg · 2023-10-10 21:25 What I am doing since years, and work very well for my usage is the unit RunElevatedSupport.pas. Do not ask me if it is compatible with Lazarus/FreePascal, I only used it in combination with Delphi 32/64 bitness targets. ---------------------------------------------------------------------- 440bx · 2023-10-10 21:42 Thank you... I'll check it out. ---------------------------------------------------------------------- Remy Lebeau · 2023-10-11 17:18 Quote from: 440bx on October 10, 2023, 10:56:44 pmBy any chance, would you happen to have (or know of) an example of how it is done using a COM object ?  Preferably something simple that is specifically to illustrate how it's done using a COM object. Not a specific example, no.  But you would simply implement a COM object normally, and then use the COM Elevation Moniker when you need to create an instance of the object. ---------------------------------------------------------------------- 440bx · 2023-10-11 17:53 Thank you Remy