April 19, 2024, 04:00:52 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Understanding the windows registry - Running your program as Admin

Started by Andy, October 18, 2016, 06:22:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

There will be times you need to make sure your program is run as Admin.

Here is a way you can do this with the registry.

My "usersdrives.exe" and "usersdrives2.exe" programs need to be run as Admin in order to load another user's registry settings ("Hive"), but asking users to right click and select "Run as Administrator" is not the ideal solution.

So how do we avoid this......

This is how:

In this "RunMyProgAsAdmin" example first change the STRING value of "MyProgramsName" to a program of your choice - it must contain the full path to the exe file, i.e. C:\\MyFolder\\MyProgram.exe.

Now compile and run RunMyProgAsAdmin.

It will save your programs name and path in the registry location:
HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

You will only need to do this once for each exe file.

The windows Desktop will restart, after that, when you run your stated program, it will run as admin.

You could use this in a little setup program for users. 

For all users, you will need to change HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE (and I think a computer restart would be best in this case).

For those of you that are worried about programs running as admin, you can always make sure that user account controls are on.

You can always use regedit.exe afterwards and remove the entry for your program at the same location:

HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers

Attached is the code.

Thanks,
Andy.
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.