Home > OS >  Run C program as admin first time
Run C program as admin first time

Time:04-21

I made a program that has to run as admin just to create one registry key. After my program created the registry key I create another key in the registry to run my program on startup, I don’t need admin rights for this. So I changed the project properties so that if the user wants to run my program he/she has to run it as admin. After the creation of my first key(where I need admin rights), I add my program to startup using the registry.

The problem is that we can’t run the program with admin rights on startup. I don’t even need admin rights after the first execution because the neccesary key is created.

My question: How can I make my program so that it only asks for admin rights on the first execution?

CodePudding user response:

Have in memory two processes of the same program, and maybe have them communicate with a pipe. The user starts the program normally without elevated privileges. Once the elevation is required you start the same program with ShellExecuteEx and verb "runas". This "runas" verb will cause UAC prompt.

If you need elevated privileges only once for the whole duration of the original process, then you can pass some parameters to the elevated process, and be done with it. But if you need elevated process to perform several actions, at different times, then you have to have a pipe open between two processes, to send data to the elevated process. This way you have UAC prompt only once, but do stuff that need elevation any time you need.

  • Related