[SOLVED] Making sure a program is [x] run as administrator

Just starting out? Need help? Post your questions and find answers here.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

[SOLVED] Making sure a program is [x] run as administrator

Post by StarWarsFan »

For some testing, I am trying to make sure the user runs my program as administrator/with admin privileges

I am using this procedure that I got from somewhere here on the PB-forum

Code: Select all

Procedure IsUserAnAdministrator()
  Protected IsMember, *AdministratorsGroup
  Structure NtAuthority
    NtAuthority.b[6]
  EndStructure
  Define SECURITY_NT_AUTHORITY.NtAuthority
  If OpenLibrary(advapi32, "advapi32.dll")
    CheckTokenMembership = GetFunction(advapi32, "CheckTokenMembership")
    If CheckTokenMembership
      AllocateAndInitializeSid = GetFunction(advapi32, "AllocateAndInitializeSid")
      If AllocateAndInitializeSid
        FreeSid = GetFunction(advapi32, "FreeSid")
        If FreeSid
          SECURITY_NT_AUTHORITY\NtAuthority[5]=5
          ; The AllocateAndInitializeSid function allocates and initializes a security identifier (SID) with up to eight subauthorities.
          If AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, #SECURITY_BUILTIN_DOMAIN_RID, #DOMAIN_ALIAS_RID_ADMINS, #Null, #Null, #Null, #Null, #Null, #Null, @*AdministratorsGroup)
            CheckTokenMembership(#Null, *AdministratorsGroup, @IsMember)
          EndIf
          FreeSid(*AdministratorsGroup)
        EndIf
      EndIf
    EndIf
    CloseLibrary(advapi32)
  EndIf
  ProcedureReturn IsMember
EndProcedure

If IsUserAnAdministrator()= #False : MessageRequester("!","no Admin privileges")
EndIf
but when I compile my program and right-click that .exe to check the box with run as admin, it still is not.

Apparently I am doing something wrong. But what?
Last edited by StarWarsFan on Wed Sep 15, 2021 11:53 am, edited 1 time in total.
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Making sure a program is [x] run as administrator

Post by Paul »

Did you read through the info that came with the posted code?
viewtopic.php?f=12&t=40492
Image Image
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Making sure a program is [x] run as administrator

Post by BarryG »

I've been using this since forever with no problems:

Code: Select all

Global admin=IsUserAdmin_()
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Making sure a program is [x] run as administrator

Post by StarWarsFan »

BarryG wrote: Mon Sep 13, 2021 9:51 pm I've been using this since forever with no problems:

Code: Select all

Global admin=IsUserAdmin_()
BARRY! THANK YOU! AGAIN YOU PROVE TO BE ONE OF THE HELPFUL PERSONS HERE! CHEERS!
I did not know there is such a short and easy way.

And Paui: If you do not want to help, just do not post.
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Making sure a program is [x] run as administrator

Post by Paul »

StarWarsFan wrote: Wed Sep 15, 2021 11:46 am And Paui: If you do not want to help, just do not post.
Not sure what you mean?
The code you posted above did not compile. The code by Rescator listed at the link I provided worked fine on my system so I'm not sure how that would not be helpful.

I cannot find docs on WinAPI IsUserAdmin_() but I can find WinAPI IsUserAnAdmin_() so probably the same thing just not documented.

Microsoft states IsUserAnAdmin is just a wrapper for CheckTokenMembership (which is what the Procedure Rescator posted in the link is using).
Microsoft recommends to call CheckTokenMembership directly to determine Administrator group status rather than calling IsUserAnAdmin because this command may be altered or unavailable in subsequent versions of Windows.

Something to think about :wink:
Image Image
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: [SOLVED] Making sure a program is [x] run as administrator

Post by Mijikai »

If you only want to know if your program runs evelated a call to GetTokenInformation_() does the job just fine.
No need to do the member stuff.
Post Reply