{"id":196,"date":"2007-04-20T12:11:33","date_gmt":"2007-04-20T11:11:33","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?p=196"},"modified":"2007-04-20T12:11:33","modified_gmt":"2007-04-20T11:11:33","slug":"programmatically-detecting-whether-uac-is-enabled","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/196-programmatically-detecting-whether-uac-is-enabled.html","title":{"rendered":"Programmatically detecting whether UAC is enabled"},"content":{"rendered":"<p>An application may sometimes need&nbsp;to know whether or not the current&nbsp;user has administrative rights. This is complicated in Windows Vista by User Account Control (UAC) &#8211; the user may be in the Adminstrators group on the local machine, but nevertheless running with limited rights. When I came across <a href=\"http:\/\/chrison.net\/UACElevationInManagedCodeGuidanceForImplementingCOMElevation.aspx\" target=\"_blank\">this blog entry on COM elevation<\/a> by Christoph Wille(thanks to <a href=\"http:\/\/feeds.feedburner.com\/~r\/DanielMoth\/~3\/110421458\/uac-elevation-with-com.html\" target=\"_blank\">Daniel Moth<\/a>) I was interested&nbsp;to see&nbsp;the function he mentions called IsUACEnabledOS. I downloaded his code, but was disappointed to see this:<\/p>\n<blockquote>\n<p>\/\/ a really simple check that does not account for possible UAC-disabledness via group policy<br \/>public static bool IsUACEnabledOS()<br \/>{<br \/>int majorVersion = Environment.OSVersion.Version.Major;<br \/>int minorVersion = Environment.OSVersion.Version.Minor; <\/p>\n<p>return (majorVersion &gt;= 6);<br \/>}<\/p>\n<\/blockquote>\n<p>This just detects Vista or higher; in fact, it won&#8217;t always return the correct result, since Vista will lie about the version number if an application is&nbsp;running under compatibility settings. How then do we discover if UAC is enabled? The best I&#8217;ve come across so far is to query this registry entry:  <\/p>\n<p>HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System\\EnableLUA  <\/p>\n<p>If the entry does not exist or is 0, UAC is not enabled. Of course there is no guarantee that this always work, but it&#8217;s unlikely to change. I also haven&#8217;t checked that it is there in the base versions of Vista; again, it is probably the same.  <\/p>\n<p><strong>Update<\/strong>: See the comments to this post for a better solution from Andrei Belogortseff,&nbsp;using the <b>GetTokenInformation<\/b> API&nbsp;call.&nbsp;&nbsp;<\/p>\n<p>I&#8217;ve now posted a <a href=\"http:\/\/www.itwriting.com\/blog\/?p=198\" target=\"_blank\">C# implementation<\/a> of Andrei&#8217;s code.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:0bbe2b0a-f280-476c-9d1c-c7bedb197576\" contenteditable=\"false\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/uac\" rel=\"tag\">uac<\/a>, <a href=\"http:\/\/technorati.com\/tags\/vista\" rel=\"tag\">vista<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An application may sometimes need&nbsp;to know whether or not the current&nbsp;user has administrative rights. This is complicated in Windows Vista by User Account Control (UAC) &#8211; the user may be in the Adminstrators group on the local machine, but nevertheless running with limited rights. When I came across this blog entry on COM elevation by &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/196-programmatically-detecting-whether-uac-is-enabled.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Programmatically detecting whether UAC is enabled<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80,97],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-software-development","category-windows"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}