Page 1 of 1

Return all users logged onto a computer

Posted: Sun Jan 28, 2007 8:35 pm
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

Re: Return all users logged onto a computer

Posted: Mon Jan 29, 2007 1:45 am
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?

Posted: Mon Jan 29, 2007 2:12 am
by roach
Click start > Log Off > Switch User.
- or -
Windows Key + L

You never log out.. you just switch which account is active.

Posted: Mon Jan 29, 2007 2:26 am
by Heathen
same roach from the gm forum? :)

Posted: Mon Jan 29, 2007 2:27 am
by roach
If you mean the GMC that is about to go to hell by merging with YoYo Games... yep.

Posted: Mon Jan 29, 2007 2:33 am
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.