Detecting a windows user change

My app auto runs. When multiple users are logged onto a computer it will be running multiple times which is the problem, as it binds to a UDP port. In python this generates an error as the UDP port is already bound, but XOJO doesnt do this . I can sort this out if I can detect from within the app if its being run but the current active user.
Is there any easy way in windows to detect if there has been a user switch ,or if my app is the current user, ?

You cannot bind a TCP port twice. You can test this easily by putting two UDPSockets on a window an this code into Window.Open:

UDPSocket1.Port = 26999 UDPSocket2.Port = 26999 UDPSocket1.Connect() UDPSocket2.Connect()
Put BREAK statements in the Error events of the two sockets. When running the application UDPSocket2.Error will be raised.

You are right!, It does generate an error.!
So I can check for that , but I would like to be able have the software active when a new user logs on, and de-active the old windows user. Its not a common situation but does occur. I only ned the current windows user to have the program actually bound to the UDP.

You should probably design it to run as a service and not a user process.

may be you can use the activate and deactivate events of the window to check when some other user logs in ?

I think a service may be the answer, but having never written one, it seems a little daunting.However, worth a shot I think.
What is the best way to communicate with a service,
via a port ?

Interestingly, I wrote a small app that sent the system.mousex and mouse y values to a remote computer.Runs every 5 seconds via a timer.

dim dg as new Datagram
dg.Address =“”" "+str(system.mousey)

Works fine when the user is active,
Once the user switches to another user account on windows the values on mousex goto 262066716 and mousey to 15726720
and dont reflect where the mouse is for the new user who is active.

I guess this stops one user having software that spies on the other user?

Anyway, when switching back to the original user, the values are correct again. So this may be an easy(But undocumented/risky ) way of determining if your program is being run by the active user.

ie if system.mousex >10000 then 'you are not the active user

Check the environment variable %USERNAME% to find out the current user. I think that might work.

Tried that, it just gives me the name of the user that is running the app, doesnt tell me if they are the active user or just in the background and inactive.

It’s worth noting that as a Windows Service, you’ll probably always get bad values for mouse coordinates as the user is technically never logged in.

You may also have to deal with Windows security getting in your way if you try to read or write any data from the user accounts.

Bit of a late reply, not been here in a while.

Get the current process ID when you start your app:

Get the sessionid from that process id and store it for later comparison

Listen for WM_WTSSESSION_CHANGE through WindowProc in all your running apps.

if the app with the session id receives WTS_CONSOLE_DISCONNECT then stop its UDP listen

if the app with the session id receives WTS_CONSOLE_CONNECT then start its UDP listen

This will only leave one app listening, the one associated with the console user (wont work if you have this on a terminal server where concurrent logged in users, for obvious reasons, see below if you need that).

I was going to do the following, but decided it was a bit more complex than the above (its not fully thought through as I stopped it half way through to do the above), interesting read though:

You can get the username of the person who runs the app by calling:

Store this in each execution of the app for each user (used later)

will get the session id of the console

then use the id obtained previously to call

using WTSUserName in WTSInfoClass

which will give you the username of the user who is currently on the console

IPC (or use whatever method you choose) to talk to each running version of your program, send them the username of the person on the console, if that is them, then turn of the UDP listen, if not, turn it off.

Probably neater to write it as a service though, depends what you need it to do.

Ok, Late reply- discovered a few things with xojo windows

If you have a declare method to move the mouse ie

Public Sub SetCursorPosition(x as int32, y as Int32) 'set mouse cursor position '#If TargetWin32 Then Soft Declare Sub SetCursorPos Lib "User32" ( X as int32, Y as int32 ) SetCursorPos(X,Y) '#Endif End Sub

This will only work if the app is running as the active user. If you are not the active user the mouse will not move.
Usually when you log out as the active user the system.mousex and system.mousey will return 0 (but not always)

so with this code , running every second from a timer

[code]dim x,y as integer

x =system.MouseX
if x=0 and y = 0 then Module1.SetCursorPosition(0,1)
dim zd as new datagram
zd.Address = “”
zd.Port = 10150
zd.Data = "Mouse coords “+str(x)+” “+str(y)+” “+str(system.mousex)+” "+str(system.mousey)

I can tell if I am the active user. (sends mouse cords to another computer listening on port 10150 )

If system.mousex is read into an integer then converted to a string it returns 0 if the app is running as an inactive user.
interestingly str(system.mousex) and str(system.mousey) will display an incorrect value, not sure why.

so, if the mouse cords are 0,0 then try to move the mouse . If its moved then you are the active user.
If it doesn’t move but stays at 0,0 then you are not.

Seems convoluted but makes sense in that you don’t want an inactive user tracking your mouse movements, or controlling your mouse.