{"id":198,"date":"2007-04-22T15:53:09","date_gmt":"2007-04-22T14:53:09","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?p=198"},"modified":"2007-04-22T15:53:09","modified_gmt":"2007-04-22T14:53:09","slug":"c-code-to-detect-uac-elevation-on-vista","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/198-c-code-to-detect-uac-elevation-on-vista.html","title":{"rendered":"C# code to detect UAC elevation on Vista"},"content":{"rendered":"<p>A couple of days ago I posted about <a href=\"http:\/\/www.itwriting.com\/blog\/?p=196\" target=\"_blank\">programmatically detecting whether UAC is enabled<\/a>. I was proposing to read a registry entry. Thanks to the power of blogs, my post drew a comment from Andrei Belogortseff, who <a href=\"http:\/\/www.tweak-uac.com\/programming\/vista-tools\/\">has posted C++ code<\/a> that does this properly. His library has functions for detecting whether the current process is elevated, detecting whether UAC is enabled, and running new processes both elevated and non-elevated (the last of these is the hardest to do).<\/p>\n<p>I liked the libary&nbsp;but wanted to use&nbsp;it from .NET. I compiled his code to a DLL and did a C# wrapper; then I considered the&nbsp;hassles of adding a dependency on the Visual C++ runtime libraries. I therefore did a quick C# implementation of the functions I cared about; I haven&#8217;t included the code for running new processes.&nbsp;Please go&nbsp;ahead and <a href=\"http:\/\/www.itwriting.com\/uacelevationcheck.zip\">download the code<\/a>, and let me know if you&nbsp;have any suggestions, or even if there is a much easier way to do this that I have missed.&nbsp;There is an app called UACElevationCheck, which calls functions in VistaTools.cs.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.itwriting.com\/images\/uacelevation.gif\"> <\/p>\n<p>The code is only intended to be called from Vista and will throw an exception otherwise; of course you can modify it as you like.<\/p>\n<p>I&#8217;ve included a function IsReallyVista that looks for a Vista-only API call, rather than relying on the reported version information. This is because the version information is unreliable if the app runs in a compatibility mode. On the other hand, IsReallyVista is a hack; if you don&#8217;t like it, use IsVista which uses the version information instead.<\/p>\n<p>You may be able to do this using the System.Security namespace rather than PInvoke. I had a quick look but the PInvoke code seemed easier to me, especially since Belogortseff has already done the real work.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"wlWriterSmartContent\" id=\"0767317B-992E-4b12-91E0-4F059A8CECA8:0f2151d5-167e-417c-a709-0ba362948d56\" contenteditable=\"false\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; 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\/elevation\" rel=\"tag\">elevation<\/a>, <a href=\"http:\/\/technorati.com\/tags\/c#\" rel=\"tag\">c#<\/a>, <a href=\"http:\/\/technorati.com\/tags\/.NET\" rel=\"tag\">.NET<\/a>, <a href=\"http:\/\/technorati.com\/tags\/c%20sharp\" rel=\"tag\">c sharp<\/a>, <a href=\"http:\/\/technorati.com\/tags\/dotnet\" rel=\"tag\">dotnet<\/a>, <a href=\"http:\/\/technorati.com\/tags\/vista\" rel=\"tag\">vista<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A couple of days ago I posted about programmatically detecting whether UAC is enabled. I was proposing to read a registry entry. Thanks to the power of blogs, my post drew a comment from Andrei Belogortseff, who has posted C++ code that does this properly. His library has functions for detecting whether the current process &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/198-c-code-to-detect-uac-elevation-on-vista.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C# code to detect UAC elevation on Vista<\/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-198","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\/198","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=198"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/198\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}