Build UAC aware apps with VS2008
- Posted: Aug 16, 2007 at 6:22 PM
- 24,551 Views
- 11 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Author: Hi, I am Daniel Moth ![]()
Introduction: User Account Control is the top compatibility hurdle for some applications moving to Windows Vista. It is relatively easy to comply with some elements of UAC (e.g. embedding a manifest in your app) and with Visual Studio 2008 it is even
easier as I show in this video. Relevant blog posts of mine are
here and
here.
Video download: Click on the image to play the video (from a streaming file). If you'd prefer to download the wmv packaged in a zip file, you may do so
here.
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
And why is this so late? Did the VS2005 team really not know that Vista was coming?
And why does the "UAC Options" button in VB.NET mode actually create a file? Isn't that just about the last action that anyone would expect given the labeling on the button?
Lastly, this video is grossly mistitled. It's not about making UAC aware apps - the app knows nothing about UAC except for a "magic string" in the manifest. It would be nice to know if an app can detect that it's elevated, or handle the user not running it. But since these apps aren't actually UAC aware at all, that's not possible.
Hi rsclient
Thank you for the thoughtful comments and questions
.
Manifests are just another resource, but in the native sense. So if you are prepared to play with RC files then you can also embed the manifest. You can read that approach for VS2005 here.The VS2005 team (and probably the VS.NET 2003 and the VS.NET 2002 and the VS6 teams) knew that Vista was coming but it hadn't come yet when their product RTMd. As a reminder, VS2005 was released in November 2005 and Windows Vista in January 2007. That is why VS2005 *itself* is not UAC-aware whereas VS2008, as I demonstrate in the video, is. I also share in the video what you must do to embed manifests using VS2005.
Not sure what action most users would expect from that button. If you think it is bad UI design or if you prefer the C# approach, please feed that back directly to the product groups or raise it in the forums. I am just the messenger explaining how things work
This video does not cover *everything* there is about writing UAC-aware apps, but it does cover some hence the title (note the "with VS2008" in the title). Hopefully, by watching the video you learnt why it is important to embed a manifest and thus taking the first step to making your app UAC-aware (e.g. not taking advantage of virtualization as I demonstrate) and towards logo certification. I also show how to add the Shield icon to the button that performs admin functionality, hence making that bit UAC-aware. Finally, in the video at 13' I provide some overall generic advice for building UAC-aware apps. If you still feel the video is mistitled then apologies. To see how to determine if your app is elevated, Download my other UAC video from June 2006 here.
Thanks again for your feedback.
Cheers
Daniel
public static class UAC {
public static bool AmElevated(){
return new WindowsPrincipal(WindowsIdentity.GetCurrent())
.IsInRole(WindowsBuiltInRole.Administrator);
}
public static void Elevate(){
if(AmElevated()) return;
ShellExecute(IntPtr.Zero, "runas\0",
Application.ExecutablePath + "\0", "\0", "\0", 1);
}
}
...
public class Program {
public static void Main(){
if(!UAC.AmElevated()){
MessageBox.Show("I'm not elevated, asking you to elevate...");
UAC.Elevate();
}else{
MessageBox.Show("I'm elevated, yays!");
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
The checking of admin rights is essentially the same as what the video I referenced shows.
You do not need to use ShellExecute directly since it is possible with the managed Process class (set the Verb to "runas" - example here).
Cheers
Daniel
There is no manifest template there for that.
My guess is that we should just copy a manifest file from a sample VS 2008 app into a VS 2005 project file. Is this correct?
Since both are running on variations of the 2.0 framework it shouldn't be a problem. Is this correct or is there another more appropriate way in which to create an embedded manifest in VS 2005 Express Edition?
BTW, if you encoutner any issues with developing for Vista these forums are very useful.
Hi Daniel,
There is an issue with auto generating UAC manifest with Visual Studio 2008. It’s hard codes the version and name as follows:
<assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
This is reported to MS but the resolution was not clear:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290160
What "setting on the project" updates this version? Please let me know. I really need a dynamic way to updating the assemblyIdentity settings as manually updating it for every build is tedious.
Thanks for your assistance with this.
There is an issue with auto generating UAC manifest with Visual Studio 2008. It’s hard codes the version and name as follows:
<assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
This is reported to MS but the resolution was not clear:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=290160
What "setting on the project" updates this version? Please let me know. I really need a dynamic way to updating the assemblyIdentity settings as manually updating it for every build is tedious.
Thanks for your assistance with this.
supachannel, sorry I don't know. Please see the MSDN documentation:
http://msdn.microsoft.com/en-us/library/dd371711(VS.85).aspx">http://msdn.microsoft.com/en-us/library/dd371711(VS.85).aspx
..and failing that, please use the MSDN forums.
Cheers
Daniel
Remove this comment
Remove this thread
close