views:

276

answers:

2

I have what the UAC Development Guides refer to as a "Administrative Choice Application." If you're familiar with what this is skip to the next section.

Background:

I want to let a "Standard" user have the ability to select/deselect a Run On Startup option in the preferences for my application.

Since my application is per machine (not per user), what needs to happen is it will either need to Delete or Copy a shortcut file into the Start Menu/Programs/Startup folder which will require administrative access to perform this operation.

So, what i'd like is for the "User Account Control credential prompt" to appear and that way if the user has an admin account also they can put in the credentials. This is apparently how applications are supposed to be designed to keep the user from having to switch to another account each time they need to do something administrative.

Excerpt from MSDN documentation:

An Administrative Choice Application

An Elevated Process or COM Object

The initial application launches without requiring elevation. Those items in the user interface that would require an administrative access token are decorated with a shield icon as identification. This decoration indicates to the user that using that feature will require administrator approval. When the application detects that one of these buttons has been selected, it has the following two choices.

  • The application launches a second program using ShellExecute() to perform the administrative task. This second program would be marked with a requestedExecutionLevel of requireAdministrator, thus causing the user to be prompted for approval. This second program would be running with a full administrative access token and would be able to perform the desired task. -OR-
  • The application launches a COM object using CreateElevatedComObject(). This API would launch the COM object with a full administrative access token following approval and this COM object would be able to perform the desired task.

I just need to copy a file... seems excessive to fork a new process using ShellExecute() and I don't know enough about COM to know if I could use it to copy a file. I am hoping someone can post some code which provides a way to copy the file and ideally also explain how to decorate a MenuItem with the "sheild decorator".

Notes:

I have looked at the UAC Demo provided by microsoft which is referenced in several StackOverflow posts such as (http://stackoverflow.com/questions/17533/request-vista-uac-elevation-if-path-is-protected) on topics related to permissions. The code only has an example of the calling a separate process.

+2  A: 

Though it still appears to involve at least restarting or spawning a process, you can find some help here: UAC Shield for Elevation at CodeProject.com

Scott Jackson
It's a start. Thanks!
blak3r
A: 

I ended up going in a different direction. I had my installer create a startup shortcut in the All Users/Startup folder that passes an argument to the application "startup".

When the application started I would check for the existence of the arg[0].equals("startup") and then check if Settings1.Default.RunOnStartup == true.

If both conditions were true I'd exit the application immediately. When the application is started without that argument (ie the Start Menu Program Group), the application loaded normally.

The RunOnStartup setting is a user scoped setting so each user can change without effecting others.

blak3r