IsUserAnAdmin - IsWow64Process

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

IsUserAnAdmin - IsWow64Process

Post by RASHAD »

Check If You are logged in As Admin Or Not

Code: Select all

If OpenLibrary(0,"shell32.dll")
  *MAlloc = GetFunction(0, "IsUserAnAdmin")
  If CallCFunctionFast(*MAlloc) = 1
    MessageRequester("Info","User is Admin", #MB_ICONINFORMATION)
  Else
    MessageRequester("Info","User is not Admin",#MB_ICONINFORMATION)
  EndIf
CloseLibrary(0)
EndIf

Check If Your program is running under x64 Windows OS Or Not
I think It is much better than kernel32.dll (IsWow64Process) function
Please check And confirm

Code: Select all

lRetVal.i
sRemMachName.s
lTopLevelKey.i
lHKeyhandle.i
sKeyName.s
lhkey.i
sValueName.s
vValue.s
msg.s ;

#ERROR_NONE = 0

Procedure.l QueryValueEx(lhkey.i, szValueName.s)
  Define.i cch, lrc, lType, lValue
  Define.s sValue
  Shared vValue
  cch    = 255
  sValue = Space(255)
  lrc    = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue, @cch)
  If lrc = #ERROR_NONE
    vValue = Left(sValue, cch - 1)
  Else
    vValue = "Empty"
  EndIf
  ProcedureReturn lrc
EndProcedure

lTopLevelKey = #HKEY_LOCAL_MACHINE 
sRemMachName = ""
sKeyName = "Software\Microsoft\Windows\CurrentVersion"
sValueName = "CommonW6432Dir"
lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0,#KEY_READ, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  MessageRequester("Information","Running System is 64 bit", #MB_ICONINFORMATION)
Else
  MessageRequester("Information","Running System is 32 bit", #MB_ICONINFORMATION)
EndIf
Please confirm
Have a good day
Egypt my love
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsUserAnAdmin - IsWow64Process

Post by SFSxOI »

How about the PureBasic function:

Code: Select all

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
But yours seems to work fine, thank you very much. :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: IsUserAnAdmin - IsWow64Process

Post by RASHAD »

Hi SFSxOI
Why do't you post your feedback as soon as you can?
More tech. more fun for the guys
Glad to talk to you
Happy new year man

SFSxOI your name makes me mad so difficult to remember
Egypt my love
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: IsUserAnAdmin - IsWow64Process

Post by rsts »

SFSxOI wrote:How about the PureBasic function:

Code: Select all

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
But yours seems to work fine, thank you very much. :)
Rashad's seems to work fine :D , but this returns s86 when, in fact I'm running 64 bit. The PB install is 32 bit however.

cheers
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: IsUserAnAdmin - IsWow64Process

Post by RASHAD »

rsts Happy new year you and North America
I wish 2010 will be much better for all of you
Now rsts give me in detail PB ver ,the running OS and the output to see what we can do
Egypt my love
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsUserAnAdmin - IsWow64Process

Post by SFSxOI »

rsts wrote:
SFSxOI wrote:How about the PureBasic function:

Code: Select all

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
But yours seems to work fine, thank you very much. :)
Rashad's seems to work fine :D , but this returns s86 when, in fact I'm running 64 bit. The PB install is 32 bit however.

cheers
"GetEnvironmentVariable" returns properly here for x86/32 bit and x64/64 bit - several machines checked - all with Windows 7 Ultimate, intel chipsets and processors. Are you using an AMD based system or 32 bit processor?
Last edited by SFSxOI on Thu Dec 10, 2009 12:50 pm, edited 1 time in total.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsUserAnAdmin - IsWow64Process

Post by SFSxOI »

RASHAD wrote:Hi SFSxOI
Why do't you post your feedback as soon as you can?
More tech. more fun for the guys
Glad to talk to you
Happy new year man

SFSxOI your name makes me mad so difficult to remember
RASHAD, checked it all out and everything works fine here on Windows 7 Ultimate. Tried on several machines and everything works properly. The only possible issue i can see with it is permissions dealing with accessing the registry for users that might have less then admin or super-user permissions.

(My name makes you mad? LoL :), first time i ever had anyone say that. Actually it's a mixture of a contraction of an office title and call sign from a previous career of 26 years in the military from which i have since retired.)

Merry Christmas and a Happy new year to you too.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: IsUserAnAdmin - IsWow64Process

Post by rsts »

Hi,

Mr RASHAD's routine correctly returns 64 bit.

Debug GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"), returns s86.

I'm running 64 bit Windows 7 on AMD AthlonII X2 245 64 bit dual core processor.

