Rob
Rob

Reputation: 78758

Forcing my MFC app to run as Administrator on Vista

I have an MFC app built using Visual Studio 2008 and it needs to run on W2K, XP, 2003 and Vista. The application writes to HKLM in the registry and will only work on Vista if you run it as Administrator.

My question is: can I force the app to run as Adminstrator automatically? Does it involve creating a manifest file? At the moment I have the following in stdafx.h which I guess creates a manifest file on the fly:

#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")

Can I modify this line to force the elevation or do I need to do something with the VC project manifest settings?

Thanks in advance.

Upvotes: 3

Views: 8200

Answers (4)

sergiol
sergiol

Reputation: 4335

  1. Select the project in question, go to menu Project > Properties.

  2. Navigate to Configuration Properties > Linker > Manifest File.

  3. In the UAC Execution Level entry, select the option

requireAdministrator (/level='requireAdministrator')

enter image description here

Upvotes: 0

Rob
Rob

Reputation: 78758

I found out how to do this using some advanced C++ linker options:

  1. Open the project's Property Pages dialog box.

  2. Expand the Configuration Properties node.

  3. Expand the Linker node.

  4. Select the Manifest File property page.

  5. Modify the Enable User Account Control (UAC), UAC Execution Level, and UAC Bypass UI Protection properties.

Upvotes: 10

Nick
Nick

Reputation: 6846

You can do either. You can add a similar line with a #pragma to add the requireAdministrator directive, or you can set it in the property sheets in VS2008.

Upvotes: 0

demoncodemonkey
demoncodemonkey

Reputation: 11957

You can do this using the manifest. Possibly something like this but I can't recall exactly:

<requestedExecutionLevel level="requireAdministrator"/>

Upvotes: 1

Related Questions