Return all users logged onto a computer

Share your advanced PureBasic knowledge/code with the community.
roach
New User
New User
Posts: 3
Joined: Sun Jan 28, 2007 7:57 pm
Location: USA

Return all users logged onto a computer

Post by roach »

Code updated for 5.20+

In an attempt to return a list of all the user accounts located on a computer, this was one of the results. Instead of listing all of the accounts, it will list which users are currently logged onto the computer. Don't know if this would ever be useful, but I thought I would share. The user account that is running this script must be an administrator. The script automatically checks for that.

I'm not sure what versions of Windows this will work for, but it works on Windows XP Home Edition.

Code: Select all

;-------------------------------------------------------------
; Find users who are logged on
; Author: roach
; Date: 1/28/07
;-------------------------------------------------------------

;The two registry functions (ListSubKey and Reg_GetValue) were not written by me.
;If you know who wrote those functions, please inform me.
Procedure.s ListSubKey(topKey.l, sKeyName.s, Index.l, ComputerName.s)
  GetHandle.l
  hKey.l
  dwIndex.l
  lpName.s
  lpcbName.l
  lpftLastWriteTime.FILETIME
  i.l
  lReturnCode.l
  lhRemoteRegistry.l
  ListSubKey.s
  
  If Left(sKeyName, 1) = "\"
    sKeyName = Right(sKeyName, Len(sKeyName) - 1)
  EndIf
  
  If ComputerName = ""
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
  Else
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
  EndIf
  
  If GetHandle = #ERROR_SUCCESS
    lpcbName = 255
    lpName = Space(255)
    
    GetHandle = RegEnumKeyEx_(hKey, Index, @lpName, @lpcbName, 0, 0, 0, @lpftLastWriteTime)
    
    If GetHandle = #ERROR_SUCCESS
      ListSubKey = Left(lpName, lpcbName)
    Else
      ListSubKey = ""
    EndIf
  EndIf
  RegCloseKey_(hKey)
  ProcedureReturn ListSubKey
EndProcedure

Procedure.s Reg_GetValue(topKey, sKeyName.s, sValueName.s, ComputerName.s)
  lpData.s
  GetValue.s =""
  
  If Left(sKeyName, 1) = "\"
    sKeyName = Right(sKeyName, Len(sKeyName) - 1)
  EndIf
  
  If ComputerName = ""
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
  Else
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
  EndIf
  
  If GetHandle = #ERROR_SUCCESS
    lpcbData = 255
    lpData = Space(255)
    
    GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData)
    
    If GetHandle = #ERROR_SUCCESS
      Select lType
        Case #REG_SZ
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData)
          
          If GetHandle = 0
            GetValue = Left(lpData, lpcbData - 1)
          Else
            GetValue = ""
          EndIf
          
        Case #REG_DWORD
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lpType, @lpDataDWORD, @lpcbData)
          
          If GetHandle = 0
            GetValue = Str(lpDataDWORD)
          Else
            GetValue = "0"
          EndIf
          
      EndSelect
    EndIf
  EndIf
  RegCloseKey_(hKey)
  ProcedureReturn GetValue
EndProcedure

win = OpenWindow(0,50,50,200,100,"Users Logged On")
ListViewGadget(0, 5, 5, 190, 90)

admin = OpenSCManager_(#Null,#Null,#SC_MANAGER_ALL_ACCESS)
If admin > 0
  key.s = "filler"
  cnt.l = 0
  pos.l = 0
  
  prof_dir_raw.s = Space(255)
  size.l = 255
  GetProfilesDirectory_(@prof_dir_raw,@size)
  chars = Len(prof_dir_raw)
  prof_dir.s = Right(prof_dir_raw,chars - 3)
  
  While key <> ""
    check = 0
    
    key = ListSubKey(#HKEY_USERS,"",cnt,"")
    cnt = cnt + 1
    
    If Len(key) < 19
      check = 1
    EndIf
    
    pos = FindString(key,"Classes",0)
    If pos > 0
      check = 1
    EndIf
    
    If check = 0
      data_raw_1.s = Reg_GetValue(#HKEY_USERS,key+"\Volatile Environment","HOMEPATH","")
      data_raw_2.s = ReplaceString(data_raw_1,prof_dir,"")
      data_raw_3.s = ReplaceString(data_raw_2,"\","")
      user.s = data_raw_3
      
      ;--------[SHOW THE USERS WHO ARE LOGGED ON]-----------------
      AddGadgetItem(0,-1,user)
      
    EndIf
  Wend
Else
  Debug "You must be an administrator to view!"
EndIf


Repeat
  EventID = WaitWindowEvent()
  If EventID = #PB_Event_CloseWindow
    Quit = 1
  EndIf
Until Quit = 1
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Return all users logged onto a computer

Post by PB »

> Instead of listing all of the accounts, it will list which users are currently
> logged onto the computer

How can more than one person be logged on at once?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
roach
New User
New User
Posts: 3
Joined: Sun Jan 28, 2007 7:57 pm
Location: USA

Post by roach »

Click start > Log Off > Switch User.
- or -
Windows Key + L

You never log out.. you just switch which account is active.
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

same roach from the gm forum? :)
I love Purebasic.
roach
New User
New User
Posts: 3
Joined: Sun Jan 28, 2007 7:57 pm
Location: USA

Post by roach »

If you mean the GMC that is about to go to hell by merging with YoYo Games... yep.
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

roach wrote:If you mean the GMC that is about to go to hell by merging with YoYo Games... yep.
The gmc went to hell a LONG time ago.. same questions asked everyday.. Cheap single function winapi wrappers spammed in the "extending gamemaker" section (you should know about that), millions of pokemon games.. etc etc

Btw, I think yoyo games will actually benefit gamaker in the end. Mark can only take so much crap by himself.
I love Purebasic.
Post Reply