Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Build UAC aware apps with VS2008

Download

Right click “Save as…”

Author: Hi, I am Daniel Moth Smiley

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.

Tags:

Follow the Discussion

  • Waleed El-BadryVBCoder Waleed El-Badry
    In my humble opinion, I think Visual Basic Team gave me a big relief as the UAC issue raised with Vista made development really hard. Thanks again for overcoming this dilemma.
  • Thanks VBCoder. I agree, both the C# and VB teams have eased our lives with the feature described in the video Smiley
  • I rather thought that the manifest was just another resource -- in which case, why not edit it through the normal resource editing mechanisms instead of via Yet Another Weird Action?

    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 Smiley.

    rsclient wrote:
    I rather thought that the manifest was just another resource -- in which case, why not edit it through the normal resource editing mechanisms instead of via Yet Another Weird Action?
    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.


    rsclient wrote:
    And why is this so late?  Did the VS2005 team really not know that Vista was coming?
    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.


    rsclient wrote:
    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?
    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 Wink


    rsclient wrote:
    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.
    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

  • evildictaitorevildictait​or Devil's advocate
    rsclient wrote:
    It would be nice to know if an app can detect that it's elevated, or handle the user not running it. 


    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());
      }
    }
  • Thanks for sharing that evildictaitor!

    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
  • Brice ItBrice It Email Brice
    You showed us how to create an embedded manifest in VS 2005 but what about embedding one in VS 2005 Express Edition?

    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?
  • Hi Brice It, for VS2005 I did not use any template. It is a simple post-build command line after you hand craft the manifest. See my reply to rsclient for the relevant link. So, assuming you have mt.exe on your machine and that you create the right manifest file then you can embed a manifest.

    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.

  • 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.
  • 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

Comments Closed

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.