cheers
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: IsUserAnAdmin - IsWow64Process

Post by doctorized »

I tried the code under Win XP x64 SP2 with both PB 4.31 x64 and x86.
In both cases returns: "Running System is 32 bit".

I am using a better code, found in this forum:

Code: Select all

Import ""
  GetNativeSystemInfo(*info)
EndImport

Procedure IsWin64(); is the OS 64;
  Protected Info.SYSTEM_INFO
  GetNativeSystemInfo(Info)
  If info\wProcessorArchitecture
    ProcedureReturn #True
  EndIf
EndProcedure 

Debug IsWin64()
If returned values is 0 then we have x86 OS, else we have x64 OS.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: IsUserAnAdmin - IsWow64Process

Post by RASHAD »

Hi doctorized
Sorry I do't have XP 64 bit for test
So please test the next snippet it is much shorter and clear (the SYSTEM_INFO structure is about( The Processor
Architecture not the running OS )

Code: Select all

#KEY_WOW64_64KEY=$100

KeyName.s = "Software\Wow6432Node" 
KeyHandle.l = 0 
If RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, @KeyName, 0,#KEY_READ|#KEY_WOW64_64KEY, @KeyHandle) = #ERROR_SUCCESS 
MessageRequester("Information","Running System is 64 bit", #MB_ICONINFORMATION)
Else
MessageRequester("Information","Running System is 32 bit", #MB_ICONINFORMATION)
EndIf
Happy new year
Egypt my love
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: IsUserAnAdmin - IsWow64Process

Post by doctorized »

Yes. This code returns "Running System is 64 bit".

Happy new year? From now? 10th of December? 21 days earlier?
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsUserAnAdmin - IsWow64Process

Post by SFSxOI »

This is what i've been using for a while to determine if a user is an admin or not:

Code: Select all

Procedure IsUserAdmin()
  isadmin = OpenSCManager_(#Null,#Null,#SC_MANAGER_ALL_ACCESS) 
  If isadmin 
    CloseServiceHandle_(isadmin) 
  EndIf 
  If isadmin > 0 : isadmin = #True : Else : isadmin = #False : EndIf
  ProcedureReturn isadmin 
EndProcedure

If IsUserAdmin() = #True
  MessageRequester("Information","Is an Admin")
Else
  MessageRequester("Information","Is not an Admin")
EndIf

The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: IsUserAnAdmin - IsWow64Process

Post by ts-soft »

SFSxOI wrote:This is what i've been using for a while to determine if a user is an admin or not:

Code: Select all

Procedure IsUserAdmin()
  isadmin = OpenSCManager_(#Null,#Null,#SC_MANAGER_ALL_ACCESS) 
  If isadmin 
    CloseServiceHandle_(isadmin) 
  EndIf 
  If isadmin > 0 : isadmin = #True : Else : isadmin = #False : EndIf
  ProcedureReturn isadmin 
EndProcedure

If IsUserAdmin() = #True
  MessageRequester("Information","Is an Admin")
Else
  MessageRequester("Information","Is not an Admin")
EndIf

Why not simple:

Code: Select all

If IsUserAnAdmin_() = #True
  MessageRequester("Information","Is an Admin")
Else
  MessageRequester("Information","Is not an Admin")
EndIf
?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: IsUserAnAdmin - IsWow64Process

Post by RASHAD »

I thought it is very simple topic but you made it somthing else
So my new year strted allready
SFSxOI,rsts,doctorized and Tomas
Happy new year boys from now on till 15 of the next Jan I will keep saying it to the forum members
Egypt my love
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: IsUserAnAdmin - IsWow64Process

Post by SFSxOI »

ts-soft wrote:
SFSxOI wrote:This is what i've been using for a while to determine if a user is an admin or not:

Code: Select all

Procedure IsUserAdmin()
  isadmin = OpenSCManager_(#Null,#Null,#SC_MANAGER_ALL_ACCESS) 
  If isadmin 
    CloseServiceHandle_(isadmin) 
  EndIf 
  If isadmin > 0 : isadmin = #True : Else : isadmin = #False : EndIf
  ProcedureReturn isadmin 
EndProcedure

If IsUserAdmin() = #True
  MessageRequester("Information","Is an Admin")
Else
  MessageRequester("Information","Is not an Admin")
EndIf

Why not simple:

Code: Select all

If IsUserAnAdmin_() = #True
  MessageRequester("Information","Is an Admin")
Else
  MessageRequester("Information","Is not an Admin")
EndIf
?
Well, yeah your right. :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Post Reply