Solved

How to elevate rights programmatically for the application in Windows 7

Posted on 2010-08-18
6
3,666 Views
Last Modified: 2012-05-11
Hi,

Our C++ application (native, unmanaged code) checks whether some essential DLLs are registered.  If not, it tries to register them.  In other words, the automatic registration must be done when the application is executed for the first time.  Second and next times this is not to be repeated.

The application is installed by copying to certain directory (no installer yet).

The situation is complicated on Windows Vista or Windows 7 because even the administrator must explicitly elevate the rights.  Currently, he or she can do it during launching the application via right-click on the app icon and choosing the "Run as administrator".  However, it is not convenient and we have to explain it again and again to the users who install the application.

My question is: Can the application elevate the right automatically when it decides so?  I understand that it should not be done silently -- the UAC warning is OK in this case, no problem.

Thanks,
    Petr
0
Comment
Question by:pepr
6 Comments
 
LVL 2

Accepted Solution

by:
jayaram13 earned 200 total points
ID: 33463773
0
 
LVL 6

Assisted Solution

by:ChristianWimmer
ChristianWimmer earned 200 total points
ID: 33463775
Easy.

1. User runs your app.
2. Test whether your app is running as admin,
 a. if yes, register your dlls and go to 4.
 b. if not continue to 3.
3. Call ShellExecuteEx with parameter verb = runas and call you app a second time with some parameter that says register dlls or similar. Your first process must wait (WaitForSingleObject) on the process handle returned by ShellExecuteEx.
Check the return value of ShellExecuteEx (also see msdn docs) and also if your DLL registration was successfull, then proceed with 4.
4. continue to run

This also works on XP btw. In this way you'll get a logon dialog where an admin can input his credentials.
0
 
LVL 8

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 100 total points
ID: 33471336
Take look at ImpersonateLoggedOnUser() in MSDN

U also can use a manifest file to do the same.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Author Comment

by:pepr
ID: 33482296
Thanks for your suggestions.  To clarify the things, the application should be installed and first-time executed by a local administrator.  This way, the impersonation does not seem to be neccessary.  Only the elevation of the existing rights is the goal.

If I understand it well, the elevation can be done only when the process starts.  No way to elevate the rigts temporarily for the running process.  This way it seems that to do the registration of DLLs or other actions that require elevated righs, the separate, helper application could be created for the purpose.  The helper application would be executed (with elevated rights) from inside the main application and it will wait until the helper quits.  After that, it can continue.

Is it reasonable?  
0
 
LVL 6

Assisted Solution

by:ChristianWimmer
ChristianWimmer earned 200 total points
ID: 33482818
Yes, this is the suggested way by MS.

Elevation means to run a new process using a different token with admin rights. Setting this token while running is only possible with a process that runs with admin rights (e.g. service) or calls LogonUser.

BTW: A process cannot be "inside" another one. Instead a process can be a child process.
0
 
LVL 28

Author Closing Comment

by:pepr
ID: 33482888
Thanks all for the help. ;)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I hope this helps those who have been battling the SanDisk / U3 problem for a while. For anyone that is running Windows 7 64bit and is receiving and searching the internet for the “Windows Error: Windows has allocated a drive letter to the U3 dri…
OfficeMate Freezes on login or does not load after login credentials are input.
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now