Is there an API call to prompt user for UAC elevation?

前端 未结 3 1634
猫巷女王i
猫巷女王i 2021-01-06 03:02

My app needs to write to a file in \\ProgramData that could be protected. This only happens once after installation.

Is there an API function that would take ACL inf

3条回答
  •  青春惊慌失措
    2021-01-06 03:25

    If you don't want to elevate your entire app, you have a few options:

    1. spawn a separate elevated process just to access the file. Use ShellExecute/Ex() with the runas verb, or CreateProcessElevated(), to run a second copy of your app, or another helper app, with command-line parameters to tell it what to do. The main process can wait for the second process to exit, if needed.

    2. create a COM object to access the file, and then use the COM Elevation Moniker to run the COM object in an elevated state.

    3. prompt the user for credentials using CredUIPromptForCredentials() or CredUIPromptForWindowsCredentials() (see Asking the User for Credentials for more details), then logon to the specified account using LogonUser() to get a token, impersonate that token using ImpersonateLoggedOnUser(), access the file as needed, and then stop impersonating using RevertToSelf() and close the token with CloseHandle().

提交回复
热议问题