Detecting if Administrator

Two questions, one minor, one major.

Minor: Why is it that even if a User has admin rights, they still have to explicitly run an app as Administrator?

Major: How can I detect programmatically that a user is running with Administrator rights? I mean, not just his Account-dictated rights, but those that come from launching the app as Run as administrator… (see minor question).

I still use the method written by Aaron Ballman from his blog:

Ah, actually, I use an amended method that contains this bit at the top.

if System.IsFunctionAvailable( "IsUserAnAdmin", "shell32" ) then
  declare function IsUserAnAdmin Lib "shell32" ()  as Boolean
  Return IsUserAnAdmin
end if

Edit to add the link to the documentation page for this function:


Note that you can also set your project builds to request elevation to Administrator privileges:

  1. Select Windows under Build Settings in the navigator
  2. Click the gear icon in the Inspector
  3. Change the Privileges drop-down to Administrator

1 Like

Thanks Anthony! Two questions: Doesn’t your ammendum apply all the time? When would that function ever not exist? It says in the spec that it goes back to XP. Also, the Advanced Build thing - doesn’t that apply to just Debug builds? Or is that just my confusion?

Just a sanity check.

Also applies to builds. There is no exclusionary statement in the documentation here or here, and I’ve been using this successfully since it was added.

So really you took this 100 line code snippet and pared it down to 3? Impressive! (I’m not talking about it replacing Aaron’s code, but again I can’t see your found-Shell function ever NOT existing. Except in based OS installs. But that doesn’t happen anymore really.) It’d be, in your words, "in"sane. =)

I still use the combination of both checks. I think there was some reason, but I’ve not needed to review that code in a very long time since it just works now.

When was that?
These advanced settings had been announced?

The elevating UAC page of the documentation was added on October 4, 2018.

I think there was an accompanying blog post.

Ah, ok. I rarely read blog posts, as it’s not in my “workflow”. But I agree they are useful at times.
Thank you.

Using IsFunctionAvailable() is only useful with soft declares. This sanity check will never be reached on a system where it would fail because the app has already crashed.

1 Like

We also have an IsWindowsAdminUserMBS function MBS Xojo Plugins. Maybe it helps also.

1 Like

Truth. Pays to look at old code.

Old code is better then no code for helping others :grin:


Just for completeness, here’s the proper block:

if System.IsFunctionAvailable( "IsUserAnAdmin", "shell32" ) then
  soft declare function IsUserAnAdmin Lib "shell32" ()  as Boolean
  Return IsUserAnAdmin
end if

@Garth_Hjelte Please be sure to pick a solution so that it’s easier for others who come across this in the future.