Windows: how to detect if other users logged in

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
omar
Posts: 545
Joined: 22 Oct 2015, 17:56

Windows: how to detect if other users logged in

30 Oct 2018, 20:27

I want to check if other users are logged into the machine.

How best can I do this?
I guess I need to make a DOS command and grab text - this is fine.
But... what DOS command?

I know there are a few ways.
Currently I just launch Task Manager and look at the User Tab.

Thanks.
Eureka
Posts: 65
Joined: 03 Apr 2018, 13:31

Re: Windows: how to detect if other users logged in

05 Nov 2018, 19:13

QUSER.EXE

Code: Select all

quser.exe | findstr.exe /v /i /B /C:" USERNAME" /C:">%USERNAME%" || Echo Nobody else logged on
Will show you all OTHER users that are logged on OR "Nobody else logged on"

quser.exe | findstr.exe /v /i /B /C:" USERNAME" /C:">%USERNAME%"
Will exit with ERRORLEVEL 1 if no one else is logged on;
ERRORLEVEL 0 when someone else is logged on.


HTH ..
omar
Posts: 545
Joined: 22 Oct 2015, 17:56

Re: Windows: how to detect if other users logged in

27 Nov 2019, 21:06

Aha!
I was just about to ask the same question :)
Thanks for the replies.
Can you tell me how I execute in AHK and then get the output and make a decision what to do in AHK?

Thanks.
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Windows: how to detect if other users logged in

30 Nov 2019, 06:49

Code: Select all

MsgBox % StdOut("quser")
ExitApp 

StdOut(CmdString)
{
	dhw := A_DetectHiddenWindows
	DetectHiddenWindows, On
	Run, %ComSpec%,, Hide, cPid
	WinWait, ahk_pid %cPid%
	DetectHiddenWindows, %dhw%
	DllCall("AttachConsole", "uint", cPid)
	objShell := ComObjCreate("WScript.Shell")
	objExec := objShell.Exec(CmdString)
    while, !objExec.StdOut.AtEndOfStream
        strStdOut := objExec.StdOut.ReadAll()
	DllCall("FreeConsole")
	Process, Close, %cPid%
	Return strStdOut
}
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
omar
Posts: 545
Joined: 22 Oct 2015, 17:56

Re: Windows: how to detect if other users logged in

02 Dec 2019, 22:44

@Odlanir
I tried that code
The PC went mad.
Tried opening loooads of files 😕
Took me 10 minutes to get back control.

Isn't there a way to execute the DOS command: quser.exe | findstr.exe /v /i /B /C:" USERNAME" /C:">%USERNAME%" || Echo Nobody else logged on

And feed that back to AHK?

Thanks.
omar
Posts: 545
Joined: 22 Oct 2015, 17:56

Re: Windows: how to detect if other users logged in

16 Dec 2019, 04:55

Anyone? Still stuck on this one

quser.exe - is this a standard windows program?
It's not in my Windows 10.

Thanks
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Windows: how to detect if other users logged in

16 Dec 2019, 05:39

E.g.

Code: Select all

; ===========================================================================================================================
; Function .....: NetWkstaUserEnum
; Link .........: https://docs.microsoft.com/en-us/windows/win32/api/lmwksta/nf-lmwksta-netwkstauserenum
;
; Description ..: The NetWkstaUserEnum function lists information about all users currently logged on to the workstation.
;                 This list includes interactive, service and batch logons.
; ===========================================================================================================================

NetWkstaUserEnum(ServerName := "127.0.0.1")
{
	static NERR_SUCCESS := 0
	static MAX_PREFERRED_LENGTH := -1
	static WKSTA_USER_INFO_1 := 1

	if (NET_API_STATUS := DllCall("netapi32\NetWkstaUserEnum", "wstr",  ServerName
															 , "uint",  WKSTA_USER_INFO_1
															 , "ptr*",  buf
															 , "uint",  MAX_PREFERRED_LENGTH
															 , "uint*", EntriesRead
															 , "uint*", TotalEntries
															 , "uint*", 0
															 , "uint")  != NERR_SUCCESS) {
		throw Exception("NetWkstaUserEnum failed: " NET_API_STATUS, -1), DllCall("netapi32\NetApiBufferFree", "ptr", buf, "uint")
		}

	addr := buf, WKSTA_USER_INFO := []
	loop % EntriesRead
	{
		WKSTA_USER_INFO[A_Index, "username"]     := StrGet(NumGet(addr + A_PtrSize * 0, "uptr"), "utf-16")
		WKSTA_USER_INFO[A_Index, "logon_domain"] := StrGet(NumGet(addr + A_PtrSize * 1, "uptr"), "utf-16")
		WKSTA_USER_INFO[A_Index, "oth_domains"]  := StrGet(NumGet(addr + A_PtrSize * 2, "uptr"), "utf-16")
		WKSTA_USER_INFO[A_Index, "logon_server"] := StrGet(NumGet(addr + A_PtrSize * 3, "uptr"), "utf-16")
		addr += A_PtrSize * 4
	}

	return WKSTA_USER_INFO, DllCall("netapi32\NetApiBufferFree", "ptr", buf, "uint")
}

; ===========================================================================================================================

GetWkstaUser := NetWkstaUserEnum()
for i, v in GetWkstaUser {
	for k, v in GetWkstaUser[i]
		output .= k ": " v "`n"
	MsgBox % output
	output := ""
}

; ===========================================================================================================================
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
omar
Posts: 545
Joined: 22 Oct 2015, 17:56

Re: Windows: how to detect if other users logged in

31 Dec 2019, 21:58

@jNizM need some help with your code.
I've now tested fully. It's a promising start.

I simply need to know if the number of users logged in is more than 1.
If I can get a list of the logged in users, that would be amazing.

Your code gives a popup for every used logged in.
If I have 3 users logged in, then I'll get 3 popups.

This is OK, I can just read the length of the array.

PROBLEM: when JUST one user is logged in, I get 2 popups.
The same popup comes up twice.

When 2 users logged in - 2 popups.
When 3 users logged in - 3 popups.

I'm completely confused about the code to get the user information in the first place!

>> https://docs.microsoft.com/en-us/windows/win32/api/lmwksta/nf-lmwksta-netwkstauserenum
That shows C++ code??

>> ServerName := "127.0.0.1"
What's that about? (I guess if I understood the rest, maybe it would be clearer!)

Really stuck on this one. Would be great to hear what you (or anyone else) say.

Thanks.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Windows: how to detect if other users logged in

07 Jan 2020, 03:17

omar wrote:
31 Dec 2019, 21:58
PROBLEM: when JUST one user is logged in, I get 2 popups.
The same popup comes up twice.

When 2 users logged in - 2 popups.
When 3 users logged in - 3 popups.

I'm completely confused about the code to get the user information in the first place!
It also shows your local computername as logged in. (e.g. MYCOMPUTER$)

Yes it shows the C++ Example

omar wrote:
31 Dec 2019, 21:58
>> ServerName := "127.0.0.1"
What's that about? (I guess if I understood the rest, maybe it would be clearer!)
127.0.0.1 is the local IP-Address from your local computer. (see: https://en.wikipedia.org/wiki/Localhost)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 184 guests