Recent

Author Topic: How to prompt for administrator account to modify registry without restarting?  (Read 3151 times)

FangQ

  • Full Member
  • ***
  • Posts: 134
On windows, I need users to change a registry setting to run the program properly.

https://github.com/fangq/mcx/blob/etherdome/mcxstudio/mcxgui.pas#L1351-L1376

However, if user starts the program in a regular account, it will get an error saying the registry editing fails. The only way I can do it correctly is to right-click on the program and start with admin account.

I am wondering if there is a way I can prompt a user to type in admin account and then modify without restarting the program?

thanks

PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
No, there is not, as you can't elevate the existing user context of a process.
What you can do however is to start your application in a "execute only this function" mode using a parameter and then start that when the user presses the button. This has the disadvantage that this will happen each time the user executes such an action compared to the behaviour if the whole application is restarted (thus the users might be confused with that behaviour).

ASerge

  • Hero Member
  • *****
  • Posts: 2242
And another addition... maybe better to think how to work in the program WITHOUT elevated privileges?

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
I am wondering if there is a way I can prompt a user to type in admin account and then modify without restarting the program?


Have you seen this?


https://docs.microsoft.com/en-us/previous-versions/bb756929(v=msdn.10)
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
On windows, I need users to change a registry setting to run the program properly.

https://github.com/fangq/mcx/blob/etherdome/mcxstudio/mcxgui.pas#L1351-L1376

That section of code modifies HKEY_CURRENT_USER, which does not need admin rights.  And the section has commented out logic that accesses HKEY_LOCAL_MACHINE with read-only access, which also does not need admin rights.

So, what exactly is failing?  Are you referring to the TfmMCX.UpdateGPUList() method, which does attempt to modify HKEY_LOCAL_MACHINE (after asking the user)?  THAT would need admin rights, yes.

I am wondering if there is a way I can prompt a user to type in admin account and then modify without restarting the program?

You cannot elevate a process once it has started running.  Your options in this situation are to either:

1. give your app a UAC manifest that sets the "requestedExecutionLevel" to "requireAdministrator".  This will make your entire app run elevated, which is undesirable unless most of its functionality requires admin rights in general.

2. Move the Registry-modifying code into a separate standalone process that you can run when needed using ShellExecute/Ex() with the 'runas' verb (or CreateProcessElevated()).  The code can remain in your same EXE if you give your app an optional command-line parameter that it looks for at startup to know whether to execute the Registry-modifying code or your normal app code.

3. Move the Registry-modifying code into a COM object that you can instantiate when needed using the COM Elevation Moniker.
« Last Edit: March 01, 2019, 08:26:04 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018