IsAdministrator()

Share your advanced PureBasic knowledge/code with the community.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

I saw this previously and thought this was for win2K and XP only so i ignored it. Seems to work here OK on Vista Ultimate. But....wichtel's latest code is smaller and works also, so i'm going to use that one. Thank you :)
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

Well PB's IsUserAdmin_() seems to work as well on 2000/XP/Vista, but I can't find it documented anywhere except for WINE.
I'll stay with my my code for now, but it is really amazing that a simply question "do I have admin privs" could lead to so many answers.

Thanks everybody.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

You can also use this:

Code: Select all

Procedure IsAdmin()
  a=OpenSCManager_(0,0,#SC_MANAGER_ALL_ACCESS)
  If a : CloseServiceHandle_(a) : EndIf
  ProcedureReturn a
EndProcedure
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

@PB:

Hey, hey, that is what I posted first in that thread. It worked but I always thought doing that way is "not professional"

But you are right that works for NT/2000/XP/VISTA.
And it has the advantage that you could poll remote machines.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> that is what I posted first in that thread

:oops: I need to open my eyes more. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

PB wrote:You can also use this:

Code: Select all

Procedure IsAdmin()
  a=OpenSCManager_(0,0,#SC_MANAGER_ALL_ACCESS)
  If a : CloseServiceHandle_(a) : EndIf
  ProcedureReturn a
EndProcedure
what exactly is this supposed to return if the user is admin? It returns a random 7 digit numer for me if the user is admin...so if it returns anything except 0 its working?

The others return either 1 or 0.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@SFSxOI: Sorry, it's 0 if not admin, or non-0 if it is. Or it can use 1/0 like this:

Code: Select all

Procedure IsAdmin()
  a=OpenSCManager_(0,0,#SC_MANAGER_ALL_ACCESS)
  If a : CloseServiceHandle_(a) : a=1 : EndIf
  ProcedureReturn a
EndProcedure
It was originally coded like opening a window: 0 if failed, or non-0 for success.
Most PureBasic and API commands work that way, so I just did the same. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

@PB; thats what I thought but wanted to make sure. Thanks :)
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

OpenSCManager_ returns a 0 if access fails.
If access is granted, it returns the handle to the SCManager.
You could then use functions like openService_ or controlService_ or startService_ to work with services.

If the first paremeter of SCManager is a pointer to a string, you can connect to SCManagers of a remote PC.

All explained in the good old win32.hlp file.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
Post Reply