1

I am very new to programming. I want to run a program with C++ which will prompt administrative permission to the user in windows. Is there any way to run another bunch of code if the permission is not granted??

system("setup.exe");//This will prompt administrative permission to the user. If permission is denied then I want to execute some other codes, for example, printif("Give administrative permission next time");

Anurag Dhadse
  • 515
  • 4
  • 16
  • If your program is not run elevated, you can attempt to run it elevated. If that fails, you can perform whatever fallback routine you have in mind. – cdhowie Jul 12 '20 at 05:31
  • This question has already been answered before. Click on this link :- https://stackoverflow.com/questions/6418791/requesting-administrator-privileges-at-run-time – sonulohani Jul 12 '20 at 05:35

2 Answers2

0

It is impossible to do for the already executing process. You can only start one more process (even if this is the same application). Another approach is to add appropriate application manifest which would force the runtime system to request the user for UAC elevation confirmation from the very beginning.

For the first approach, please see: CreateProcessAsUser function (Windows).

For the second (manifest) approach, please see, for example, this answer: C++ — How to run application with Admin privileges using Manifest file in Visual Studio 2005?.

Note that it is impossible to elevate the privileges without explicit user's consent. If it was possible, it would defeat the purpose of UAC. Please see: User Account Control — Wikipedia, the free encyclopedia.

sonulohani
  • 886
  • 6
  • 8
0

As I told I am very new to programming. So U was trying different codes without knowing what actually they are supposed to do. So finally a simple code servrd my purpose though I dont know what actually happening there. The code is

if(system("setup.exe")){printf("Give administrative permission next time");}